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

chore: warn about : in attributes and props #8633

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Treat slots as if they don't exist when using CSS adjacent and general sibling combinators ([#8284](https://github.com/sveltejs/svelte/issues/8284))
* 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)).
* Explicitly disallow `var` declarations extending the reactive statement scope ([#6800](https://github.com/sveltejs/svelte/pull/6800))
* Warn about `:` in attributes and props to prevent ambiguity with Svelte directives ([#6823](https://github.com/sveltejs/svelte/issues/6823))

## 3.59.1

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,9 @@ export default {
code: 'avoid-mouse-events-on-document',
message:
'Mouse enter/leave events on the document are not supported in all browsers and should be avoided'
},
illegal_attribute_character: {
code: 'illegal-attribute-character',
message: "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives"
}
};
14 changes: 14 additions & 0 deletions src/compiler/compile/nodes/Attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import add_to_set from '../utils/add_to_set.js';
import Node from './shared/Node.js';
import Expression from './shared/Expression.js';
import { x } from 'code-red';
import compiler_warnings from '../compiler_warnings.js';

/** @extends Node<'Attribute' | 'Spread', import('./Element.js').default> */
export default class Attribute extends Node {
Expand Down Expand Up @@ -39,6 +40,7 @@ export default class Attribute extends Node {
constructor(component, parent, scope, info) {
super(component, parent, scope, info);
this.scope = scope;

if (info.type === 'Spread') {
this.name = null;
this.is_spread = true;
Expand All @@ -64,10 +66,22 @@ export default class Attribute extends Node {
}
);
}

if (this.dependencies.size > 0) {
parent.cannot_use_innerhtml();
parent.not_static_content();
}

// TODO Svelte 5: Think about moving this into the parser and make it an error
Copy link
Member

Choose a reason for hiding this comment

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

or should we merge this PR in the Svelte 3 branch and make it an error in Svelte 4?

if (
this.name &&
this.name.includes(':') &&
!this.name.startsWith('xmlns:') &&
!this.name.startsWith('xlink:') &&
!this.name.startsWith('xml:')
) {
component.warn(this, compiler_warnings.illegal_attribute_character);
}
}
get_dependencies() {
if (this.is_spread) return this.expression.dynamic_dependencies();
Expand Down
1 change: 0 additions & 1 deletion src/compiler/parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ function get_directive_type(name) {
if (name === 'style') return 'StyleDirective';
if (name === 'on') return 'EventHandler';
if (name === 'let') return 'Let';
if (name === 'ref') return 'Ref';
if (name === 'in' || name === 'out' || name === 'transition') return 'Transition';
}

Expand Down
15 changes: 15 additions & 0 deletions test/validator/samples/illegal-attribute-character/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import C from './irrelevant';
</script>

<button on:click />
<button xml:click />
<button xmlns:click />
<button xlink:click />
<C on:click />
<C xml:click />
<C xmlns:click />
<C xlink:click />

<button foo:bar />
<C foo:bar />
26 changes: 26 additions & 0 deletions test/validator/samples/illegal-attribute-character/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"code": "illegal-attribute-character",
"end": {
"column": 15,
"line": 14
},
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
"start": {
"column": 8,
"line": 14
}
},
{
"code": "illegal-attribute-character",
"end": {
"column": 10,
"line": 15
},
"message": "Attributes should not contain ':' characters to prevent ambiguity with Svelte directives",
"start": {
"column": 3,
"line": 15
}
}
]