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
pull/14464/head
Simon H 4 weeks ago committed by GitHub
parent c4ac0e01e7
commit d62e7bdbe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ignore text and expressions outside the template when validating HTML

@ -9,9 +9,9 @@ import { mark_subtree_dynamic } from './shared/fragment.js';
* @param {Context} context * @param {Context} context
*/ */
export function ExpressionTag(node, context) { export function ExpressionTag(node, context) {
const in_attribute = context.path.at(-1)?.type === 'Attribute'; const in_template = context.path.at(-1)?.type === 'Fragment';
if (!in_attribute && context.state.parent_element) { if (in_template && context.state.parent_element) {
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) { if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
e.node_invalid_placement(node, '`{expression}`', context.state.parent_element); e.node_invalid_placement(node, '`{expression}`', context.state.parent_element);
} }

@ -9,9 +9,9 @@ import * as e from '../../../errors.js';
* @param {Context} context * @param {Context} context
*/ */
export function Text(node, context) { export function Text(node, context) {
const in_attribute = context.path.at(-1)?.type === 'Attribute'; const in_template = context.path.at(-1)?.type === 'Fragment';
if (!in_attribute && context.state.parent_element && regex_not_whitespace.test(node.data)) { if (in_template && context.state.parent_element && regex_not_whitespace.test(node.data)) {
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) { if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
e.node_invalid_placement(node, 'Text node', context.state.parent_element); e.node_invalid_placement(node, 'Text node', context.state.parent_element);
} }

@ -0,0 +1,14 @@
[
{
"code": "node_invalid_placement",
"message": "Text node is invalid inside `<tbody>`",
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 13
}
}
]

@ -0,0 +1,4 @@
<table attr={value} style:x={y} class="I'm not {counted} as text in the table">
<tbody>I am {bad}</tbody>
</table>
Loading…
Cancel
Save