fix: disallow invalid attributes for `<svelte:window>` and `<svelte:document>` (#14228)

pull/13438/merge
Dominic Gannaway 20 hours ago committed by GitHub
parent 312dd51acc
commit 7fd3774015
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: disallow invalid attributes for `<svelte:window>` and `<svelte:document>`

@ -412,6 +412,12 @@ Expected whitespace
`$host()` can only be used inside custom element component instances `$host()` can only be used inside custom element component instances
``` ```
### illegal_element_attribute
```
`<%name%>` does not support non-event attributes or spread attributes
```
### import_svelte_internal_forbidden ### import_svelte_internal_forbidden
``` ```

@ -172,6 +172,10 @@
> Expected whitespace > Expected whitespace
## illegal_element_attribute
> `<%name%>` does not support non-event attributes or spread attributes
## js_parse_error ## js_parse_error
> %message% > %message%

@ -984,6 +984,16 @@ export function expected_whitespace(node) {
e(node, "expected_whitespace", "Expected whitespace"); e(node, "expected_whitespace", "Expected whitespace");
} }
/**
* `<%name%>` does not support non-event attributes or spread attributes
* @param {null | number | NodeLike} node
* @param {string} name
* @returns {never}
*/
export function illegal_element_attribute(node, name) {
e(node, "illegal_element_attribute", `\`<${name}>\` does not support non-event attributes or spread attributes`);
}
/** /**
* %message% * %message%
* @param {null | number | NodeLike} node * @param {null | number | NodeLike} node

@ -1,6 +1,8 @@
/** @import { AST } from '#compiler' */ /** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
import { disallow_children } from './shared/special-element.js'; import { disallow_children } from './shared/special-element.js';
import * as e from '../../../errors.js';
import { is_event_attribute } from '../../../utils/ast.js';
/** /**
* @param {AST.SvelteDocument} node * @param {AST.SvelteDocument} node
@ -8,5 +10,15 @@ import { disallow_children } from './shared/special-element.js';
*/ */
export function SvelteDocument(node, context) { export function SvelteDocument(node, context) {
disallow_children(node); disallow_children(node);
for (const attribute of node.attributes) {
if (
attribute.type === 'SpreadAttribute' ||
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
) {
e.illegal_element_attribute(attribute, 'svelte:document');
}
}
context.next(); context.next();
} }

@ -1,6 +1,8 @@
/** @import { AST } from '#compiler' */ /** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
import { disallow_children } from './shared/special-element.js'; import { disallow_children } from './shared/special-element.js';
import * as e from '../../../errors.js';
import { is_event_attribute } from '../../../utils/ast.js';
/** /**
* @param {AST.SvelteWindow} node * @param {AST.SvelteWindow} node
@ -8,5 +10,15 @@ import { disallow_children } from './shared/special-element.js';
*/ */
export function SvelteWindow(node, context) { export function SvelteWindow(node, context) {
disallow_children(node); disallow_children(node);
for (const attribute of node.attributes) {
if (
attribute.type === 'SpreadAttribute' ||
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
) {
e.illegal_element_attribute(attribute, 'svelte:window');
}
}
context.next(); context.next();
} }

@ -0,0 +1,14 @@
[
{
"code": "illegal_element_attribute",
"message": "`<svelte:document>` does not support non-event attributes or spread attributes",
"start": {
"line": 4,
"column": 17
},
"end": {
"line": 4,
"column": 23
}
}
]

@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:document {...a}></svelte:document>

@ -0,0 +1,14 @@
[
{
"code": "illegal_element_attribute",
"message": "`<svelte:window>` does not support non-event attributes or spread attributes",
"start": {
"line": 4,
"column": 15
},
"end": {
"line": 4,
"column": 21
}
}
]

@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:window {...a}></svelte:window>
Loading…
Cancel
Save