Skip to content

Commit c4c4718

Browse files
Preserve text and element boundaries in SVG and MathML SSR (#11427)
Co-authored-by: StarbirdTech <64431622+StarbirdTech@users.noreply.github.com>
1 parent 5073089 commit c4c4718

4 files changed

Lines changed: 93 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `HtmlServer` output for SVG and MathML so the browser preserves text in elements such as `style` and `script`. Elements such as `source` now have closing tags, preventing later siblings from becoming their children.

‎packages/effect/src/ui/html/internal/server.ts‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ const voidTags: ReadonlySet<string> = new Set([
3131
])
3232

3333
/**
34-
* Elements whose content the HTML parser reads as text: escaped text
34+
* HTML elements whose content the HTML parser reads as text: escaped text
3535
* (`textarea`, `title`) or raw text. Markers or tags inside them would become
36-
* part of the text, so the server writes only their text.
36+
* part of the text, so the server writes only their text. These rules do not
37+
* apply to elements in the SVG or MathML namespace.
3738
*
3839
* @internal
3940
*/
@@ -54,6 +55,53 @@ const escapedTextTags: ReadonlySet<string> = new Set(["textarea", "title"])
5455
/** Elements whose first newline the HTML parser drops. */
5556
const newlineTags: ReadonlySet<string> = new Set(["pre", "textarea", "listing"])
5657

58+
// Child parsing contexts can differ from the parent element's namespace at integration points.
59+
const HtmlContent = 0
60+
const SvgContent = 1
61+
const MathContent = 2
62+
/** A MathML text integration point: HTML content, but `mglyph` and `malignmark` stay MathML. */
63+
const MathText = 3
64+
/** An `annotation-xml` that isn't an HTML integration point: MathML content, but `svg` is SVG. */
65+
const Annotation = 4
66+
67+
/**
68+
* Resolve an element's namespace from its parent's child parsing context.
69+
* `name` must be ASCII lowercase, matching the HTML parser's tag names.
70+
*/
71+
const namespaceOf = (content: number, name: string): number => {
72+
if (content === SvgContent || content === MathContent) return content
73+
if (content === Annotation) return name === "svg" ? SvgContent : MathContent
74+
if (content === MathText && (name === "mglyph" || name === "malignmark")) return MathContent
75+
return name === "svg" ? SvgContent : name === "math" ? MathContent : HtmlContent
76+
}
77+
78+
/** Child parsing context for an SVG or MathML element. `name` must be ASCII lowercase. */
79+
const foreignContent = (
80+
namespace: number,
81+
name: string,
82+
attrs: Readonly<Record<string, string>>
83+
): number => {
84+
if (namespace === SvgContent) {
85+
return name === "foreignobject" || name === "desc" || name === "title" ? HtmlContent : SvgContent
86+
}
87+
switch (name) {
88+
case "mi":
89+
case "mo":
90+
case "mn":
91+
case "ms":
92+
case "mtext":
93+
return MathText
94+
case "annotation-xml": {
95+
let encoding = ""
96+
for (const key in attrs) {
97+
if (elements.attributeName(key, false) === "encoding") encoding = elements.asciiLower(attrs[key]!)
98+
}
99+
return encoding === "text/html" || encoding === "application/xhtml+xml" ? HtmlContent : Annotation
100+
}
101+
}
102+
return MathContent
103+
}
104+
57105
// Most text and values need no escaping, and are returned as they are without a replace.
58106
const textSpecial = /[&<>]/
59107
const attributeSpecial = /[&"<>]/
@@ -251,6 +299,10 @@ const write = (tape: ReadonlyArray<any>): string => {
251299
let skips = false
252300
/** The value of the select the options being written belong to, cleared once an option matches it. */
253301
let selectValue: string | undefined
302+
/** How the parser reads the innermost open element's children. */
303+
let content = HtmlContent
304+
/** Parent contexts to restore on close, including for elements skipped during writing. */
305+
const contents: Array<number> = []
254306
for (let at = 0; at < tape.length;) {
255307
switch (tape[at++]) {
256308
case Text: {
@@ -277,10 +329,13 @@ const write = (tape: ReadonlyArray<any>): string => {
277329
const props: Readonly<Record<string, unknown>> = tape[at++]
278330
depth++
279331
bare = false
332+
contents.push(content)
280333
if (skips) break
334+
const name = elements.asciiLower(tag)
335+
const namespace = namespaceOf(content, name)
336+
content = namespace === HtmlContent ? HtmlContent : foreignContent(namespace, name, attrs)
281337
const plain = plainAttributes(tag, attrs, props)
282-
// Most elements: outside a select, and closed by their closing tag.
283-
if (selectValue === undefined && !framed.has(tag)) {
338+
if (namespace !== HtmlContent || (selectValue === undefined && !framed.has(tag))) {
284339
out += `<${tag}${plain ?? written(elementAttributes(tag, attrs, props))}>`
285340
// Read for nothing: every element's `value` is read once, whether or not its tag writes it.
286341
void props.value
@@ -340,6 +395,7 @@ const write = (tape: ReadonlyArray<any>): string => {
340395
}
341396
case Close: {
342397
const tag: string = tape[at++]
398+
content = contents.pop()!
343399
const open = frame
344400
if (open === undefined || open.depth !== depth) {
345401
depth--

‎packages/effect/src/ui/html/testing/Ssr.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ const serverNodes = (container: globalThis.Element): Array<ServerNode> => {
171171
continue
172172
}
173173
out.push({ node, paths: paths.slice() })
174-
// A text-only element's content is rebuilt by design: only the element itself is adopted.
175-
if (!server.textOnlyTags.has((node as globalThis.Element).localName)) {
174+
// Hydration rebuilds the contents of text-only HTML elements, adopting only the element itself.
175+
const element = node as globalThis.Element
176+
if (element.namespaceURI !== "http://www.w3.org/1999/xhtml" || !server.textOnlyTags.has(element.localName)) {
176177
visit(node)
177178
}
178179
}

‎packages/effect/test/ui/html/HtmlServer.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,31 @@ describe("HtmlServer", { concurrent: false }, () => {
724724
)
725725
}))
726726

727+
it.effect("applies HTML text and void rules only to elements the parser reads as HTML", () =>
728+
Effect.gen(function*() {
729+
const Page = Html.component({
730+
name: "Foreign",
731+
model: Schema.Null,
732+
message: Schema.Never
733+
}).make({
734+
init: () => null,
735+
update: ($) => $.model,
736+
view: (h) =>
737+
h.div(
738+
h.svg(
739+
h.el("style", "a &lt; <b>"),
740+
h.el("source"),
741+
h.foreignObject(h.el("style", "a < b"))
742+
),
743+
h.el("MATH", h.el("mi", h.el("style", "a < b")), h.el("style", "a < b"))
744+
)
745+
})
746+
const rendered = yield* HtmlServer.renderToHtml(Page, {})
747+
expect(rendered.html).toBe(
748+
"<!--[/Foreign--><div><svg><style>a &amp;lt; &lt;b&gt;</style><source></source><foreignObject><style>a < b</style></foreignObject></svg><MATH><mi><style>a < b</style></mi><style>a &lt; b</style></MATH></div><!--]/Foreign-->"
749+
)
750+
}))
751+
727752
it.effect("escapes text and a textarea's value, keeps a leading newline, and selects the option whose text is the select's value", () =>
728753
Effect.gen(function*() {
729754
const Page = Html.component({

0 commit comments

Comments
 (0)