Skip to content

Commit 4ec9986

Browse files
authored
fix: check options namespace for top level svelte:elements (#14101)
We were checking it for nested elements, but not root elements fixes #13875
1 parent 96c299a commit 4ec9986

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

.changeset/polite-timers-tell.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: check options namespace for top level `svelte:element`s

packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteElement.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export function SvelteElement(node, context) {
3535
ancestor.type === 'Component' ||
3636
ancestor.type === 'SvelteComponent' ||
3737
ancestor.type === 'SvelteFragment' ||
38-
ancestor.type === 'SnippetBlock'
38+
ancestor.type === 'SnippetBlock' ||
39+
i === 0
3940
) {
40-
// Inside a slot or a snippet -> this resets the namespace, so assume the component namespace
41+
// Root element, or inside a slot or a snippet -> this resets the namespace, so assume the component namespace
4142
node.metadata.svg = context.state.options.namespace === 'svg';
4243
node.metadata.mathml = context.state.options.namespace === 'mathml';
4344
break;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: '<svg><rect fill="black" width="10" height="90"></rect></svg>',
5+
6+
test({ assert, target }) {
7+
const svg = target.querySelector('svg');
8+
const rect = target.querySelector('rect');
9+
assert.equal(svg?.namespaceURI, 'http://www.w3.org/2000/svg');
10+
assert.equal(rect?.namespaceURI, 'http://www.w3.org/2000/svg');
11+
}
12+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Rect from './rect.svelte';
3+
</script>
4+
5+
<svg>
6+
<Rect />
7+
</svg>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<svelte:options namespace="svg" />
2+
3+
<script>
4+
const tag = 'rect';
5+
</script>
6+
7+
<svelte:element this={tag} fill="black" width="10" height="90" />

0 commit comments

Comments
 (0)