Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] call attribute bindings for custom element if <svelte:element> render custom element #7766

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,31 @@ export default class ElementWrapper extends Wrapper {

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

block.chunks.hydrate.push(
b`${fn}(${this.var}, ${data});`
);
if (this.node.is_dynamic_element) {
// call attribute bindings for custom element if tag is custom element
const tag = this.node.tag_expr.manipulate(block);
const attr_update = b`
if (/-/.test(${tag})) {
@set_custom_element_data_map(${this.var}, ${data});
} else {
${fn}(${this.var}, ${data});
}`;
block.chunks.hydrate.push(attr_update);
block.chunks.update.push(b`
${data} = @get_spread_update(${levels}, [${updates}]);
${attr_update}`
);
} else {
block.chunks.hydrate.push(
b`${fn}(${this.var}, ${data});`
);

block.chunks.update.push(b`
${fn}(${this.var}, ${data} = @get_spread_update(${levels}, [
${updates}
]));
`);
block.chunks.update.push(b`
${fn}(${this.var}, ${data} = @get_spread_update(${levels}, [
${updates}
]));
`);
}

// handle edge cases for elements
if (this.node.name === 'select') {
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 @@ -310,6 +310,12 @@ export function set_svg_attributes(node: Element & ElementCSSInlineStyle, attrib
}
}

export function set_custom_element_data_map(node, data_map: Record<string, unknown>) {
Object.keys(data_map).forEach((key) => {
set_custom_element_data(node, key, data_map[key]);
});
}

export function set_custom_element_data(node, prop, value) {
if (prop in node) {
node[prop] = typeof node[prop] === 'boolean' && value === '' ? true : value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default {
skip_if_ssr: true,
props: {
tag: 'my-custom-element',
name: null
},
html: `
<my-custom-element id="a">Hello null!</my-custom-element>
<my-custom-element id="b">Hello null!</my-custom-element>
`,

test({ assert, component, target }) {
component.name = undefined;
assert.htmlEqual(
target.innerHTML,
`<my-custom-element id="a">Hello undefined!</my-custom-element>
<my-custom-element id="b">Hello undefined!</my-custom-element>`
);

component.name = 'foo';
assert.htmlEqual(
target.innerHTML,
`<my-custom-element id="a">Hello foo!</my-custom-element>
<my-custom-element id="b">Hello foo!</my-custom-element>`
);

component.tag = null;
assert.htmlEqual(
target.innerHTML,
'<my-custom-element id="b">Hello foo!</my-custom-element>'
);

component.tag = 'div';
assert.htmlEqual(
target.innerHTML,
`<div name="foo" id="a"></div>
<my-custom-element id="b">Hello foo!</my-custom-element>`
);

component.tag = 'my-custom-element';
assert.htmlEqual(
target.innerHTML,
`<my-custom-element id="a">Hello foo!</my-custom-element>
<my-custom-element id="b">Hello foo!</my-custom-element>`
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script>
class MyCustomElement extends HTMLElement {
constructor() {
super();
this._name = null;
}

/**
* @param {string} name
*/
set name(name) {
this._name = name;
this.render();
}

connectedCallback() {
this.render();
}

render() {
this.innerHTML = "Hello " + this._name + "!";
}
}

window.customElements.define("my-custom-element", MyCustomElement);

export let tag;
export let name;
</script>

<svelte:element this="{tag}" {name} id="a" />
<my-custom-element {name} id="b" />