Skip to content

Commit 0fb2538

Browse files
committed
address feedback
1 parent 05d4a9e commit 0fb2538

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,6 @@
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '../../../../utils/builders.js';
55

6-
/**
7-
* @param {AST.Fragment} fragment
8-
* @return {Namespace}
9-
*/
10-
function get_namespace(fragment) {
11-
const elements = fragment.nodes.filter((n) => n.type === 'RegularElement');
12-
/** @type {Namespace | null} */
13-
let namespace = null;
14-
15-
// Check the elements within the fragment and look for consistent namespaces.
16-
// If we have no namespaces or they are mixed, then fallback to `html`
17-
for (const element of elements) {
18-
const metadata = element.metadata;
19-
20-
if (metadata.mathml) {
21-
if (namespace === null || namespace === 'mathml') {
22-
namespace = 'mathml';
23-
} else {
24-
namespace = 'html';
25-
}
26-
} else if (metadata.svg) {
27-
if (namespace === null || namespace === 'svg') {
28-
namespace = 'svg';
29-
} else {
30-
namespace = 'html';
31-
}
32-
} else {
33-
namespace = 'html';
34-
}
35-
}
36-
37-
return namespace ?? 'html';
38-
}
39-
406
/**
417
* @param {AST.IfBlock} node
428
* @param {ComponentContext} context
@@ -45,27 +11,15 @@ export function IfBlock(node, context) {
4511
context.state.template.push('<!>');
4612
const statements = [];
4713

48-
const consequent_namespace = get_namespace(node.consequent);
49-
const consequent = /** @type {BlockStatement} */ (
50-
context.visit(node.consequent, {
51-
...context.state,
52-
metadata: { ...context.state.metadata, namespace: consequent_namespace }
53-
})
54-
);
14+
const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
5515
const consequent_id = context.state.scope.generate('consequent');
5616

5717
statements.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
5818

5919
let alternate_id;
6020

6121
if (node.alternate) {
62-
const alternate_namespace = get_namespace(node.consequent);
63-
const alternate = /** @type {BlockStatement} */ (
64-
context.visit(node.alternate, {
65-
...context.state,
66-
metadata: { ...context.state.metadata, namespace: alternate_namespace }
67-
})
68-
);
22+
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
6923
alternate_id = context.state.scope.generate('alternate');
7024
statements.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
7125
}

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,33 @@ export function infer_namespace(namespace, parent, nodes) {
346346
return new_namespace;
347347
}
348348
}
349+
const elements = nodes.filter((n) => n.type === 'RegularElement');
350+
/** @type {Namespace | null} */
351+
let new_namespace = null;
352+
353+
// Check the elements within the fragment and look for consistent namespaces.
354+
// If we have no namespaces or they are mixed, then fallback to existing namespace
355+
for (const element of elements) {
356+
const metadata = element.metadata;
357+
358+
if (metadata.mathml) {
359+
if (new_namespace === null || new_namespace === 'mathml') {
360+
new_namespace = 'mathml';
361+
} else {
362+
new_namespace = 'html';
363+
}
364+
} else if (metadata.svg) {
365+
if (new_namespace === null || new_namespace === 'svg') {
366+
new_namespace = 'svg';
367+
} else {
368+
new_namespace = 'html';
369+
}
370+
} else {
371+
new_namespace = 'html';
372+
}
373+
}
349374

350-
return namespace;
375+
return new_namespace ?? namespace;
351376
}
352377

353378
/**

0 commit comments

Comments
 (0)