Skip to content

fix: create <svelte:element> instances with the correct namespace #9647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/read/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { namespace_svg } from '../../../../constants.js';
import { error } from '../../../errors.js';

const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
Expand Down Expand Up @@ -156,7 +157,7 @@ export default function read_options(node) {
error(attribute, 'invalid-svelte-option-namespace');
}

if (value === 'http://www.w3.org/2000/svg') {
if (value === namespace_svg) {
component_options.namespace = 'svg';
} else if (value === 'html' || value === 'svg' || value === 'foreign') {
component_options.namespace = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2102,9 +2102,7 @@ export const template_visitors = {
context.state.node,
get_tag,
b.arrow([element_id, b.id('$$anchor')], b.block(inner)),
namespace === 'http://www.w3.org/2000/svg'
? b.literal(true)
: /** @type {any} */ (undefined)
namespace ? b.literal(namespace) : /** @type {any} */ (undefined)
)
)
);
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ export const DOMBooleanAttributes = [
'seamless',
'selected'
];

export const namespace_svg = 'http://www.w3.org/2000/svg';
export const namespace_html = 'http://www.w3.org/1999/xhtml';
17 changes: 10 additions & 7 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import {
EACH_ITEM_REACTIVE,
PassiveDelegatedEvents,
DelegatedEvents,
AttributeAliases
AttributeAliases,
namespace_svg,
namespace_html
} from '../../constants.js';
import {
create_fragment_from_html,
Expand Down Expand Up @@ -1543,10 +1545,10 @@ function swap_block_dom(block, from, to) {
* @param {Comment} anchor_node
* @param {() => string} tag_fn
* @param {null | ((element: Element, anchor: Node) => void)} render_fn
* @param {any} is_svg
* @param {string} namespace
* @returns {void}
*/
export function element(anchor_node, tag_fn, render_fn, is_svg = false) {
export function element(anchor_node, tag_fn, render_fn, namespace) {
const block = create_dynamic_element_block();
hydrate_block_anchor(anchor_node);
let has_mounted = false;
Expand All @@ -1570,11 +1572,12 @@ export function element(anchor_node, tag_fn, render_fn, is_svg = false) {
// Managed effect
const render_effect_signal = render_effect(
() => {
const ns = namespace ?? tag === 'svg' ? namespace_svg : null;
const next_element = tag
? current_hydration_fragment !== null
? /** @type {HTMLElement | SVGElement} */ (current_hydration_fragment[0])
: is_svg
? document.createElementNS('http://www.w3.org/2000/svg', tag)
: ns
? document.createElementNS(ns, tag)
: document.createElement(tag)
: null;
const prev_element = element;
Expand Down Expand Up @@ -2327,7 +2330,7 @@ export function cssProps(anchor, is_html, props, component) {
tag = document.createElement('div');
tag.style.display = 'contents';
} else {
tag = document.createElementNS('http://www.w3.org/2000/svg', 'g');
tag = document.createElementNS(namespace_svg, 'g');
}
insert(tag, null, anchor);
component_anchor = empty();
Expand Down Expand Up @@ -2821,7 +2824,7 @@ export function spread_dynamic_element_attributes(node, prev, attrs, css_hash) {
/** @type {Element & ElementCSSInlineStyle} */ (node),
prev,
attrs,
node.namespaceURI !== 'http://www.w3.org/2000/svg',
node.namespaceURI !== namespace_svg,
css_hash
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from '../../test';

export default test({
html: '<svg><path></path></svg>',

test({ assert, target }) {
const svg = target.querySelector('svg');
const rect = target.querySelector('path');
assert.equal(svg?.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(rect?.namespaceURI, 'http://www.w3.org/2000/svg');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
// ensure these are treated as dynamic, despite whatever
// optimisations we might apply
export let svg = 'svg';
export let path = 'path';
</script>

<svelte:element this={svg}>
<svelte:element this={path}></svelte:element>
</svelte:element>