|
5 | 5 | regex_whitespaces_strict
|
6 | 6 | } from '../patterns.js';
|
7 | 7 | import * as b from '../../utils/builders.js';
|
| 8 | +import { walk } from 'zimmerframe'; |
8 | 9 |
|
9 | 10 | /**
|
10 | 11 | * @param {string} s
|
@@ -249,51 +250,49 @@ export function infer_namespace(namespace, parent, nodes, path) {
|
249 | 250 | * @param {import('#compiler').Namespace | 'keep' | 'maybe_html'} namespace
|
250 | 251 | */
|
251 | 252 | function check_nodes_for_namespace(nodes, namespace) {
|
| 253 | + /** |
| 254 | + * @param {import('#compiler').SvelteElement | import('#compiler').RegularElement} node} |
| 255 | + * @param {{stop: () => void}} context |
| 256 | + */ |
| 257 | + const RegularElement = (node, { stop }) => { |
| 258 | + if (!node.metadata.svg) { |
| 259 | + namespace = 'html'; |
| 260 | + stop(); |
| 261 | + } else if (namespace === 'keep') { |
| 262 | + namespace = 'svg'; |
| 263 | + } |
| 264 | + }; |
| 265 | + |
252 | 266 | for (const node of nodes) {
|
253 |
| - if (node.type === 'RegularElement' || node.type === 'SvelteElement') { |
254 |
| - if (!node.metadata.svg) { |
255 |
| - namespace = 'html'; |
256 |
| - break; |
257 |
| - } else if (namespace === 'keep') { |
258 |
| - namespace = 'svg'; |
259 |
| - } |
260 |
| - } else if ( |
261 |
| - (node.type === 'Text' && node.data.trim() !== '') || |
262 |
| - node.type === 'HtmlTag' || |
263 |
| - node.type === 'RenderTag' |
264 |
| - ) { |
265 |
| - namespace = 'maybe_html'; |
266 |
| - } else if (node.type === 'EachBlock') { |
267 |
| - namespace = check_nodes_for_namespace(node.body.nodes, namespace); |
268 |
| - if (namespace === 'html') break; |
269 |
| - if (node.fallback) { |
270 |
| - namespace = check_nodes_for_namespace(node.fallback.nodes, namespace); |
271 |
| - if (namespace === 'html') break; |
272 |
| - } |
273 |
| - } else if (node.type === 'IfBlock') { |
274 |
| - namespace = check_nodes_for_namespace(node.consequent.nodes, namespace); |
275 |
| - if (namespace === 'html') break; |
276 |
| - if (node.alternate) { |
277 |
| - namespace = check_nodes_for_namespace(node.alternate.nodes, namespace); |
278 |
| - if (namespace === 'html') break; |
279 |
| - } |
280 |
| - } else if (node.type === 'AwaitBlock') { |
281 |
| - if (node.pending) { |
282 |
| - namespace = check_nodes_for_namespace(node.pending.nodes, namespace); |
283 |
| - if (namespace === 'html') break; |
| 267 | + walk( |
| 268 | + node, |
| 269 | + {}, |
| 270 | + { |
| 271 | + _(node, { next }) { |
| 272 | + if ( |
| 273 | + node.type === 'EachBlock' || |
| 274 | + node.type === 'IfBlock' || |
| 275 | + node.type === 'AwaitBlock' || |
| 276 | + node.type === 'Fragment' || |
| 277 | + node.type === 'KeyBlock' || |
| 278 | + node.type === 'RegularElement' || |
| 279 | + node.type === 'SvelteElement' || |
| 280 | + node.type === 'Text' |
| 281 | + ) { |
| 282 | + next(); |
| 283 | + } |
| 284 | + }, |
| 285 | + SvelteElement: RegularElement, |
| 286 | + RegularElement, |
| 287 | + Text(node) { |
| 288 | + if (node.data.trim() !== '') { |
| 289 | + namespace = 'maybe_html'; |
| 290 | + } |
| 291 | + } |
284 | 292 | }
|
285 |
| - if (node.then) { |
286 |
| - namespace = check_nodes_for_namespace(node.then.nodes, namespace); |
287 |
| - if (namespace === 'html') break; |
288 |
| - } |
289 |
| - if (node.catch) { |
290 |
| - namespace = check_nodes_for_namespace(node.catch.nodes, namespace); |
291 |
| - if (namespace === 'html') break; |
292 |
| - } |
293 |
| - } else if (node.type === 'KeyBlock') { |
294 |
| - namespace = check_nodes_for_namespace(node.fragment.nodes, namespace); |
295 |
| - if (namespace === 'html') break; |
296 |
| - } |
| 293 | + ); |
| 294 | + |
| 295 | + if (namespace === 'html') return namespace; |
297 | 296 | }
|
298 | 297 |
|
299 | 298 | return namespace;
|
|
0 commit comments