HTML children of <foreignObject> are created in the SVG namespace on the client, so they don't render #106
Send to my agentVersion: @tanstack/redact 0.2.2, renderHost() unchanged on main (47f206ea). Baseline: react / react-dom 19.3.1.
On the client, renderHost() in packages/redact/src/dom/reconcile.ts creates an element in the SVG namespace whenever its DOM parent is in the SVG namespace:
const isSvg = type === 'svg' || (domParent as Element).namespaceURI === 'http://www.w3.org/2000/svg';
<foreignObject> is itself an SVG element, so its HTML children (<div>, <span>, …) are created with createElementNS(SVG_NS, 'div'). The browser treats them as unknown SVG elements and draws nothing. React switches back to the HTML namespace inside foreignObject. The hydration path already has this case (tag !== 'foreignobject' in features/hydration/full.ts); the client render path doesn't.
We hit it on a badge that draws an emoji as an HTML <div> inside a <foreignObject>. The SVG frame renders, and the emoji is missing.
Reproduction#
npm i @tanstack/redact@0.2.2 react@19.3.1 react-dom@19.3.1 jsdom, then node repro.mjs react and node repro.mjs redact:
// repro.mjs
import { JSDOM } from "jsdom"
const { window } = new JSDOM("<!doctype html><div id=root></div>")
Object.assign(globalThis, { window, document: window.document, HTMLElement: window.HTMLElement, Node: window.Node })
const redact = process.argv[2] === "redact"
const R = await import(redact ? "@tanstack/redact" : "react")
const { createRoot } = await import(redact ? "@tanstack/redact/dom-client" : "react-dom/client")
const { flushSync } = await import(redact ? "@tanstack/redact/dom" : "react-dom")
const h = R.createElement
const App = () =>
h("svg", { viewBox: "0 0 100 100" },
h("foreignObject", { width: 100, height: 100 },
h("div", null, h("span", null, "🦔"))))
const root = document.getElementById("root")
flushSync(() => createRoot(root).render(h(App)))
for (const tag of ["foreignObject", "div", "span"]) {
console.log(tag.padEnd(14), root.querySelector(tag).namespaceURI)
}
| Element | React 19.3.1 | Redact 0.2.2 |
|---|---|---|
foreignObject | http://www.w3.org/2000/svg | http://www.w3.org/2000/svg |
div | http://www.w3.org/1999/xhtml | http://www.w3.org/2000/svg |
span | http://www.w3.org/1999/xhtml | http://www.w3.org/2000/svg |
Suggested fix#
Leave the SVG namespace when the DOM parent is a foreignObject:
const isSvg = type === 'svg' ||
((domParent as Element).namespaceURI === 'http://www.w3.org/2000/svg' &&
(domParent as Element).localName !== 'foreignObject');
We run this as a local patch and the emoji renders again.
Sign in to join this conversation.