Skip to content

Commit 06c9e58

Browse files
authored
fix: exclude custom elements from HTML tree validation (#13540)
fixes #13537
1 parent 6ed3ca3 commit 06c9e58

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

.changeset/chilly-glasses-retire.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: exclude custom elements from HTML tree validation

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,17 @@ const disallowed_children = {
142142
* @returns {boolean}
143143
*/
144144
export function is_tag_valid_with_ancestor(tag, ancestors) {
145+
if (tag.includes('-')) return true; // custom elements can be anything
146+
145147
const target = ancestors[ancestors.length - 1];
146148
const disallowed = disallowed_children[target];
147149
if (!disallowed) return true;
148150

149151
if ('reset_by' in disallowed && disallowed.reset_by) {
150152
for (let i = ancestors.length - 2; i >= 0; i--) {
153+
const ancestor = ancestors[i];
154+
if (ancestor.includes('-')) return true; // custom elements can be anything
155+
151156
// A reset means that forbidden descendants are allowed again
152157
if (disallowed.reset_by.includes(ancestors[i])) {
153158
return true;
@@ -166,6 +171,8 @@ export function is_tag_valid_with_ancestor(tag, ancestors) {
166171
* @returns {boolean}
167172
*/
168173
export function is_tag_valid_with_parent(tag, parent_tag) {
174+
if (tag.includes('-') || parent_tag?.includes('-')) return true; // custom elements can be anything
175+
169176
if (parent_tag !== null) {
170177
const disallowed = disallowed_children[parent_tag];
171178

packages/svelte/tests/validator/samples/invalid-node-placement-6/errors.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"code": "node_invalid_placement",
44
"message": "`<dt>` is invalid inside `<dd>`",
55
"start": {
6-
"line": 11,
6+
"line": 16,
77
"column": 3
88
},
99
"end": {
10-
"line": 11,
10+
"line": 16,
1111
"column": 19
1212
}
1313
}

packages/svelte/tests/validator/samples/invalid-node-placement-6/input.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<dt>valid</dt>
77
<dd>valid</dd>
88
</dl>
9+
<!-- custom element resets the validation -->
10+
<foo-bar>
11+
<dt>valid</dt>
12+
<dd>valid</dd>
13+
</foo-bar>
914
<!-- other tags don't -->
1015
<div>
1116
<dt>invalid</dt>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "node_invalid_placement",
4+
"message": "`<tbody>` is invalid inside `<div>`",
5+
"start": {
6+
"line": 8,
7+
"column": 1
8+
},
9+
"end": {
10+
"line": 8,
11+
"column": 16
12+
}
13+
}
14+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<some-table>
2+
<!-- No error -->
3+
<tbody></tbody>
4+
</some-table>
5+
6+
<div>
7+
<!-- Error -->
8+
<tbody></tbody>
9+
</div>

0 commit comments

Comments
 (0)