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

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

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/fast-ligers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: disallow invalid attributes for `<svelte:window>` and `<svelte:document>`
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ Expected whitespace
`$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

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@

> Expected whitespace

## illegal_element_attribute

> `<%name%>` does not support non-event attributes or spread attributes

## js_parse_error

> %message%
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,16 @@ export function expected_whitespace(node) {
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%
* @param {null | number | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
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 {Context} context
*/
export function SvelteDocument(node, context) {
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();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
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 {Context} context
*/
export function SvelteWindow(node, context) {
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();
}
Original file line number Diff line number Diff line change
@@ -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
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:document {...a}></svelte:document>
Original file line number Diff line number Diff line change
@@ -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
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:window {...a}></svelte:window>
Loading