Skip to content

Commit 969bfd8

Browse files
committed
chore: warn about : in attributes and props
...to prevent ambiguity with Svelte directives closes #6823
1 parent a40af4d commit 969bfd8

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Treat slots as if they don't exist when using CSS adjacent and general sibling combinators ([#8284](https://github.com/sveltejs/svelte/issues/8284))
2828
* Fix transitions so that they don't require a `style-src 'unsafe-inline'` Content Security Policy (CSP) ([#6662](https://github.com/sveltejs/svelte/issues/6662)).
2929
* Explicitly disallow `var` declarations extending the reactive statement scope ([#6800](https://github.com/sveltejs/svelte/pull/6800))
30+
* Warn about `:` in attributes and props to prevent ambiguity with Svelte directives ([#6823](https://github.com/sveltejs/svelte/issues/6823))
3031

3132
## 3.59.1
3233

Diff for: src/compiler/compile/compiler_warnings.js

+4
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,9 @@ export default {
301301
code: 'avoid-mouse-events-on-document',
302302
message:
303303
'Mouse enter/leave events on the document are not supported in all browsers and should be avoided'
304+
},
305+
illegal_attribute_character: {
306+
code: `illegal-attribute-character`,
307+
message: `Attributes should not contain ':' characters to prevent ambiguity with Svelte directives`
304308
}
305309
};

Diff for: src/compiler/compile/nodes/Attribute.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import add_to_set from '../utils/add_to_set.js';
33
import Node from './shared/Node.js';
44
import Expression from './shared/Expression.js';
55
import { x } from 'code-red';
6+
import compiler_warnings from '../compiler_warnings.js';
67

78
/** @extends Node<'Attribute' | 'Spread', import('./Element.js').default> */
89
export default class Attribute extends Node {
@@ -39,6 +40,7 @@ export default class Attribute extends Node {
3940
constructor(component, parent, scope, info) {
4041
super(component, parent, scope, info);
4142
this.scope = scope;
43+
4244
if (info.type === 'Spread') {
4345
this.name = null;
4446
this.is_spread = true;
@@ -64,10 +66,21 @@ export default class Attribute extends Node {
6466
}
6567
);
6668
}
69+
6770
if (this.dependencies.size > 0) {
6871
parent.cannot_use_innerhtml();
6972
parent.not_static_content();
7073
}
74+
75+
// TODO Svelte 5: Think about moving this into the parser and make it an error
76+
if (
77+
this.name.includes(':') &&
78+
!this.name.startsWith('xmlns:') &&
79+
!this.name.startsWith('xlink:') &&
80+
!this.name.startsWith('xml:')
81+
) {
82+
component.warn(this, compiler_warnings.illegal_attribute_character);
83+
}
7184
}
7285
get_dependencies() {
7386
if (this.is_spread) return this.expression.dynamic_dependencies();

Diff for: src/compiler/parse/state/tag.js

-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ function get_directive_type(name) {
433433
if (name === 'style') return 'StyleDirective';
434434
if (name === 'on') return 'EventHandler';
435435
if (name === 'let') return 'Let';
436-
if (name === 'ref') return 'Ref';
437436
if (name === 'in' || name === 'out' || name === 'transition') return 'Transition';
438437
}
439438

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import C from './irrelevant';
3+
</script>
4+
5+
<button on:click />
6+
<button xml:click />
7+
<button xmlns:click />
8+
<button xlink:click />
9+
<C on:click />
10+
<C xml:click />
11+
<C xmlns:click />
12+
<C xlink:click />
13+
14+
<button foo:bar />
15+
<C foo:bar />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"code": "illegal-attribute-character",
4+
"end": {
5+
"column": 15,
6+
"line": 14
7+
},
8+
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
9+
"start": {
10+
"column": 8,
11+
"line": 14
12+
}
13+
},
14+
{
15+
"code": "illegal-attribute-character",
16+
"end": {
17+
"column": 10,
18+
"line": 15
19+
},
20+
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
21+
"start": {
22+
"column": 3,
23+
"line": 15
24+
}
25+
}
26+
]

0 commit comments

Comments
 (0)