Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dull-roses-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: infer `svg` namespace correctly
7 changes: 5 additions & 2 deletions packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ export function infer_namespace(namespace, parent, nodes, path) {
parent_node.type === 'Component' ||
parent_node.type === 'SvelteComponent' ||
parent_node.type === 'SvelteFragment' ||
parent_node.type === 'SnippetBlock')
parent_node.type === 'SnippetBlock' ||
parent_node.type === 'IfBlock' ||
parent_node.type === 'EachBlock' ||
parent_node.type === 'AwaitBlock')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parent_node.type === 'SnippetBlock' ||
parent_node.type === 'IfBlock' ||
parent_node.type === 'EachBlock' ||
parent_node.type === 'AwaitBlock')
parent_node.type === 'SnippetBlock')

) {
// Heuristic: Keep current namespace, unless we find a regular element,
// in which case we always want html, or we only find svg nodes,
Expand All @@ -222,7 +225,7 @@ export function infer_namespace(namespace, parent, nodes, path) {
only_svg = false;
break;
}
} else if (node.type !== 'Text' || node.data.trim() !== '') {
} else if (node.type !== 'ConstTag' && (node.type !== 'Text' || node.data.trim() !== '')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for tackling this, but this is not quite right. We need to keep the check above as-is, and instead need to check that if we discover if/await/each blocks while traversing the nodes here, that the top level children of those are all svg elements (basically this loop needs to become recursive). Also, could you add a test case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I realized that is the correct approach while trying to fix it, that's why I kept it a draft. I will add a test case shortly and maybe implement this.

only_svg = false;
}
}
Expand Down