Skip to content

Commit

Permalink
fix: ensure if block paths retain correct template namespacing (#14685)
Browse files Browse the repository at this point in the history
* fix: ensure if block paths retain correct template namespacing

* add tests

* address feedback

* address feedback

* simplify

---------

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
trueadm and Rich-Harris authored Dec 12, 2024
1 parent 780041a commit 2e0dcd7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/giant-moons-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure if block paths retain correct template namespacing
19 changes: 18 additions & 1 deletion packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,24 @@ export function infer_namespace(namespace, parent, nodes) {
}
}

return namespace;
/** @type {Namespace | null} */
let new_namespace = null;

// Check the elements within the fragment and look for consistent namespaces.
// If we have no namespaces or they are mixed, then fallback to existing namespace
for (const node of nodes) {
if (node.type !== 'RegularElement') continue;

if (node.metadata.mathml) {
new_namespace = new_namespace === null || new_namespace === 'mathml' ? 'mathml' : 'html';
} else if (node.metadata.svg) {
new_namespace = new_namespace === null || new_namespace === 'svg' ? 'svg' : 'html';
} else {
return 'html';
}
}

return new_namespace ?? namespace;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{#if true}
<g>
<rect x="20" y="10" width="50" height="50" fill="yellow"/>
</g>
{:else}
<div>lol</div>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, ok } from '../../test';

export default test({
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>`,
test({ assert, target }) {
const g = target.querySelector('g');
const rect = target.querySelector('rect');
ok(g);
ok(rect);

assert.equal(g.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(rect.namespaceURI, 'http://www.w3.org/2000/svg');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Child from "./Child.svelte";
</script>

<svg viewBox="0 0 100 100" width="200px" height="200px">
<Child />
</svg>

0 comments on commit 2e0dcd7

Please sign in to comment.