Skip to content

Commit

Permalink
prevent illegal attribute names at compile time (sveltejs#4650)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfeld committed Apr 23, 2020
1 parent 68ac96b commit ddfb751
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/compiler/compile/nodes/Element.ts
Expand Up @@ -378,6 +378,14 @@ export default class Element extends Node {
}
}


if (/(^[0-9-.])|[\^$@%&#?!|()[\]{}^*+~;]/.test(name)) {
component.error(attribute, {
code: `illegal-attribute`,
message: `'${name}' is not a valid attribute name`,
});
}

if (name === 'slot') {
if (!attribute.is_static) {
component.error(attribute, {
Expand Down Expand Up @@ -768,4 +776,4 @@ function within_custom_element(parent: INode) {
parent = parent.parent;
}
return false;
}
}
15 changes: 15 additions & 0 deletions test/validator/samples/attribute-invalid-name-2/errors.json
@@ -0,0 +1,15 @@
[{
"code": "illegal-attribute",
"message": "'3aa' is not a valid attribute name",
"start": {
"line": 1,
"column": 3,
"character": 3
},
"end": {
"line": 1,
"column": 12,
"character": 12
},
"pos": 3
}]
@@ -0,0 +1 @@
<p 3aa="abc">Test</p>
15 changes: 15 additions & 0 deletions test/validator/samples/attribute-invalid-name-3/errors.json
@@ -0,0 +1,15 @@
[{
"code": "illegal-attribute",
"message": "'a*a' is not a valid attribute name",
"start": {
"line": 1,
"column": 3,
"character": 3
},
"end": {
"line": 1,
"column": 6,
"character": 6
},
"pos": 3
}]
@@ -0,0 +1 @@
<p a*a>Test</p>
15 changes: 15 additions & 0 deletions test/validator/samples/attribute-invalid-name-4/errors.json
@@ -0,0 +1,15 @@
[{
"code": "illegal-attribute",
"message": "'-a' is not a valid attribute name",
"start": {
"line": 1,
"column": 3,
"character": 3
},
"end": {
"line": 1,
"column": 5,
"character": 5
},
"pos": 3
}]
@@ -0,0 +1 @@
<p -a>Test</p>
15 changes: 15 additions & 0 deletions test/validator/samples/attribute-invalid-name-5/errors.json
@@ -0,0 +1,15 @@
[{
"code": "illegal-attribute",
"message": "'a;' is not a valid attribute name",
"start": {
"line": 1,
"column": 3,
"character": 3
},
"end": {
"line": 1,
"column": 11,
"character": 11
},
"pos": 3
}]
@@ -0,0 +1 @@
<p a;="abc">Test</p>
15 changes: 15 additions & 0 deletions test/validator/samples/attribute-invalid-name/errors.json
@@ -0,0 +1,15 @@
[{
"code": "illegal-attribute",
"message": "'}' is not a valid attribute name",
"start": {
"line": 1,
"column": 3,
"character": 3
},
"end": {
"line": 1,
"column": 4,
"character": 4
},
"pos": 3
}]
1 change: 1 addition & 0 deletions test/validator/samples/attribute-invalid-name/input.svelte
@@ -0,0 +1 @@
<p }>Test</p>

0 comments on commit ddfb751

Please sign in to comment.