From cd1adbc4e6f4305b4a7f19763299544f480484b2 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 6 Jan 2025 23:08:08 +0000 Subject: [PATCH] fix: ensure logic blocks keep consistent namespacing (#14817) * fix: ensure logic blocks keep consitent namespacing * lint * add test * handle `` ambiguity the same as `<a>` * update changeset --------- Co-authored-by: Rich Harris <rich.harris@vercel.com> --- .changeset/real-onions-confess.md | 5 +++++ .../phases/2-analyze/visitors/RegularElement.js | 7 +++++-- .../samples/svg-namespace-if-block-3/_config.js | 11 +++++++++++ .../samples/svg-namespace-if-block-3/main.svelte | 5 +++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 .changeset/real-onions-confess.md create mode 100644 packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/main.svelte diff --git a/.changeset/real-onions-confess.md b/.changeset/real-onions-confess.md new file mode 100644 index 0000000000..5f11b225ad --- /dev/null +++ b/.changeset/real-onions-confess.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: inherit correct namespace for `<title>` elements diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js index 9a891c0c49..03dfaebcb7 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js @@ -90,8 +90,11 @@ export function RegularElement(node, context) { if (is_svg(node.name)) { return true; } - if (node.name === 'a') { - for (let i = context.path.length - 1; i >= 0; i--) { + + if (node.name === 'a' || node.name === 'title') { + let i = context.path.length; + + while (i--) { const ancestor = context.path[i]; if (ancestor.type === 'RegularElement') { return ancestor.metadata.svg; diff --git a/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/_config.js b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/_config.js new file mode 100644 index 0000000000..7543278b62 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/_config.js @@ -0,0 +1,11 @@ +import { test, ok } from '../../test'; + +export default test({ + html: `<svg><title>potato`, + test({ assert, target }) { + const title = target.querySelector('title'); + ok(title); + + assert.equal(title.namespaceURI, 'http://www.w3.org/2000/svg'); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/main.svelte b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/main.svelte new file mode 100644 index 0000000000..8be23f8b23 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block-3/main.svelte @@ -0,0 +1,5 @@ + + {#if true} + potato + {/if} +