Skip to content

Commit

Permalink
fix: ensure svg namespace for <a> elements is correct (#14756)
Browse files Browse the repository at this point in the history
* fix: ensure svg namespace persists from parent of if blocks

* better fix

* better fix

* address feedback
  • Loading branch information
trueadm authored Dec 18, 2024
1 parent bfa0b34 commit 8705d44
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nine-buses-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure svg namespace for `<a>` elements is correct
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,23 @@ export function RegularElement(node, context) {
(attribute) => attribute.type === 'SpreadAttribute'
);

node.metadata.svg = is_svg(node.name);
const is_svg_element = () => {
if (is_svg(node.name)) {
return true;
}
if (node.name === 'a') {
for (let i = context.path.length - 1; i >= 0; i--) {
const ancestor = context.path[i];
if (ancestor.type === 'RegularElement') {
return ancestor.metadata.svg;
}
}
}

return false;
};

node.metadata.svg = is_svg_element();
node.metadata.mathml = is_mathml(node.name);

if (is_custom_element_node(node) && node.attributes.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test, ok } from '../../test';

export default test({
html: `<svg><a href="/docs"><text class="small" x="20" y="40"></text></a></svg>`,
test({ assert, target }) {
const a = target.querySelector('a');
ok(a);

assert.equal(a.namespaceURI, 'http://www.w3.org/2000/svg');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svg>
{#if true}
<a href="/docs">
<text x="20" y="40" class="small">{name}</text>
</a>
{/if}
</svg>

0 comments on commit 8705d44

Please sign in to comment.