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: preserve property case for custom elements #5193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
const element = this.parent;
const { name, property_name, should_cache, is_indirectly_bound_value } = this;

const is_custom_element = /-/.test(element.node.name);

// xlink is a special case... we could maybe extend this to generic
// namespaced attributes but I'm not sure that's applicable in
// HTML5?
const method = /-/.test(element.node.name)
const method = is_custom_element
? '@set_custom_element_data'
: name.slice(0, 6) === 'xlink:'
? '@xlink_attr'
Expand Down Expand Up @@ -130,10 +132,12 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
? b`@prop_dev(${element.var}, "${property_name}", ${should_cache ? this.last : value});`
: b`${element.var}.${property_name} = ${should_cache ? this.last : value};`;
} else {
// use this.node.name when the element is a custom element to preserve property case-sensitivity
const custom_element_aware_name = is_custom_element ? this.node.name : name;
block.chunks.hydrate.push(
b`${method}(${element.var}, "${name}", ${init});`
b`${method}(${element.var}, "${custom_element_aware_name}", ${init});`
);
updater = b`${method}(${element.var}, "${name}", ${should_cache ? this.last : value});`;
updater = b`${method}(${element.var}, "${custom_element_aware_name}", ${should_cache ? this.last : value});`;
}

if (is_indirectly_bound_value) {
Expand Down Expand Up @@ -387,4 +391,4 @@ function is_indirectly_bound_value(attribute: AttributeWrapper) {
(binding) =>
/checked|group/.test(binding.name)
)));
}
}
2 changes: 1 addition & 1 deletion test/custom-elements/samples/props/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export let items = ['a', 'b', 'c'];
</script>

<my-widget class="foo" {items}/>
<my-widget class="foo" widgetItems={items}/>
6 changes: 3 additions & 3 deletions test/custom-elements/samples/props/my-widget.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<svelte:options tag="my-widget"/>

<script>
export let items = [];
export let widgetItems = [];
</script>

<p>{items.length} items</p>
<p>{items.join(', ')}</p>
<p>{widgetItems.length} items</p>
<p>{widgetItems.join(', ')}</p>