Skip to content

Commit 8fe2f0d

Browse files
committed
ignore control characters following a svelte-ignore comment
1 parent c8a828d commit 8fe2f0d

File tree

2 files changed

+30
-5
lines changed
  • packages/svelte
    • src/compiler/phases/2-analyze/visitors
    • tests/validator/samples/bidirectional-control-characters

2 files changed

+30
-5
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import { is_tag_valid_with_parent } from '../../../../html-tree-validation.js';
44
import { regex_bidirectional_control_characters, regex_not_whitespace } from '../../patterns.js';
55
import * as e from '../../../errors.js';
66
import * as w from '../../../warnings.js';
7+
import { extract_svelte_ignore } from '../../../utils/extract_svelte_ignore.js';
78

89
/**
910
* @param {AST.Text} node
1011
* @param {Context} context
1112
*/
1213
export function Text(node, context) {
13-
const in_template = context.path.at(-1)?.type === 'Fragment';
14+
const parent = /** @type {AST.SvelteNode} */ (context.path.at(-1));
1415

15-
if (in_template && context.state.parent_element && regex_not_whitespace.test(node.data)) {
16+
if (
17+
parent.type === 'Fragment' &&
18+
context.state.parent_element &&
19+
regex_not_whitespace.test(node.data)
20+
) {
1621
const message = is_tag_valid_with_parent('#text', context.state.parent_element);
1722
if (message) {
1823
e.node_invalid_placement(node, message);
@@ -21,7 +26,27 @@ export function Text(node, context) {
2126

2227
regex_bidirectional_control_characters.lastIndex = 0;
2328
for (const match of node.data.matchAll(regex_bidirectional_control_characters)) {
24-
let start = match.index + node.start;
25-
w.bidirectional_control_characters({ start, end: start + match[0].length });
29+
let is_ignored = false;
30+
31+
// if we have a svelte-ignore comment earlier in the text, bail
32+
// (otherwise we can only use svelte-ignore on parent elements/blocks)
33+
if (parent.type === 'Fragment') {
34+
for (const child of parent.nodes) {
35+
if (child === node) break;
36+
37+
if (child.type === 'Comment') {
38+
is_ignored ||= extract_svelte_ignore(
39+
child.start + 4,
40+
child.data,
41+
context.state.analysis.runes
42+
).includes('bidirectional_control_characters');
43+
}
44+
}
45+
}
46+
47+
if (!is_ignored) {
48+
let start = match.index + node.start;
49+
w.bidirectional_control_characters({ start, end: start + match[0].length });
50+
}
2651
}
2752
}

packages/svelte/tests/validator/samples/bidirectional-control-characters/input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
<h1>Hello, {name}!</h1>
66

77
<!-- svelte-ignore bidirectional_control_characters -->
8-
<div>⁧⁦def⁩⁦abc⁩⁩</div>
8+
⁧⁦def⁩⁦abc⁩⁩

0 commit comments

Comments
 (0)