@@ -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. */
5556const 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.
58106const textSpecial = / [ & < > ] /
59107const 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 --
0 commit comments