Skip to content

Commit 0332abb

Browse files
authored
chore: improve ssr parent validation (#13158)
1 parent c372dd8 commit 0332abb

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

.changeset/strange-trains-destroy.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+
chore: improve ssr parent validation

packages/svelte/src/html-tree-validation.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,23 @@ export function is_tag_valid_with_ancestor(tag, ancestors) {
162162
* Returns false if the tag is not allowed inside the parent tag such that it will result
163163
* in the browser repairing the HTML, which will likely result in an error during hydration.
164164
* @param {string} tag
165-
* @param {string} parent_tag
165+
* @param {string | null} parent_tag
166166
* @returns {boolean}
167167
*/
168168
export function is_tag_valid_with_parent(tag, parent_tag) {
169-
const disallowed = disallowed_children[parent_tag];
169+
if (parent_tag !== null) {
170+
const disallowed = disallowed_children[parent_tag];
170171

171-
if (disallowed) {
172-
if ('direct' in disallowed && disallowed.direct.includes(tag)) {
173-
return false;
174-
}
175-
if ('descendant' in disallowed && disallowed.descendant.includes(tag)) {
176-
return false;
177-
}
178-
if ('only' in disallowed && disallowed.only) {
179-
return disallowed.only.includes(tag);
172+
if (disallowed) {
173+
if ('direct' in disallowed && disallowed.direct.includes(tag)) {
174+
return false;
175+
}
176+
if ('descendant' in disallowed && disallowed.descendant.includes(tag)) {
177+
return false;
178+
}
179+
if ('only' in disallowed && disallowed.only) {
180+
return disallowed.only.includes(tag);
181+
}
180182
}
181183
}
182184

packages/svelte/src/internal/server/dev.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ function stringify(element) {
3434

3535
/**
3636
* @param {Payload} payload
37-
* @param {Element} parent
37+
* @param {Element | null} parent
3838
* @param {Element} child
3939
*/
4040
function print_error(payload, parent, child) {
4141
var message =
42-
`node_invalid_placement_ssr: ${stringify(parent)} cannot contain ${stringify(child)}\n\n` +
42+
(parent === null
43+
? `node_invalid_placement_ssr: ${stringify(child)} needs a valid parent element\n\n`
44+
: `node_invalid_placement_ssr: ${stringify(parent)} cannot contain ${stringify(child)}\n\n`) +
4345
'This can cause content to shift around as the browser repairs the HTML, and will likely result in a `hydration_mismatch` warning.';
4446

4547
if ((seen ??= new Set()).has(message)) return;
@@ -79,6 +81,8 @@ export function push_element(payload, tag, line, column) {
7981
}
8082
ancestor = ancestor.parent;
8183
}
84+
} else if (!is_tag_valid_with_parent(tag, null)) {
85+
print_error(payload, null, child);
8286
}
8387

8488
parent = child;

0 commit comments

Comments
 (0)