diff --git a/.changeset/giant-moons-accept.md b/.changeset/giant-moons-accept.md
new file mode 100644
index 0000000000..7940371d6f
--- /dev/null
+++ b/.changeset/giant-moons-accept.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: ensure if block paths retain correct template namespacing
diff --git a/packages/svelte/src/compiler/phases/3-transform/utils.js b/packages/svelte/src/compiler/phases/3-transform/utils.js
index 14fd3aa2e8..ffd07dd26a 100644
--- a/packages/svelte/src/compiler/phases/3-transform/utils.js
+++ b/packages/svelte/src/compiler/phases/3-transform/utils.js
@@ -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;
}
/**
diff --git a/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block/Child.svelte b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block/Child.svelte
new file mode 100644
index 0000000000..53e6203dde
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/svg-namespace-if-block/Child.svelte
@@ -0,0 +1,8 @@
+
+{#if true}
+