Skip to content

Commit

Permalink
fix: ignore text and expressions outside the template when validating…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
dummdidumm authored Nov 29, 2024
1 parent c4ac0e0 commit d62e7bd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-keys-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ignore text and expressions outside the template when validating HTML
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { mark_subtree_dynamic } from './shared/fragment.js';
* @param {Context} 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)) {
e.node_invalid_placement(node, '`{expression}`', context.state.parent_element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import * as e from '../../../errors.js';
* @param {Context} 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)) {
e.node_invalid_placement(node, 'Text node', context.state.parent_element);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
]
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit d62e7bd

Please sign in to comment.