Skip to content

Commit e287f47

Browse files
committed
🐛 Allow to use bind:property on custom elements
1 parent 0919128 commit e287f47

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

packages/svelte/src/compiler/compile/compiler_warnings.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -306,5 +306,12 @@ export default {
306306
code: 'illegal-attribute-character',
307307
message:
308308
"Attributes should not contain ':' characters to prevent ambiguity with Svelte directives"
309-
}
309+
},
310+
binding_custom_element: /**
311+
* @param {string} element
312+
* @param {string} binding
313+
*/ (element, binding) => ({
314+
code: 'binding-custom-element',
315+
message: `Binding on custom element <${element}> is not checked. You need to ensure '${binding}' is a valid property.`
316+
})
310317
};

packages/svelte/src/compiler/compile/nodes/Element.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,12 @@ export default class Element extends Node {
11001100
};
11011101
this.bindings.forEach((binding) => {
11021102
const { name } = binding;
1103-
if (name === 'value') {
1103+
1104+
//custom elements (i.e. web components) can be bound to any attribute, we simply emit a warning
1105+
//to identify a custom element, we check if the element name contains a hyphen
1106+
if (this.name.includes('-') && name !== 'this') {
1107+
return component.warn(binding, compiler_warnings.binding_custom_element(this.name, name));
1108+
} else if (name === 'value') {
11041109
if (this.name !== 'input' && this.name !== 'textarea' && this.name !== 'select') {
11051110
return component.error(
11061111
binding,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
let myValue;
3+
let myElement;
4+
</script>
5+
6+
<my-element bind:value={myValue} bind:this={myElement}></my-element>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "binding-custom-element",
4+
"message": "Binding on custom element <my-element> is not checked. You need to ensure 'value' is a valid property.",
5+
"start": {
6+
"line": 6,
7+
"column": 12
8+
},
9+
"end": {
10+
"line": 6,
11+
"column": 32
12+
}
13+
}
14+
]

packages/svelte/test/validator/samples/binding-dimensions-svg/errors.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"end": {
1010
"line": 5,
11-
"column": 21
11+
"column": 25
1212
}
1313
}
1414
]

0 commit comments

Comments
 (0)