Skip to content

Commit

Permalink
only use setAttribute with SVG spread props - fixes #3522
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 7, 2019
1 parent 41f5961 commit 8519003
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,14 @@ export default class ElementWrapper extends Wrapper {
}
`);

const fn = this.node.namespace === namespaces.svg ? `set_svg_attributes` : `set_attributes`;

block.builders.hydrate.add_line(
`@set_attributes(${this.var}, ${data});`
`@${fn}(${this.var}, ${data});`
);

block.builders.update.add_block(deindent`
@set_attributes(${this.var}, @get_spread_update(${levels}, [
@${fn}(${this.var}, @get_spread_update(${levels}, [
${updates.join(',\n')}
]));
`);
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export function set_attributes(node: Element & ElementCSSInlineStyle, attributes
}
}

export function set_svg_attributes(node: Element & ElementCSSInlineStyle, attributes: { [x: string]: string }) {
for (const key in attributes) {
attr(node, key, attributes[key]);
}
}

export function set_custom_element_data(node, prop, value) {
if (prop in node) {
node[prop] = value;
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/svg-spread/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
html: `
<svg height="400" width="400">
<rect x="50" y="50" width="100" height="75" fill="#ff0000"></rect>
</svg>
`
};
7 changes: 7 additions & 0 deletions test/runtime/samples/svg-spread/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const style = { fill: '#ff0000', x: '50', y: '50', width: '100', height: '75'};
</script>

<svg width="400" height="400">
<rect {...style}/>
</svg>

0 comments on commit 8519003

Please sign in to comment.