Skip to content

fix: support camelCase properties on custom elements #9328

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

Merged
merged 4 commits into from
Oct 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-cars-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: support camelCase properties on custom elements
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
this.parent.has_dynamic_value = true;
}
}
if (this.parent.node.namespace == namespaces.foreign) {
// leave attribute case alone for elements in the "foreign" namespace
if (this.parent.node.namespace == namespaces.foreign || this.parent.node.name.includes('-')) {
// leave attribute case alone for elements in the "foreign" namespace and for custom elements
this.name = this.node.name;
this.metadata = this.get_metadata();
this.is_indirectly_bound_value = false;
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/src/runtime/internal/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ export function set_custom_element_data_map(node, data_map) {
/**
* @returns {void} */
export function set_custom_element_data(node, prop, value) {
if (prop in node) {
const lower = prop.toLowerCase(); // for backwards compatibility with existing behavior we do lowercase first
if (lower in node) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a particularly good thing to do. Can you remove this in Svelte 5?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we'll likely do that

node[lower] = typeof node[lower] === 'boolean' && value === '' ? true : value;
} else if (prop in node) {
node[prop] = typeof node[prop] === 'boolean' && value === '' ? true : value;
} else {
attr(node, prop, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
skip_if_ssr: true,
skip_if_hydrate: true,
html: `
<my-custom-element>Hello World!</my-custom-element>
`
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
class MyCustomElement extends HTMLElement {
constructor() {
super();
this._obj = null;
}

set camelCase(obj) {
this._obj = obj;
this.render();
}

connectedCallback() {
this.render();
}

render() {
this.innerHTML = 'Hello ' + this._obj.text + '!';
}
}

window.customElements.define('my-custom-element', MyCustomElement);
</script>

<my-custom-element camelCase={{ text: 'World' }} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
skip_if_ssr: true,
skip_if_hydrate: true,
html: `
<my-custom-inheritance-element>Hello World!</my-custom-inheritance-element>
`
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
class MyCustomElement extends HTMLElement {
constructor() {
super();
this._obj = null;
this._text = null;
}

set text(text) {
this._text = text;
this.render();
}

set camelCase(obj) {
this._obj = obj;
this.render();
}

connectedCallback() {
this.render();
}

render() {
this.innerHTML = 'Hello ' + this._obj.text + this._text;
}
}

class Extended extends MyCustomElement {}

window.customElements.define('my-custom-inheritance-element', Extended);
</script>

<my-custom-inheritance-element camelCase={{ text: 'World' }} text="!" />