Skip to content

Commit 3a7685f

Browse files
dummdidummbenmccanntanhauhau
authored
fix: special-case width/height attribute during spread (#8412)
fixes #6752 --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Tan Li Hau <[email protected]>
1 parent def1890 commit 3a7685f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/runtime/internal/dom.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ export function attr(node: Element, attribute: string, value?: string) {
306306
else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value);
307307
}
308308

309+
/**
310+
* List of attributes that should always be set through the attr method,
311+
* because updating them through the property setter doesn't work reliably.
312+
* In the example of `width`/`height`, the problem is that the setter only
313+
* accepts numeric values, but the attribute can also be set to a string like `50%`.
314+
* If this list becomes too big, rethink this approach.
315+
*/
316+
const always_set_through_set_attribute = ['width', 'height'];
317+
309318
export function set_attributes(node: Element & ElementCSSInlineStyle, attributes: { [x: string]: string }) {
310319
// @ts-ignore
311320
const descriptors = Object.getOwnPropertyDescriptors(node.__proto__);
@@ -316,7 +325,7 @@ export function set_attributes(node: Element & ElementCSSInlineStyle, attributes
316325
node.style.cssText = attributes[key];
317326
} else if (key === '__value') {
318327
(node as any).value = node[key] = attributes[key];
319-
} else if (descriptors[key] && descriptors[key].set) {
328+
} else if (descriptors[key] && descriptors[key].set && always_set_through_set_attribute.indexOf(key) === -1) {
320329
node[key] = attributes[key];
321330
} else {
322331
attr(node, key, attributes[key]);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
// https://github.com/sveltejs/svelte/issues/6752
3+
html: '<img height="100%" width="100%" alt="" />'
4+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<img height="100%" width="100%" alt="" {...$$restProps} />

0 commit comments

Comments
 (0)