Skip to content

Commit c50de89

Browse files
committed
fix: disallow invalid attributes for <svelte:window> and <svelte:document>
1 parent d7caf08 commit c50de89

File tree

10 files changed

+85
-0
lines changed

10 files changed

+85
-0
lines changed

.changeset/fast-ligers-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: disallow invalid attributes for `<svelte:window>` and `<svelte:document>`

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,12 @@ Expected whitespace
412412
`$host()` can only be used inside custom element component instances
413413
```
414414

415+
### illegal_element_attribute
416+
417+
```
418+
`<%name%>` does not support non-event attributes or spread attributes
419+
```
420+
415421
### import_svelte_internal_forbidden
416422

417423
```

packages/svelte/messages/compile-errors/template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@
172172

173173
> Expected whitespace
174174
175+
## illegal_element_attribute
176+
177+
> `<%name%>` does not support non-event attributes or spread attributes
178+
175179
## js_parse_error
176180

177181
> %message%

packages/svelte/src/compiler/errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,16 @@ export function expected_whitespace(node) {
984984
e(node, "expected_whitespace", "Expected whitespace");
985985
}
986986

987+
/**
988+
* `<%name%>` does not support non-event attributes or spread attributes
989+
* @param {null | number | NodeLike} node
990+
* @param {string} name
991+
* @returns {never}
992+
*/
993+
export function illegal_element_attribute(node, name) {
994+
e(node, "illegal_element_attribute", `\`<${name}>\` does not support non-event attributes or spread attributes`);
995+
}
996+
987997
/**
988998
* %message%
989999
* @param {null | number | NodeLike} node
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Context } from '../types' */
33
import { disallow_children } from './shared/special-element.js';
4+
import * as e from '../../../errors.js';
5+
import { is_event_attribute } from '../../../utils/ast.js';
46

57
/**
68
* @param {AST.SvelteDocument} node
79
* @param {Context} context
810
*/
911
export function SvelteDocument(node, context) {
1012
disallow_children(node);
13+
14+
for (const attribute of node.attributes) {
15+
if (
16+
attribute.type === 'SpreadAttribute' ||
17+
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
18+
) {
19+
e.illegal_element_attribute(attribute, 'svelte:document');
20+
}
21+
}
22+
1123
context.next();
1224
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Context } from '../types' */
33
import { disallow_children } from './shared/special-element.js';
4+
import * as e from '../../../errors.js';
5+
import { is_event_attribute } from '../../../utils/ast.js';
46

57
/**
68
* @param {AST.SvelteWindow} node
79
* @param {Context} context
810
*/
911
export function SvelteWindow(node, context) {
1012
disallow_children(node);
13+
14+
for (const attribute of node.attributes) {
15+
if (
16+
attribute.type === 'SpreadAttribute' ||
17+
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
18+
) {
19+
e.illegal_element_attribute(attribute, 'svelte:window');
20+
}
21+
}
22+
1123
context.next();
1224
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "illegal_element_attribute",
4+
"message": "`<svelte:document>` does not support non-event attributes or spread attributes",
5+
"start": {
6+
"line": 4,
7+
"column": 17
8+
},
9+
"end": {
10+
"line": 4,
11+
"column": 23
12+
}
13+
}
14+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
let a = {};
3+
</script>
4+
<svelte:document {...a}></svelte:document>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "illegal_element_attribute",
4+
"message": "`<svelte:window>` does not support non-event attributes or spread attributes",
5+
"start": {
6+
"line": 4,
7+
"column": 15
8+
},
9+
"end": {
10+
"line": 4,
11+
"column": 21
12+
}
13+
}
14+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
let a = {};
3+
</script>
4+
<svelte:window {...a}></svelte:window>

0 commit comments

Comments
 (0)