Skip to content

Commit 2e0dcd7

Browse files
trueadmRich-Harris
andauthored
fix: ensure if block paths retain correct template namespacing (#14685)
* fix: ensure if block paths retain correct template namespacing * add tests * address feedback * address feedback * simplify --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 780041a commit 2e0dcd7

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

.changeset/giant-moons-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure if block paths retain correct template namespacing

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,24 @@ export function infer_namespace(namespace, parent, nodes) {
347347
}
348348
}
349349

350-
return namespace;
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 node of nodes) {
356+
if (node.type !== 'RegularElement') continue;
357+
358+
if (node.metadata.mathml) {
359+
new_namespace = new_namespace === null || new_namespace === 'mathml' ? 'mathml' : 'html';
360+
} else if (node.metadata.svg) {
361+
new_namespace = new_namespace === null || new_namespace === 'svg' ? 'svg' : 'html';
362+
} else {
363+
return 'html';
364+
}
365+
}
366+
367+
return new_namespace ?? namespace;
351368
}
352369

353370
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
{#if true}
3+
<g>
4+
<rect x="20" y="10" width="50" height="50" fill="yellow"/>
5+
</g>
6+
{:else}
7+
<div>lol</div>
8+
{/if}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test, ok } from '../../test';
2+
3+
export default test({
4+
html: `<svg height="200px" viewBox="0 0 100 100" width="200px"><g><rect fill="yellow" height="50" width="50" x="20" y="10"></rect></g></svg>`,
5+
test({ assert, target }) {
6+
const g = target.querySelector('g');
7+
const rect = target.querySelector('rect');
8+
ok(g);
9+
ok(rect);
10+
11+
assert.equal(g.namespaceURI, 'http://www.w3.org/2000/svg');
12+
assert.equal(rect.namespaceURI, 'http://www.w3.org/2000/svg');
13+
}
14+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Child from "./Child.svelte";
3+
</script>
4+
5+
<svg viewBox="0 0 100 100" width="200px" height="200px">
6+
<Child />
7+
</svg>

0 commit comments

Comments
 (0)