|
1 | 1 | /** @import { AST } from '#compiler' */
|
2 | 2 | /** @import { Context } from '../types' */
|
3 | 3 | import { is_tag_valid_with_parent } from '../../../../html-tree-validation.js';
|
4 |
| -import { regex_not_whitespace } from '../../patterns.js'; |
| 4 | +import { regex_bidirectional_control_characters, regex_not_whitespace } from '../../patterns.js'; |
5 | 5 | import * as e from '../../../errors.js';
|
| 6 | +import * as w from '../../../warnings.js'; |
| 7 | +import { extract_svelte_ignore } from '../../../utils/extract_svelte_ignore.js'; |
6 | 8 |
|
7 | 9 | /**
|
8 | 10 | * @param {AST.Text} node
|
9 | 11 | * @param {Context} context
|
10 | 12 | */
|
11 | 13 | export function Text(node, context) {
|
12 |
| - const in_template = context.path.at(-1)?.type === 'Fragment'; |
| 14 | + const parent = /** @type {AST.SvelteNode} */ (context.path.at(-1)); |
13 | 15 |
|
14 |
| - 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 | + ) { |
15 | 21 | const message = is_tag_valid_with_parent('#text', context.state.parent_element);
|
16 | 22 | if (message) {
|
17 | 23 | e.node_invalid_placement(node, message);
|
18 | 24 | }
|
19 | 25 | }
|
| 26 | + |
| 27 | + regex_bidirectional_control_characters.lastIndex = 0; |
| 28 | + for (const match of node.data.matchAll(regex_bidirectional_control_characters)) { |
| 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 | + } |
| 51 | + } |
20 | 52 | }
|
0 commit comments