Skip to content

Commit cf6b64c

Browse files
authored
fix: error on incorrect attributes for svelte:body (#13084)
* fix: error on spreading attributes for svelte:body * tweak
1 parent 2af9b19 commit cf6b64c

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

.changeset/eleven-icons-sniff.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: error on incorrect attributes for svelte:body

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ HTML restricts where certain elements can appear. In case of a violation the bro
274274

275275
> A component can have a single top-level `<style>` element
276276
277+
## svelte_body_illegal_attribute
278+
279+
> `<svelte:body>` does not support non-event attributes or spread attributes
280+
277281
## svelte_component_invalid_this
278282

279283
> Invalid component definition — must be an `{expression}`

packages/svelte/src/compiler/errors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,15 @@ export function style_duplicate(node) {
11831183
e(node, "style_duplicate", "A component can have a single top-level `<style>` element");
11841184
}
11851185

1186+
/**
1187+
* `<svelte:body>` does not support non-event attributes or spread attributes
1188+
* @param {null | number | NodeLike} node
1189+
* @returns {never}
1190+
*/
1191+
export function svelte_body_illegal_attribute(node) {
1192+
e(node, "svelte_body_illegal_attribute", "`<svelte:body>` does not support non-event attributes or spread attributes");
1193+
}
1194+
11861195
/**
11871196
* Invalid component definition — must be an `{expression}`
11881197
* @param {null | number | NodeLike} node

packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteBody.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/** @import { SvelteBody } from '#compiler' */
22
/** @import { Context } from '../types' */
3+
import * as e from '../../../errors.js';
4+
import { is_event_attribute } from '../../../utils/ast.js';
35
import { disallow_children } from './shared/special-element.js';
46

57
/**
@@ -8,5 +10,13 @@ import { disallow_children } from './shared/special-element.js';
810
*/
911
export function SvelteBody(node, context) {
1012
disallow_children(node);
13+
for (const attribute of node.attributes) {
14+
if (
15+
attribute.type === 'SpreadAttribute' ||
16+
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
17+
) {
18+
e.svelte_body_illegal_attribute(attribute);
19+
}
20+
}
1121
context.next();
1222
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/special_element.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @import { Expression } from 'estree' */
22
/** @import { SvelteBody, SvelteDocument, SvelteWindow } from '#compiler' */
33
/** @import { ComponentContext } from '../../types' */
4-
import { is_event_attribute } from '../../../../../utils/ast.js';
54
import * as b from '../../../../../utils/builders.js';
65

76
/**

0 commit comments

Comments
 (0)