Skip to content

Commit d62e7bd

Browse files
authored
fix: ignore text and expressions outside the template when validating HTML (#14468)
fixes #14466 The logic introduced in #14395 was flawed - not every text or expression outside the template is the child of an attribute. This turns it around: We know that every child of a fragment is inside the template, so we ignore all text/expression tags that are not child of a fragment
1 parent c4ac0e0 commit d62e7bd

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

.changeset/brave-keys-rescue.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: ignore text and expressions outside the template when validating HTML

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { mark_subtree_dynamic } from './shared/fragment.js';
99
* @param {Context} context
1010
*/
1111
export function ExpressionTag(node, context) {
12-
const in_attribute = context.path.at(-1)?.type === 'Attribute';
12+
const in_template = context.path.at(-1)?.type === 'Fragment';
1313

14-
if (!in_attribute && context.state.parent_element) {
14+
if (in_template && context.state.parent_element) {
1515
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
1616
e.node_invalid_placement(node, '`{expression}`', context.state.parent_element);
1717
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import * as e from '../../../errors.js';
99
* @param {Context} context
1010
*/
1111
export function Text(node, context) {
12-
const in_attribute = context.path.at(-1)?.type === 'Attribute';
12+
const in_template = context.path.at(-1)?.type === 'Fragment';
1313

14-
if (!in_attribute && context.state.parent_element && regex_not_whitespace.test(node.data)) {
14+
if (in_template && context.state.parent_element && regex_not_whitespace.test(node.data)) {
1515
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
1616
e.node_invalid_placement(node, 'Text node', context.state.parent_element);
1717
}
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": "Text node is invalid inside `<tbody>`",
5+
"start": {
6+
"line": 3,
7+
"column": 8
8+
},
9+
"end": {
10+
"line": 3,
11+
"column": 13
12+
}
13+
}
14+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
<table attr={value} style:x={y} class="I'm not {counted} as text in the table">
3+
<tbody>I am {bad}</tbody>
4+
</table>

0 commit comments

Comments
 (0)