Skip to content

Commit c452850

Browse files
ryansolidcursoragentClaude via Cursor
authored
fix(web): dynamic() honors xmlns when creating a tag-name element (#3386) (#3436)
* fix(web): dynamic() honors xmlns when creating a tag-name element (#3386) The compiler resolves a tag's namespace from its parent at build time; the dynamic() runtime path creates the element before it has a parent, so a tag that exists in both HTML and SVG (a, script, style, title) was always an HTML element — `<svg><Link href/></svg>` with `Link = dynamic(() => "a")` produced an HTML anchor inside the SVG tree (#3386 part 2). The instance now says which namespace it means with the attribute compiled JSX already uses for exactly this (`<a xmlns="http://www.w3.org/2000/svg">`). Like `is`, `xmlns` is an attribute of the element that also decides how the node is created: read once, untracked, at creation — the DOM can't re-namespace a node — then applied through spread() as an ordinary attribute, so a client-rendered element carries the same attribute the server serializes and the parser keeps. Server: nothing to do; ssrElement already emits it, and hydration claims the parser-namespaced node. Without xmlns the namespace is still inferred from the tag. Types: dynamic() returns Component<DynamicComponentProps<T>>, adding `xmlns?: string` for tag-name targets only (the ambiguous tags' intrinsic attribute types are the HTML ones, which lack it). Tests: namespaceURI with/without xmlns inside <svg> and <math>, inference still wins for unambiguous tags, xmlns overrides inference both ways, <Dynamic> parity, read-once (later values update the attribute, never the node); server output byte-equal to compiled `<a xmlns>`; parity-harness scenario hydrating a dynamic() <a xmlns> inside <svg>. Also pins ref arrays through spread into a dynamic() tag (the shape Kobalte's solid2 branch works around at rc.3) as working on next. Co-authored-by: Cursor <[email protected]> * fix(web): accept xmlns on every element's attribute type CI test-types rejected the compiled-JSX comparison `<a xmlns={SVG} href>`: `AnchorHTMLAttributes` had no `xmlns` — the compiler has honored `<a xmlns=…>` on any tag all along (it's how it disambiguates a/script/style/title), so the attribute belongs on the shared `ElementAttributes`, not only SVG/MathML. Source of truth is jsx-h.d.ts; jsx.d.ts and @solidjs/h's copy are regenerated. With every intrinsic's attribute type carrying `xmlns`, dynamic()'s tag-name components inherit it and the conditional `DynamicComponentProps` / `DynamicElementProps` types from the previous commit are unnecessary — drop them; dynamic() returns Component<ComponentProps<T>> as before. Co-authored-by: Cursor <[email protected]> * fix(web): scope xmlns type to the HTML/SVG-ambiguous tags Narrow the previous commit: instead of accepting xmlns on every element via ElementAttributes, keep it on SVGAttributes/MathMLAttributes and add it only to the four tags that exist in both HTML and SVG (a, script, style, title). Those are the only tags where the attribute changes what gets created, both in the compiler and in dynamic(). Introduces TitleHTMLAttributes so title has a home for it. Regenerates the web and h JSX types. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]> Co-authored-by: Claude via Cursor <[email protected]>
1 parent efcaeba commit c452850

9 files changed

Lines changed: 338 additions & 19 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@solidjs/web": patch
3+
"@solidjs/h": patch
4+
---
5+
6+
`dynamic()` / `Dynamic` with a tag-name source honor an `xmlns` prop when creating the element (#3386)
7+
8+
The compiler resolves a tag's namespace from its parent at build time; the `dynamic()` runtime path creates the element before it has a parent, so a tag that exists in both HTML and SVG (`a`, `script`, `style`, `title`) was always created as an HTML element — `<svg><Link href=…/></svg>` with `Link = dynamic(() => "a")` produced an HTML anchor inside the SVG tree. The instance can now say which one it means with the same attribute compiled JSX uses for the same purpose: `<Link xmlns="http://www.w3.org/2000/svg" href=…/>`. Like `is`, `xmlns` is read once, untracked, at creation (the DOM can't re-namespace a node) and then applied as an ordinary attribute, so a client-rendered element carries the same attribute the server serializes. Without `xmlns` the namespace is still inferred from the tag name. Hydration is unaffected: it claims the parser-namespaced node.
9+
10+
Types: `xmlns` is now accepted on the four tags that exist in both HTML and SVG (`a`, `script`, `style`, `title`) in addition to the SVG/MathML attribute sets that already had it — those are the only tags where the attribute changes what gets created, and the compiler has honored `<a xmlns=…>` on them all along. Unambiguous HTML tags (`div`, `span`, …) still reject it. `dynamic()`'s tag-name components inherit it through their intrinsic attribute types. Also syncs `@solidjs/h`'s generated JSX types.

‎packages/h/jsx-runtime/src/jsx.d.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,17 @@ export namespace JSX {
10951095
type HTMLPreloadAs = "fetch" | "font" | "image" | "script" | "style" | "track";
10961096
type HTMLLinkAs = HTMLPreloadAs | "audio" | "document" | "embed" | "object" | "video" | "worker";
10971097

1098-
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T> {
1098+
/**
1099+
* `xmlns` on the tags that exist in both HTML and SVG (`a`, `script`, `style`,
1100+
* `title`). The compiler reads it to pick the namespace when the tag is not
1101+
* nested under `<svg>` at compile time (e.g. an XML partial), and `dynamic()`
1102+
* honors it the same way when creating a tag-name element at runtime.
1103+
*/
1104+
interface AmbiguousNamespaceAttributes {
1105+
xmlns?: FunctionMaybe<string | RemoveAttribute>;
1106+
}
1107+
1108+
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
10991109
download?: FunctionMaybe<string | EnumeratedAcceptsEmpty | RemoveAttribute>;
11001110
href?: FunctionMaybe<string | SerializableAttributeValue | RemoveAttribute>;
11011111
hreflang?: FunctionMaybe<string | RemoveAttribute>;
@@ -1700,7 +1710,7 @@ export namespace JSX {
17001710
max?: FunctionMaybe<number | string | RemoveAttribute>;
17011711
value?: FunctionMaybe<string | string[] | number | RemoveAttribute>;
17021712
}
1703-
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T> {
1713+
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17041714
async?: FunctionMaybe<BooleanAttribute | RemoveAttribute>;
17051715
blocking?: FunctionMaybe<"render" | RemoveAttribute>;
17061716
crossorigin?: FunctionMaybe<HTMLCrossorigin | RemoveAttribute>;
@@ -1764,7 +1774,7 @@ export namespace JSX {
17641774
type?: FunctionMaybe<string | RemoveAttribute>;
17651775
width?: FunctionMaybe<number | string | RemoveAttribute>;
17661776
}
1767-
interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
1777+
interface StyleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17681778
blocking?: FunctionMaybe<"render" | RemoveAttribute>;
17691779
media?: FunctionMaybe<string | RemoveAttribute>;
17701780

@@ -1865,6 +1875,7 @@ export namespace JSX {
18651875
interface TimeHTMLAttributes<T> extends HTMLAttributes<T> {
18661876
datetime?: FunctionMaybe<string | RemoveAttribute>;
18671877
}
1878+
interface TitleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {}
18681879
interface TrackHTMLAttributes<T> extends HTMLAttributes<T> {
18691880
default?: FunctionMaybe<BooleanAttribute | RemoveAttribute>;
18701881
kind?: FunctionMaybe<
@@ -3600,7 +3611,7 @@ export namespace JSX {
36003611
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
36013612
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement
36023613
*/
3603-
title: HTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
3614+
title: TitleHTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
36043615
/**
36053616
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr
36063617
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement

‎packages/web/jsx/jsx-h.d.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,17 @@ export namespace JSX {
10961096
type HTMLPreloadAs = "fetch" | "font" | "image" | "script" | "style" | "track";
10971097
type HTMLLinkAs = HTMLPreloadAs | "audio" | "document" | "embed" | "object" | "video" | "worker";
10981098

1099-
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T> {
1099+
/**
1100+
* `xmlns` on the tags that exist in both HTML and SVG (`a`, `script`, `style`,
1101+
* `title`). The compiler reads it to pick the namespace when the tag is not
1102+
* nested under `<svg>` at compile time (e.g. an XML partial), and `dynamic()`
1103+
* honors it the same way when creating a tag-name element at runtime.
1104+
*/
1105+
interface AmbiguousNamespaceAttributes {
1106+
xmlns?: FunctionMaybe<string | RemoveAttribute>;
1107+
}
1108+
1109+
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
11001110
download?: FunctionMaybe<string | EnumeratedAcceptsEmpty | RemoveAttribute>;
11011111
href?: FunctionMaybe<string | SerializableAttributeValue | RemoveAttribute>;
11021112
hreflang?: FunctionMaybe<string | RemoveAttribute>;
@@ -1701,7 +1711,7 @@ export namespace JSX {
17011711
max?: FunctionMaybe<number | string | RemoveAttribute>;
17021712
value?: FunctionMaybe<string | string[] | number | RemoveAttribute>;
17031713
}
1704-
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T> {
1714+
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17051715
async?: FunctionMaybe<BooleanAttribute | RemoveAttribute>;
17061716
blocking?: FunctionMaybe<"render" | RemoveAttribute>;
17071717
crossorigin?: FunctionMaybe<HTMLCrossorigin | RemoveAttribute>;
@@ -1765,7 +1775,7 @@ export namespace JSX {
17651775
type?: FunctionMaybe<string | RemoveAttribute>;
17661776
width?: FunctionMaybe<number | string | RemoveAttribute>;
17671777
}
1768-
interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
1778+
interface StyleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17691779
blocking?: FunctionMaybe<"render" | RemoveAttribute>;
17701780
media?: FunctionMaybe<string | RemoveAttribute>;
17711781

@@ -1866,6 +1876,7 @@ export namespace JSX {
18661876
interface TimeHTMLAttributes<T> extends HTMLAttributes<T> {
18671877
datetime?: FunctionMaybe<string | RemoveAttribute>;
18681878
}
1879+
interface TitleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {}
18691880
interface TrackHTMLAttributes<T> extends HTMLAttributes<T> {
18701881
default?: FunctionMaybe<BooleanAttribute | RemoveAttribute>;
18711882
kind?: FunctionMaybe<
@@ -3601,7 +3612,7 @@ export namespace JSX {
36013612
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
36023613
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement
36033614
*/
3604-
title: HTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
3615+
title: TitleHTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
36053616
/**
36063617
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr
36073618
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement

‎packages/web/jsx/jsx.d.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,17 @@ export namespace JSX {
11091109
type HTMLPreloadAs = "fetch" | "font" | "image" | "script" | "style" | "track";
11101110
type HTMLLinkAs = HTMLPreloadAs | "audio" | "document" | "embed" | "object" | "video" | "worker";
11111111

1112-
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T> {
1112+
/**
1113+
* `xmlns` on the tags that exist in both HTML and SVG (`a`, `script`, `style`,
1114+
* `title`). The compiler reads it to pick the namespace when the tag is not
1115+
* nested under `<svg>` at compile time (e.g. an XML partial), and `dynamic()`
1116+
* honors it the same way when creating a tag-name element at runtime.
1117+
*/
1118+
interface AmbiguousNamespaceAttributes {
1119+
xmlns?: string | RemoveAttribute;
1120+
}
1121+
1122+
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
11131123
download?: string | EnumeratedAcceptsEmpty | RemoveAttribute;
11141124
href?: string | SerializableAttributeValue | RemoveAttribute;
11151125
hreflang?: string | RemoveAttribute;
@@ -1698,7 +1708,7 @@ export namespace JSX {
16981708
max?: number | string | RemoveAttribute;
16991709
value?: string | string[] | number | RemoveAttribute;
17001710
}
1701-
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T> {
1711+
interface ScriptHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17021712
async?: BooleanAttribute | RemoveAttribute;
17031713
blocking?: "render" | RemoveAttribute;
17041714
crossorigin?: HTMLCrossorigin | RemoveAttribute;
@@ -1760,7 +1770,7 @@ export namespace JSX {
17601770
type?: string | RemoveAttribute;
17611771
width?: number | string | RemoveAttribute;
17621772
}
1763-
interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
1773+
interface StyleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {
17641774
blocking?: "render" | RemoveAttribute;
17651775
media?: string | RemoveAttribute;
17661776

@@ -1861,6 +1871,7 @@ export namespace JSX {
18611871
interface TimeHTMLAttributes<T> extends HTMLAttributes<T> {
18621872
datetime?: string | RemoveAttribute;
18631873
}
1874+
interface TitleHTMLAttributes<T> extends HTMLAttributes<T>, AmbiguousNamespaceAttributes {}
18641875
interface TrackHTMLAttributes<T> extends HTMLAttributes<T> {
18651876
default?: BooleanAttribute | RemoveAttribute;
18661877
kind?:
@@ -3589,7 +3600,7 @@ export namespace JSX {
35893600
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
35903601
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement
35913602
*/
3592-
title: HTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
3603+
title: TitleHTMLAttributes<HTMLTitleElement> & Properties<HTMLTitleElement>;
35933604
/**
35943605
* @url https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr
35953606
* @url https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement

‎packages/web/src/index.ts‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,23 @@ function portalImpl(props: { mount?: Element; children: JSX.Element }): JSX.Elem
243243
* `{ deferStream: true }` to hold the document's first flush until it settles
244244
* (the same option `createMemo` takes); the client ignores it.
245245
*
246+
* For a tag-name source the element's namespace is inferred from the tag. A
247+
* tag that exists in both HTML and SVG (`a`, `script`, `style`, `title`) is
248+
* created as HTML unless the instance passes `xmlns` — the same attribute
249+
* compiled JSX uses for the same purpose. `is` (customized built-ins) and
250+
* `xmlns` are read once at creation and then applied as ordinary attributes.
251+
*
246252
* @example
247253
* ```tsx
248254
* // `source` can return either a custom Component or a native tag
249255
* // name — they're interchangeable, and the returned reference is a
250256
* // stable Component you can use anywhere a normal one would go.
251257
* const Field = dynamic(() => multiline() ? RichTextEditor : "input");
252258
* return <Field value={value()} onInput={onInput} />;
259+
*
260+
* // An ambiguous tag inside an SVG tree: say which namespace you mean.
261+
* const Link = dynamic(() => "a");
262+
* <svg><Link xmlns="http://www.w3.org/2000/svg" href="/x">…</Link></svg>
253263
* ```
254264
*
255265
* @description https://docs.solidjs.com/reference/components/dynamic
@@ -366,11 +376,17 @@ export function dynamic<T extends ValidComponent>(
366376

367377
case "string": {
368378
const hydrating = sharedConfig.hydrating;
379+
// `is` and `xmlns` are attributes of the element that also decide
380+
// how it is CREATED (customized built-in / namespace), so they are
381+
// read once here, untracked — the DOM can't change either after
382+
// creation — and then flow through spread() like any attribute.
383+
// Hydration claims the parser-namespaced node, so neither applies.
369384
const el = hydrating
370385
? getNextElement()
371386
: createElement(
372387
component as string,
373-
untrack(() => (props as any).is)
388+
untrack(() => (props as any).is),
389+
untrack(() => (props as any).xmlns)
374390
);
375391
spread(el, props);
376392
// Compiled JSX emits runHydrationEvents() after an element that
@@ -409,13 +425,24 @@ export function Dynamic<T extends ValidComponent>(props: DynamicProps<T>): JSX.E
409425
return createComponent(Comp, omit(props, "component") as ComponentProps<T>);
410426
}
411427

412-
function createElement(tagName: string, is = undefined): HTMLElement | SVGElement | MathMLElement {
428+
// Namespace comes from an explicit `xmlns` first, then from the tag name. The
429+
// compiler resolves a tag's namespace from its PARENT at build time; this
430+
// runtime path has no parent yet, so a tag that exists in both HTML and SVG
431+
// (`a`, `script`, `style`, `title`) is HTML unless `xmlns` says otherwise —
432+
// the same attribute compiled JSX uses for the same purpose (`<a xmlns=…>`).
433+
function createElement(
434+
tagName: string,
435+
is: string | undefined = undefined,
436+
xmlns: string | undefined = undefined
437+
): HTMLElement | SVGElement | MathMLElement {
413438
return (
414-
SVGElements.has(tagName)
415-
? document.createElementNS(Namespaces.svg, tagName)
416-
: MathMLElements.has(tagName)
417-
? document.createElementNS(Namespaces.mathml, tagName)
418-
: document.createElement(tagName, { is })
439+
xmlns
440+
? document.createElementNS(xmlns, tagName, { is })
441+
: SVGElements.has(tagName)
442+
? document.createElementNS(Namespaces.svg, tagName)
443+
: MathMLElements.has(tagName)
444+
? document.createElementNS(Namespaces.mathml, tagName)
445+
: document.createElement(tagName, { is })
419446
) as HTMLElement | SVGElement | MathMLElement;
420447
}
421448

0 commit comments

Comments
 (0)