Skip to content

Commit

Permalink
✨ Allow to bind: custom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ouvreboite committed May 2, 2024
1 parent 0919128 commit 5e53482
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/svelte/src/compiler/compile/compiler_warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,12 @@ export default {
code: 'illegal-attribute-character',
message:
"Attributes should not contain ':' characters to prevent ambiguity with Svelte directives"
}
},
binding_custom_element: /**
* @param {string} element
* @param {string} binding
*/ (element, binding) => ({
code: 'binding-custom-element',
message: `Binding on custom element <${element}> is not checked. You need to ensure '${binding}' is a valid property.`
})
};
7 changes: 6 additions & 1 deletion packages/svelte/src/compiler/compile/nodes/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,12 @@ export default class Element extends Node {
};
this.bindings.forEach((binding) => {
const { name } = binding;
if (name === 'value') {

//custom elements (i.e. web components) can be bound to any attribute, we simply emit a warning
//to identify a custom element, we check if the element name contains a hyphen
if (this.name.includes('-')) {
return component.warn(binding, compiler_warnings.binding_custom_element(this.name, name));
} else if (name === 'value') {
if (this.name !== 'input' && this.name !== 'textarea' && this.name !== 'select') {
return component.error(
binding,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let myValue;
let myElement;
</script>

<my-element bind:value={myValue}></my-element>
<my-element bind:this={myElement}></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "binding-custom-element",
"message": "Binding on custom element <my-element> is not checked. You need to ensure 'value' is a valid property.",
"start": {
"line": 6,
"column": 12
},
"end": {
"line": 6,
"column": 22
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"end": {
"line": 5,
"column": 21
"column": 25
}
}
]

0 comments on commit 5e53482

Please sign in to comment.