From 5739e5cf3324e6187aa1a51bc449f9433573c17a Mon Sep 17 00:00:00 2001 From: SAPHID Date: Thu, 3 Dec 2026 16:44:41 +0000 Subject: [PATCH 1/9] fix(server): html_render no longer rejects viewport height examples inside textarea or title text --- apps/server/src/htmlRender/HtmlRender.test.ts | 18 ++++++++++++++++- apps/server/src/htmlRender/HtmlRender.ts | 20 ++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index 25630ba204738..f7db3c828bfdc 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -155,9 +155,16 @@ describe("HtmlRender", () => { "", `
x
`, ``, + '', + 'Example', + '<TITLE>Example</title ><div style="height: 100vh">Content</div>', + '<textarea-demo style="height: 100vh">Content</textarea-demo>', + '<svg><title/></svg><div style="height: 100vh">Content</div>', + '<svg><title data-label="1 > 0" /></svg><style>body{height:100vh}</style>', + '<svg><title style="height: 100vh"/></svg>', ]) { const error = yield* htmlRender.prepare(page).pipe(Effect.flip); - expect(error).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); + expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); } yield* htmlRender.prepare( '<style>main{width:100vw;height:320px}</style><img src="data:image/png;base64,AB1vh+2vh/3vhC">', @@ -179,6 +186,15 @@ describe("HtmlRender", () => { ); yield* htmlRender.prepare(`<style>${"1".repeat(200_000)}vx</style>`); yield* htmlRender.prepare(`<style>p::after{content:"\\"100vh\\""}</style><p>Unit: </p>`); + const example = '<div style="height: 100vh">Example</div>'; + for (const tag of ["textarea", "title", "xmp", "noembed", "noframes", "noscript"]) { + const html = `<${tag}></${tag}-example>${example}<style>body{height:100vh}</style></${tag}>`; + expect(yield* htmlRender.prepare(html)).toContain(html); + } + const plaintext = `<plaintext>${example}</plaintext>${example}`; + expect(yield* htmlRender.prepare(plaintext)).toContain(plaintext); + const textarea = `<textarea data-path=/example/>${example}</textarea>`; + expect(yield* htmlRender.prepare(textarea)).toContain(textarea); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index aa36f726bbabc..f54abd875ca54 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -106,8 +106,15 @@ export class HtmlRenderViewportHeightError extends Schema.TaggedError<HtmlRender // Check stylesheets, style attributes, and scripts (which may write either); skip comments whole. // Consume unclosed comments, tags, or quotes through EOF to keep every pattern linear. -const HTML_CSS_SOURCE = - /<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])((?:=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)|[^>])*)(?:>|$)([\s\S]*?)(?:<\/script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|<[a-z]((?:=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)|[^>])*)/gi; +const HTML_CSS_ATTRIBUTE_VALUE = String.raw`=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)`; +const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*`; +const HTML_TEXT_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|/(?!>)|[^>/])*`; +const HTML_CSS_SOURCE = new RegExp( + String.raw`<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` + + String.raw`|<(?<textElement>textarea|title|xmp|noembed|noframes|noscript)(?=[\t\n\f\r />])(${HTML_TEXT_TAG_ATTRIBUTES})(?:/>|(?:>|$)[\s\S]*?(?:</\k<textElement>(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)|$))` + + String.raw`|<plaintext(?=[\t\n\f\r />])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)[\s\S]*$|<[a-z](${HTML_CSS_TAG_ATTRIBUTES})`, + "gi", +); // Inside a tag, skip other attributes' quoted values, such as a title that mentions style=. // A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space. const TAG_STYLE = @@ -148,13 +155,16 @@ const findViewportHeight = (source: string, pattern: RegExp) => { }; const rejectViewportHeight = (html: string) => { - for (const [, scriptTag, script, sheet, tag] of html.matchAll(HTML_CSS_SOURCE)) { + for (const [, scriptTag, script, sheet, , textTag, plaintextTag, tag] of html.matchAll( + HTML_CSS_SOURCE, + )) { + const attributes = textTag ?? plaintextTag ?? tag; const found = script !== undefined ? (findViewportHeight(scriptTag ?? "", TAG_STYLE) ?? findViewportHeight(script, SCRIPT_STYLE)) - : tag !== undefined - ? findViewportHeight(tag, TAG_STYLE) + : attributes !== undefined + ? findViewportHeight(attributes, TAG_STYLE) : findInCss(sheet ?? ""); if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found })); } From 961507361c7eeaecc544b784d8fe788f354da5c0 Mon Sep 17 00:00:00 2001 From: SAPHID <saphid@users.noreply.github.com> Date: Thu, 3 Dec 2026 17:28:01 +0000 Subject: [PATCH 2/9] fix(server): track SVG/MathML foreign context in viewport-height scanner --- apps/server/src/htmlRender/HtmlRender.test.ts | 25 ++++ apps/server/src/htmlRender/HtmlRender.ts | 123 ++++++++++++++++-- 2 files changed, 135 insertions(+), 13 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index f7db3c828bfdc..e1b46b7274835 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -162,6 +162,17 @@ describe("HtmlRender", () => { '<svg><title/></svg><div style="height: 100vh">Content</div>', '<svg><title data-label="1 > 0" /></svg><style>body{height:100vh}</style>', '<svg><title style="height: 100vh"/></svg>', + "<svg><title><style>body{min-height:100vh}</style></title></svg>", + '<svg><title><div style="height: 100vh">Content</div></title></svg>', + '<textarea style="height: 100vh"/>Example</textarea>', + '<iframe style="height: 100vh">Fallback</iframe>', + "<!example <textarea>><style>main{height:100vh}</style>", + '<iframe><textarea>Fallback</iframe><main style="height:100vh">Content</main>', + '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', + "<svg><foreignObject><div><![CDATA[text><style>main{height:100vh}</style></div></foreignObject></svg>", + '<math><annotation-xml encoding="TEXT/HTML"><div><![CDATA[text><style>main{height:100vh}</style></div></annotation-xml></math>', + "<svg><g><div><![CDATA[text><style>main{height:100vh}</style></div>", + '<math><svg><title><textarea><div style="height:100vh">Content</div>', ]) { const error = yield* htmlRender.prepare(page).pipe(Effect.flip); expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); @@ -195,6 +206,20 @@ describe("HtmlRender", () => { expect(yield* htmlRender.prepare(plaintext)).toContain(plaintext); const textarea = `<textarea data-path=/example/>${example}</textarea>`; expect(yield* htmlRender.prepare(textarea)).toContain(textarea); + const selfClosingTextarea = `<textarea/>${example}</textarea>`; + expect(yield* htmlRender.prepare(selfClosingTextarea)).toContain(selfClosingTextarea); + const afterSelfClosingSvg = `<svg/><title>${example}</title>`; + expect(yield* htmlRender.prepare(afterSelfClosingSvg)).toContain(afterSelfClosingSvg); + const svgForeignObject = `<svg><foreignObject><textarea>${example}</textarea></foreignObject></svg>`; + expect(yield* htmlRender.prepare(svgForeignObject)).toContain(svgForeignObject); + const svgCdata = `<svg><![CDATA[${example}]]></svg>`; + expect(yield* htmlRender.prepare(svgCdata)).toContain(svgCdata); + for (const tag of ["annotation-xml", "mtext"]) { + const mathWithSvg = `<math><${tag}>${svgForeignObject}</${tag}></math>`; + expect(yield* htmlRender.prepare(mathWithSvg)).toContain(mathWithSvg); + } + const afterNestedSvg = `<svg><foreignObject><svg><desc></svg></foreignObject></svg><title>${example}</title>`; + expect(yield* htmlRender.prepare(afterNestedSvg)).toContain(afterNestedSvg); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index f54abd875ca54..d2a7bb9ecb9f1 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -110,15 +110,28 @@ const HTML_CSS_ATTRIBUTE_VALUE = String.raw`=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[ const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*`; const HTML_TEXT_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|/(?!>)|[^>/])*`; const HTML_CSS_SOURCE = new RegExp( - String.raw`<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` + - String.raw`|<(?<textElement>textarea|title|xmp|noembed|noframes|noscript)(?=[\t\n\f\r />])(${HTML_TEXT_TAG_ATTRIBUTES})(?:/>|(?:>|$)[\s\S]*?(?:</\k<textElement>(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)|$))` + - String.raw`|<plaintext(?=[\t\n\f\r />])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)[\s\S]*$|<[a-z](${HTML_CSS_TAG_ATTRIBUTES})`, + String.raw`<!--[\s\S]*?(?:-->|$)|<!\[(CDATA)\[|<!(?!doctype[\t\n\f\r >])[^>]*(?:>|$)` + + String.raw`|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` + + String.raw`|</(svg|math|title|desc|foreignobject|annotation-xml|mi|mo|mn|ms|mtext|p|br)(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` + + String.raw`|<([a-z][^\t\n\f\r />]*)(${HTML_TEXT_TAG_ATTRIBUTES})(\/?)(?:>|$)`, "gi", ); +const HTML_TEXT_END_PATTERNS = new Map( + ["textarea", "title", "xmp", "iframe", "noembed", "noframes", "noscript"].map((tag) => [ + tag, + new RegExp(String.raw`</${tag}(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)`, "gi"), + ]), +); // Inside a tag, skip other attributes' quoted values, such as a title that mentions style=. // A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space. const TAG_STYLE = /\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; +const TAG_ENCODING = + /\sencoding[\t\n\f\r ]*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; +const TAG_FONT_HTML_ATTRIBUTE = + /\s(color|face|size)(?=[\t\n\f\r =/]|$)|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; +const FOREIGN_HTML_START_TAG = + /^(?:b|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|i|img|li|listing|menu|meta|nobr|ol|p|pre|ruby|s|small|span|strong|strike|sub|sup|table|tt|u|ul|var)$/; // A script is not markup, so take any style block or attribute it spells out. const SCRIPT_STYLE = /<!--[\s\S]*?(?:-->|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))/gi; @@ -154,22 +167,106 @@ const findViewportHeight = (source: string, pattern: RegExp) => { return undefined; }; -const rejectViewportHeight = (html: string) => { - for (const [, scriptTag, script, sheet, , textTag, plaintextTag, tag] of html.matchAll( - HTML_CSS_SOURCE, - )) { - const attributes = textTag ?? plaintextTag ?? tag; +const mathAnnotationIntegratesHtml = (attributes: string) => { + for (const [, doubleQuoted, singleQuoted, bare] of attributes.matchAll(TAG_ENCODING)) { + const value = doubleQuoted ?? singleQuoted ?? bare; + if (value === undefined) continue; + const encoding = decodeHtmlAttribute(value, true).toLowerCase(); + return encoding === "text/html" || encoding === "application/xhtml+xml"; + } + return false; +}; + +const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: string) { + const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags); + const foreignStack: Array<{ + readonly tag: string; + readonly namespace: "svg" | "math"; + readonly integrationPoint: boolean; + readonly previousIndex: number | undefined; + }> = []; + const foreignTagIndices = new Map<string, number>(); + const popForeignTag = () => { + const entry = foreignStack.pop()!; + if (entry.previousIndex === undefined) foreignTagIndices.delete(entry.tag); + else foreignTagIndices.set(entry.tag, entry.previousIndex); + }; + const exitForeignContent = () => { + while (foreignStack.at(-1)?.integrationPoint === false) popForeignTag(); + }; + for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) { + const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = + match; + const parent = foreignStack.at(-1); + const foreignNamespace = parent?.integrationPoint === false ? parent.namespace : undefined; + if (cdata !== undefined) { + const foreignCdata = cdata === "CDATA" && foreignNamespace !== undefined; + const end = foreignCdata ? "]]>" : ">"; + const endIndex = html.indexOf(end, pattern.lastIndex); + if (endIndex === -1) break; + pattern.lastIndex = endIndex + end.length; + continue; + } + if (closeTag !== undefined) { + const closed = closeTag.toLowerCase(); + if (closed === "p" || closed === "br") exitForeignContent(); + const index = foreignTagIndices.get(closed); + if (index !== undefined) while (foreignStack.length > index) popForeignTag(); + continue; + } const found = script !== undefined ? (findViewportHeight(scriptTag ?? "", TAG_STYLE) ?? findViewportHeight(script, SCRIPT_STYLE)) - : attributes !== undefined - ? findViewportHeight(attributes, TAG_STYLE) + : tagAttributes !== undefined + ? findViewportHeight(tagAttributes, TAG_STYLE) : findInCss(sheet ?? ""); - if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found })); + if (found !== undefined) return yield* new HtmlRenderViewportHeightError({ found }); + if (tagName === undefined) continue; + const tag = tagName.toLowerCase(); + if ( + foreignNamespace !== undefined && + (FOREIGN_HTML_START_TAG.test(tag) || + (tag === "font" && + Array.from((tagAttributes ?? "").matchAll(TAG_FONT_HTML_ATTRIBUTE)).some( + ([, attr]) => attr !== undefined, + ))) + ) { + exitForeignContent(); + continue; + } + if (foreignNamespace === undefined) { + if (tag === "plaintext") break; + const endPattern = HTML_TEXT_END_PATTERNS.get(tag); + if (endPattern !== undefined) { + endPattern.lastIndex = pattern.lastIndex; + if (endPattern.exec(html) === null) break; + pattern.lastIndex = endPattern.lastIndex; + continue; + } + } + const opensForeign = tag === "svg" || tag === "math"; + const opensMathAnnotation = foreignNamespace === "math" && tag === "annotation-xml"; + const opensIntegrationPoint = + (foreignNamespace === "svg" && + (tag === "title" || tag === "desc" || tag === "foreignobject")) || + (foreignNamespace === "math" && /^(?:mi|mo|mn|ms|mtext)$/.test(tag)) || + (opensMathAnnotation && mathAnnotationIntegratesHtml(tagAttributes ?? "")); + const namespace = + tag === "svg" && parent?.tag === "annotation-xml" + ? "svg" + : (foreignNamespace ?? (opensForeign ? tag : undefined)); + if ( + !selfClosing && + namespace !== undefined && + (opensForeign || opensIntegrationPoint || opensMathAnnotation) + ) { + const previousIndex = foreignTagIndices.get(tag); + foreignTagIndices.set(tag, foreignStack.length); + foreignStack.push({ tag, namespace, integrationPoint: opensIntegrationPoint, previousIndex }); + } } - return Effect.void; -}; +}); export class HtmlRenderStoreError extends Schema.TaggedError<HtmlRenderStoreError>()( "HtmlRenderStoreError", From ebc0ab7523ff50c79ab49bb02f52bcc837214d18 Mon Sep 17 00:00:00 2001 From: SAPHID <saphid@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:28:49 +0000 Subject: [PATCH 3/9] fix(server): handle PIs, malformed DOCTYPEs, end-tag attrs, and valueless encoding in viewport-height scanner --- apps/server/src/htmlRender/HtmlRender.test.ts | 4 ++++ apps/server/src/htmlRender/HtmlRender.ts | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index e1b46b7274835..bbd0006d9b62c 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -167,10 +167,14 @@ describe("HtmlRender", () => { '<textarea style="height: 100vh"/>Example</textarea>', '<iframe style="height: 100vh">Fallback</iframe>', "<!example <textarea>><style>main{height:100vh}</style>", + "<?xml <textarea>><style>main{height:100vh}</style>", + '<!doctype html <textarea>><main style="height:100vh">Content</main>', + '<div></div data-example="<textarea>"><main style="height:100vh">Content</main>', '<iframe><textarea>Fallback</iframe><main style="height:100vh">Content</main>', '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', "<svg><foreignObject><div><![CDATA[text><style>main{height:100vh}</style></div></foreignObject></svg>", '<math><annotation-xml encoding="TEXT/HTML"><div><![CDATA[text><style>main{height:100vh}</style></div></annotation-xml></math>', + '<math><annotation-xml encoding encoding="text/html"><textarea><div style="height:100vh">', "<svg><g><div><![CDATA[text><style>main{height:100vh}</style></div>", '<math><svg><title><textarea><div style="height:100vh">Content</div>', ]) { diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index d2a7bb9ecb9f1..640fc60a82a2b 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -110,9 +110,9 @@ const HTML_CSS_ATTRIBUTE_VALUE = String.raw`=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[ const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*`; const HTML_TEXT_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|/(?!>)|[^>/])*`; const HTML_CSS_SOURCE = new RegExp( - String.raw`<!--[\s\S]*?(?:-->|$)|<!\[(CDATA)\[|<!(?!doctype[\t\n\f\r >])[^>]*(?:>|$)` + + String.raw`<!--[\s\S]*?(?:-->|$)|<\?[^>]*(?:>|$)|<!\[(CDATA)\[|<![^>]*(?:>|$)` + String.raw`|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` + - String.raw`|</(svg|math|title|desc|foreignobject|annotation-xml|mi|mo|mn|ms|mtext|p|br)(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` + + String.raw`|</([a-z][^\t\n\f\r />]*)${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` + String.raw`|<([a-z][^\t\n\f\r />]*)(${HTML_TEXT_TAG_ATTRIBUTES})(\/?)(?:>|$)`, "gi", ); @@ -127,7 +127,7 @@ const HTML_TEXT_END_PATTERNS = new Map( const TAG_STYLE = /\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; const TAG_ENCODING = - /\sencoding[\t\n\f\r ]*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; + /\s(encoding)(?:[\t\n\f\r ]*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]*))|(?=[\t\n\f\r /]|$))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; const TAG_FONT_HTML_ATTRIBUTE = /\s(color|face|size)(?=[\t\n\f\r =/]|$)|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; const FOREIGN_HTML_START_TAG = @@ -168,9 +168,9 @@ const findViewportHeight = (source: string, pattern: RegExp) => { }; const mathAnnotationIntegratesHtml = (attributes: string) => { - for (const [, doubleQuoted, singleQuoted, bare] of attributes.matchAll(TAG_ENCODING)) { - const value = doubleQuoted ?? singleQuoted ?? bare; - if (value === undefined) continue; + for (const [, name, doubleQuoted, singleQuoted, bare] of attributes.matchAll(TAG_ENCODING)) { + if (name === undefined) continue; + const value = doubleQuoted ?? singleQuoted ?? bare ?? ""; const encoding = decodeHtmlAttribute(value, true).toLowerCase(); return encoding === "text/html" || encoding === "application/xhtml+xml"; } From 100fa1d225b84c441ffb3f518524b8eee4a44319 Mon Sep 17 00:00:00 2001 From: SAPHID <saphid@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:00:02 +0000 Subject: [PATCH 4/9] fix(server): retain MathML context for glyph exceptions --- apps/server/src/htmlRender/HtmlRender.test.ts | 11 +++++ apps/server/src/htmlRender/HtmlRender.ts | 44 ++++++++++++++----- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index bbd0006d9b62c..79ccae13a0e26 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -222,6 +222,17 @@ describe("HtmlRender", () => { const mathWithSvg = `<math><${tag}>${svgForeignObject}</${tag}></math>`; expect(yield* htmlRender.prepare(mathWithSvg)).toContain(mathWithSvg); } + for (const tag of ["mglyph", "malignmark"]) { + const activeStyle = `<math><mtext><span>Label</span><br><${tag}><title><mrow style="--sample:1vh"></mrow></title></${tag}></mtext></math>`; + const error = yield* htmlRender.prepare(activeStyle).pipe(Effect.flip); + expect(error, tag).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); + const htmlWrapper = `<math><mtext><span><${tag}><textarea>${example}</textarea></${tag}></span></mtext></math>`; + expect(yield* htmlRender.prepare(htmlWrapper)).toContain(htmlWrapper); + for (const glyph of [`<${tag}/>`, `<${tag}></${tag}>`]) { + const literalText = `<math><mtext>${glyph}<textarea>${example}</textarea></mtext></math>`; + expect(yield* htmlRender.prepare(literalText)).toContain(literalText); + } + } const afterNestedSvg = `<svg><foreignObject><svg><desc></svg></foreignObject></svg><title>${example}</title>`; expect(yield* htmlRender.prepare(afterNestedSvg)).toContain(afterNestedSvg); }).pipe(Effect.provide(layerTest)), diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index 640fc60a82a2b..310d96ad6b3e1 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -132,6 +132,9 @@ const TAG_FONT_HTML_ATTRIBUTE = /\s(color|face|size)(?=[\t\n\f\r =/]|$)|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; const FOREIGN_HTML_START_TAG = /^(?:b|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|i|img|li|listing|menu|meta|nobr|ol|p|pre|ruby|s|small|span|strong|strike|sub|sup|table|tt|u|ul|var)$/; +const MATH_TEXT_INTEGRATION_POINT = /^(?:mi|mo|mn|ms|mtext)$/; +const HTML_NON_CONTAINER_TAG = + /^(?:area|base|basefont|bgsound|body|br|col|embed|frame|head|hr|html|img|input|keygen|link|meta|param|source|track|wbr)$/; // A script is not markup, so take any style block or attribute it spells out. const SCRIPT_STYLE = /<!--[\s\S]*?(?:-->|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))/gi; @@ -181,7 +184,7 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags); const foreignStack: Array<{ readonly tag: string; - readonly namespace: "svg" | "math"; + readonly namespace: "svg" | "math" | "html"; readonly integrationPoint: boolean; readonly previousIndex: number | undefined; }> = []; @@ -192,13 +195,27 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: else foreignTagIndices.set(entry.tag, entry.previousIndex); }; const exitForeignContent = () => { - while (foreignStack.at(-1)?.integrationPoint === false) popForeignTag(); + for (let current = foreignStack.at(-1); current; current = foreignStack.at(-1)) { + if (current.namespace === "html" || current.integrationPoint) break; + popForeignTag(); + } }; for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) { const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = match; + const tag = tagName?.toLowerCase(); const parent = foreignStack.at(-1); - const foreignNamespace = parent?.integrationPoint === false ? parent.namespace : undefined; + const isMathTextException = tag === "mglyph" || tag === "malignmark"; + const mathTextParent = + parent?.namespace === "math" && + parent.integrationPoint && + MATH_TEXT_INTEGRATION_POINT.test(parent.tag); + const foreignNamespace = + parent && + parent.namespace !== "html" && + (!parent.integrationPoint || (isMathTextException && mathTextParent)) + ? parent.namespace + : undefined; if (cdata !== undefined) { const foreignCdata = cdata === "CDATA" && foreignNamespace !== undefined; const end = foreignCdata ? "]]>" : ">"; @@ -222,8 +239,7 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: ? findViewportHeight(tagAttributes, TAG_STYLE) : findInCss(sheet ?? ""); if (found !== undefined) return yield* new HtmlRenderViewportHeightError({ found }); - if (tagName === undefined) continue; - const tag = tagName.toLowerCase(); + if (tag === undefined) continue; if ( foreignNamespace !== undefined && (FOREIGN_HTML_START_TAG.test(tag) || @@ -247,19 +263,27 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: } const opensForeign = tag === "svg" || tag === "math"; const opensMathAnnotation = foreignNamespace === "math" && tag === "annotation-xml"; + const opensMathTextException = foreignNamespace === "math" && isMathTextException; + const opensHtmlWrapper = + foreignNamespace === undefined && + !opensForeign && + (mathTextParent || parent?.namespace === "html") && + !HTML_NON_CONTAINER_TAG.test(tag); const opensIntegrationPoint = (foreignNamespace === "svg" && (tag === "title" || tag === "desc" || tag === "foreignobject")) || - (foreignNamespace === "math" && /^(?:mi|mo|mn|ms|mtext)$/.test(tag)) || + (foreignNamespace === "math" && MATH_TEXT_INTEGRATION_POINT.test(tag)) || (opensMathAnnotation && mathAnnotationIntegratesHtml(tagAttributes ?? "")); - const namespace = - tag === "svg" && parent?.tag === "annotation-xml" + const namespace = opensHtmlWrapper + ? "html" + : tag === "svg" && parent?.tag === "annotation-xml" ? "svg" : (foreignNamespace ?? (opensForeign ? tag : undefined)); if ( - !selfClosing && namespace !== undefined && - (opensForeign || opensIntegrationPoint || opensMathAnnotation) + (opensHtmlWrapper || + (!selfClosing && + (opensForeign || opensIntegrationPoint || opensMathAnnotation || opensMathTextException))) ) { const previousIndex = foreignTagIndices.get(tag); foreignTagIndices.set(tag, foreignStack.length); From 813c72bd6be6c6df79032eb2aafb03dfd657fcc6 Mon Sep 17 00:00:00 2001 From: SAPHID <saphid@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:28:13 +0000 Subject: [PATCH 5/9] fix(server): handle integration-point CDATA and HTML wrapper transitions --- apps/server/src/htmlRender/HtmlRender.test.ts | 6 +++ apps/server/src/htmlRender/HtmlRender.ts | 39 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index 79ccae13a0e26..519c6e26ed52a 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -173,10 +173,14 @@ describe("HtmlRender", () => { '<iframe><textarea>Fallback</iframe><main style="height:100vh">Content</main>', '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', "<svg><foreignObject><div><![CDATA[text><style>main{height:100vh}</style></div></foreignObject></svg>", + "<svg><foreignObject><![CDATA[text><textarea>]]></foreignObject></svg><style>main{height:100vh}</style>", '<math><annotation-xml encoding="TEXT/HTML"><div><![CDATA[text><style>main{height:100vh}</style></div></annotation-xml></math>', + "<math><mtext><![CDATA[text><textarea>]]></mtext></math><style>main{height:100vh}</style>", '<math><annotation-xml encoding encoding="text/html"><textarea><div style="height:100vh">', "<svg><g><div><![CDATA[text><style>main{height:100vh}</style></div>", '<math><svg><title><textarea><div style="height:100vh">Content</div>', + '<math><mtext><tr><mglyph><textarea><div style="height:100vh">', + '<math><mtext><p><div></div><mglyph><textarea><div style="height:100vh">', ]) { const error = yield* htmlRender.prepare(page).pipe(Effect.flip); expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); @@ -228,6 +232,8 @@ describe("HtmlRender", () => { expect(error, tag).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); const htmlWrapper = `<math><mtext><span><${tag}><textarea>${example}</textarea></${tag}></span></mtext></math>`; expect(yield* htmlRender.prepare(htmlWrapper)).toContain(htmlWrapper); + const breakoutWrapper = `<math><mtext><${tag}><div><${tag}><textarea>${example}</textarea></div></mtext></math>`; + expect(yield* htmlRender.prepare(breakoutWrapper)).toContain(breakoutWrapper); for (const glyph of [`<${tag}/>`, `<${tag}></${tag}>`]) { const literalText = `<math><mtext>${glyph}<textarea>${example}</textarea></mtext></math>`; expect(yield* htmlRender.prepare(literalText)).toContain(literalText); diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index 310d96ad6b3e1..e670d8ebb4c4b 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -134,7 +134,10 @@ const FOREIGN_HTML_START_TAG = /^(?:b|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|i|img|li|listing|menu|meta|nobr|ol|p|pre|ruby|s|small|span|strong|strike|sub|sup|table|tt|u|ul|var)$/; const MATH_TEXT_INTEGRATION_POINT = /^(?:mi|mo|mn|ms|mtext)$/; const HTML_NON_CONTAINER_TAG = - /^(?:area|base|basefont|bgsound|body|br|col|embed|frame|head|hr|html|img|input|keygen|link|meta|param|source|track|wbr)$/; + /^(?:area|base|basefont|bgsound|body|br|caption|col|colgroup|embed|frame|frameset|head|hr|html|img|input|keygen|link|meta|param|source|tbody|td|tfoot|th|thead|tr|track|wbr)$/; +const HTML_P_CLOSING_TAG = + /^(?:address|article|aside|blockquote|details|dialog|dd|div|dl|dt|fieldset|figcaption|figure|footer|form|h[1-6]|header|hgroup|hr|li|main|menu|nav|ol|p|pre|search|section|table|ul)$/; +const HTML_P_SCOPE_BOUNDARY = /^(?:applet|caption|html|marquee|object|table|td|template|th)$/; // A script is not markup, so take any style block or attribute it spells out. const SCRIPT_STYLE = /<!--[\s\S]*?(?:-->|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))/gi; @@ -186,6 +189,7 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: readonly tag: string; readonly namespace: "svg" | "math" | "html"; readonly integrationPoint: boolean; + readonly openPIndex: number | undefined; readonly previousIndex: number | undefined; }> = []; const foreignTagIndices = new Map<string, number>(); @@ -204,20 +208,20 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = match; const tag = tagName?.toLowerCase(); - const parent = foreignStack.at(-1); + let parent = foreignStack.at(-1); const isMathTextException = tag === "mglyph" || tag === "malignmark"; const mathTextParent = parent?.namespace === "math" && parent.integrationPoint && MATH_TEXT_INTEGRATION_POINT.test(parent.tag); - const foreignNamespace = + let foreignNamespace = parent && parent.namespace !== "html" && (!parent.integrationPoint || (isMathTextException && mathTextParent)) ? parent.namespace : undefined; if (cdata !== undefined) { - const foreignCdata = cdata === "CDATA" && foreignNamespace !== undefined; + const foreignCdata = cdata === "CDATA" && parent !== undefined && parent.namespace !== "html"; const end = foreignCdata ? "]]>" : ">"; const endIndex = html.indexOf(end, pattern.lastIndex); if (endIndex === -1) break; @@ -249,7 +253,8 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: ))) ) { exitForeignContent(); - continue; + parent = foreignStack.at(-1); + foreignNamespace = undefined; } if (foreignNamespace === undefined) { if (tag === "plaintext") break; @@ -260,6 +265,11 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: pattern.lastIndex = endPattern.lastIndex; continue; } + if (parent?.openPIndex !== undefined && HTML_P_CLOSING_TAG.test(tag)) { + const pIndex = parent.openPIndex; + while (foreignStack.length > pIndex) popForeignTag(); + parent = foreignStack.at(-1); + } } const opensForeign = tag === "svg" || tag === "math"; const opensMathAnnotation = foreignNamespace === "math" && tag === "annotation-xml"; @@ -267,7 +277,7 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: const opensHtmlWrapper = foreignNamespace === undefined && !opensForeign && - (mathTextParent || parent?.namespace === "html") && + (parent?.integrationPoint || parent?.namespace === "html") && !HTML_NON_CONTAINER_TAG.test(tag); const opensIntegrationPoint = (foreignNamespace === "svg" && @@ -286,8 +296,21 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: (opensForeign || opensIntegrationPoint || opensMathAnnotation || opensMathTextException))) ) { const previousIndex = foreignTagIndices.get(tag); - foreignTagIndices.set(tag, foreignStack.length); - foreignStack.push({ tag, namespace, integrationPoint: opensIntegrationPoint, previousIndex }); + const nextIndex = foreignStack.length; + const openPIndex = + namespace !== "html" || HTML_P_SCOPE_BOUNDARY.test(tag) + ? undefined + : tag === "p" + ? nextIndex + : parent?.openPIndex; + foreignTagIndices.set(tag, nextIndex); + foreignStack.push({ + tag, + namespace, + integrationPoint: opensIntegrationPoint, + openPIndex, + previousIndex, + }); } } }); From 918a9dc7dda176748f0f99a4fd9d3f3d1b65d3a2 Mon Sep 17 00:00:00 2001 From: SAPHID <saphid@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:46:49 +0000 Subject: [PATCH 6/9] fix(server): track ordinary foreign elements in viewport-height scanner --- apps/server/src/htmlRender/HtmlRender.test.ts | 2 ++ apps/server/src/htmlRender/HtmlRender.ts | 8 +------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index 519c6e26ed52a..f357b1b4246c9 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -178,6 +178,8 @@ describe("HtmlRender", () => { "<math><mtext><![CDATA[text><textarea>]]></mtext></math><style>main{height:100vh}</style>", '<math><annotation-xml encoding encoding="text/html"><textarea><div style="height:100vh">', "<svg><g><div><![CDATA[text><style>main{height:100vh}</style></div>", + '<svg><g><title></g><textarea><main style="height:100vh"></main></svg>', + "<math><mrow><mtext></mrow><textarea><style>main{height:100vh}</style></math>", '<math><svg><title><textarea><div style="height:100vh">Content</div>', '<math><mtext><tr><mglyph><textarea><div style="height:100vh">', '<math><mtext><p><div></div><mglyph><textarea><div style="height:100vh">', diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index e670d8ebb4c4b..25d7cbbf5eac1 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -273,7 +273,6 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: } const opensForeign = tag === "svg" || tag === "math"; const opensMathAnnotation = foreignNamespace === "math" && tag === "annotation-xml"; - const opensMathTextException = foreignNamespace === "math" && isMathTextException; const opensHtmlWrapper = foreignNamespace === undefined && !opensForeign && @@ -289,12 +288,7 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: : tag === "svg" && parent?.tag === "annotation-xml" ? "svg" : (foreignNamespace ?? (opensForeign ? tag : undefined)); - if ( - namespace !== undefined && - (opensHtmlWrapper || - (!selfClosing && - (opensForeign || opensIntegrationPoint || opensMathAnnotation || opensMathTextException))) - ) { + if (namespace !== undefined && (opensHtmlWrapper || !selfClosing)) { const previousIndex = foreignTagIndices.get(tag); const nextIndex = foreignStack.length; const openPIndex = From 7157f703facc4b01c414e63007f9e842631c8df4 Mon Sep 17 00:00:00 2001 From: Julius Marminge <julius0216@outlook.com> Date: Thu, 3 Dec 2026 20:31:50 +0000 Subject: [PATCH 7/9] refactor(server): keep viewport-height text skipping to HTML content only Replace the tree-builder emulation (integration points, p scope, font breakout, mglyph exceptions) with one conservative rule: raw-text bodies are skipped only outside inline SVG and MathML, and foreign content ends only on its own end tag. The guard can then only err towards checking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- apps/server/src/htmlRender/HtmlRender.test.ts | 62 ++------ apps/server/src/htmlRender/HtmlRender.ts | 147 +++--------------- 2 files changed, 33 insertions(+), 176 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index f357b1b4246c9..fcf2a7e067036 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -156,33 +156,14 @@ describe("HtmlRender", () => { `<main style="--a:&quot;'&quot;;height:100vh;--b:&quot;'&quot;">x</main>`, `<script>document.write("<style>" + 'main{height:100vh}' + "</style>");</script>`, '<textarea title="1 > 0" style="height: 100vh">Example</textarea>', - '<plaintext style="height: 100vh">Example', '<TITLE>Example</title ><div style="height: 100vh">Content</div>', '<textarea-demo style="height: 100vh">Content</textarea-demo>', - '<svg><title/></svg><div style="height: 100vh">Content</div>', - '<svg><title data-label="1 > 0" /></svg><style>body{height:100vh}</style>', - '<svg><title style="height: 100vh"/></svg>', "<svg><title><style>body{min-height:100vh}</style></title></svg>", - '<svg><title><div style="height: 100vh">Content</div></title></svg>', - '<textarea style="height: 100vh"/>Example</textarea>', - '<iframe style="height: 100vh">Fallback</iframe>', + '<svg><title/></svg><div style="height: 100vh">Content</div>', + '<math><mtext></svg><textarea><div style="height:100vh">', + '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', "<!example <textarea>><style>main{height:100vh}</style>", - "<?xml <textarea>><style>main{height:100vh}</style>", - '<!doctype html <textarea>><main style="height:100vh">Content</main>', '<div></div data-example="<textarea>"><main style="height:100vh">Content</main>', - '<iframe><textarea>Fallback</iframe><main style="height:100vh">Content</main>', - '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', - "<svg><foreignObject><div><![CDATA[text><style>main{height:100vh}</style></div></foreignObject></svg>", - "<svg><foreignObject><![CDATA[text><textarea>]]></foreignObject></svg><style>main{height:100vh}</style>", - '<math><annotation-xml encoding="TEXT/HTML"><div><![CDATA[text><style>main{height:100vh}</style></div></annotation-xml></math>', - "<math><mtext><![CDATA[text><textarea>]]></mtext></math><style>main{height:100vh}</style>", - '<math><annotation-xml encoding encoding="text/html"><textarea><div style="height:100vh">', - "<svg><g><div><![CDATA[text><style>main{height:100vh}</style></div>", - '<svg><g><title></g><textarea><main style="height:100vh"></main></svg>', - "<math><mrow><mtext></mrow><textarea><style>main{height:100vh}</style></math>", - '<math><svg><title><textarea><div style="height:100vh">Content</div>', - '<math><mtext><tr><mglyph><textarea><div style="height:100vh">', - '<math><mtext><p><div></div><mglyph><textarea><div style="height:100vh">', ]) { const error = yield* htmlRender.prepare(page).pipe(Effect.flip); expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); @@ -212,37 +193,14 @@ describe("HtmlRender", () => { const html = `<${tag}></${tag}-example>${example}<style>body{height:100vh}</style></${tag}>`; expect(yield* htmlRender.prepare(html)).toContain(html); } - const plaintext = `<plaintext>${example}</plaintext>${example}`; - expect(yield* htmlRender.prepare(plaintext)).toContain(plaintext); - const textarea = `<textarea data-path=/example/>${example}</textarea>`; - expect(yield* htmlRender.prepare(textarea)).toContain(textarea); - const selfClosingTextarea = `<textarea/>${example}</textarea>`; - expect(yield* htmlRender.prepare(selfClosingTextarea)).toContain(selfClosingTextarea); - const afterSelfClosingSvg = `<svg/><title>${example}</title>`; - expect(yield* htmlRender.prepare(afterSelfClosingSvg)).toContain(afterSelfClosingSvg); - const svgForeignObject = `<svg><foreignObject><textarea>${example}</textarea></foreignObject></svg>`; - expect(yield* htmlRender.prepare(svgForeignObject)).toContain(svgForeignObject); - const svgCdata = `<svg><![CDATA[${example}]]></svg>`; - expect(yield* htmlRender.prepare(svgCdata)).toContain(svgCdata); - for (const tag of ["annotation-xml", "mtext"]) { - const mathWithSvg = `<math><${tag}>${svgForeignObject}</${tag}></math>`; - expect(yield* htmlRender.prepare(mathWithSvg)).toContain(mathWithSvg); - } - for (const tag of ["mglyph", "malignmark"]) { - const activeStyle = `<math><mtext><span>Label</span><br><${tag}><title><mrow style="--sample:1vh"></mrow></title></${tag}></mtext></math>`; - const error = yield* htmlRender.prepare(activeStyle).pipe(Effect.flip); - expect(error, tag).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError); - const htmlWrapper = `<math><mtext><span><${tag}><textarea>${example}</textarea></${tag}></span></mtext></math>`; - expect(yield* htmlRender.prepare(htmlWrapper)).toContain(htmlWrapper); - const breakoutWrapper = `<math><mtext><${tag}><div><${tag}><textarea>${example}</textarea></div></mtext></math>`; - expect(yield* htmlRender.prepare(breakoutWrapper)).toContain(breakoutWrapper); - for (const glyph of [`<${tag}/>`, `<${tag}></${tag}>`]) { - const literalText = `<math><mtext>${glyph}<textarea>${example}</textarea></mtext></math>`; - expect(yield* htmlRender.prepare(literalText)).toContain(literalText); - } + for (const html of [ + `<plaintext>${example}</plaintext>${example}`, + `<textarea/>${example}</textarea>`, + `<svg/><title>${example}</title>`, + `<svg><svg></svg></svg><title>${example}</title>`, + ]) { + expect(yield* htmlRender.prepare(html)).toContain(html); } - const afterNestedSvg = `<svg><foreignObject><svg><desc></svg></foreignObject></svg><title>${example}</title>`; - expect(yield* htmlRender.prepare(afterNestedSvg)).toContain(afterNestedSvg); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index 25d7cbbf5eac1..2ed28e9ed84f4 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -105,6 +105,7 @@ export class HtmlRenderViewportHeightError extends Schema.TaggedError<HtmlRender } // Check stylesheets, style attributes, and scripts (which may write either); skip comments whole. +// End tags are consumed whole so a `<` in their attributes does not open a tag. // Consume unclosed comments, tags, or quotes through EOF to keep every pattern linear. const HTML_CSS_ATTRIBUTE_VALUE = String.raw`=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)`; const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*`; @@ -116,6 +117,7 @@ const HTML_CSS_SOURCE = new RegExp( String.raw`|<([a-z][^\t\n\f\r />]*)(${HTML_TEXT_TAG_ATTRIBUTES})(\/?)(?:>|$)`, "gi", ); +// Text-only elements whose body the browser never parses as markup in HTML content. const HTML_TEXT_END_PATTERNS = new Map( ["textarea", "title", "xmp", "iframe", "noembed", "noframes", "noscript"].map((tag) => [ tag, @@ -126,18 +128,6 @@ const HTML_TEXT_END_PATTERNS = new Map( // A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space. const TAG_STYLE = /\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; -const TAG_ENCODING = - /\s(encoding)(?:[\t\n\f\r ]*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]*))|(?=[\t\n\f\r /]|$))|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; -const TAG_FONT_HTML_ATTRIBUTE = - /\s(color|face|size)(?=[\t\n\f\r =/]|$)|=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)/gi; -const FOREIGN_HTML_START_TAG = - /^(?:b|big|blockquote|body|br|center|code|dd|div|dl|dt|em|embed|h[1-6]|head|hr|i|img|li|listing|menu|meta|nobr|ol|p|pre|ruby|s|small|span|strong|strike|sub|sup|table|tt|u|ul|var)$/; -const MATH_TEXT_INTEGRATION_POINT = /^(?:mi|mo|mn|ms|mtext)$/; -const HTML_NON_CONTAINER_TAG = - /^(?:area|base|basefont|bgsound|body|br|caption|col|colgroup|embed|frame|frameset|head|hr|html|img|input|keygen|link|meta|param|source|tbody|td|tfoot|th|thead|tr|track|wbr)$/; -const HTML_P_CLOSING_TAG = - /^(?:address|article|aside|blockquote|details|dialog|dd|div|dl|dt|fieldset|figcaption|figure|footer|form|h[1-6]|header|hgroup|hr|li|main|menu|nav|ol|p|pre|search|section|table|ul)$/; -const HTML_P_SCOPE_BOUNDARY = /^(?:applet|caption|html|marquee|object|table|td|template|th)$/; // A script is not markup, so take any style block or attribute it spells out. const SCRIPT_STYLE = /<!--[\s\S]*?(?:-->|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))/gi; @@ -173,66 +163,24 @@ const findViewportHeight = (source: string, pattern: RegExp) => { return undefined; }; -const mathAnnotationIntegratesHtml = (attributes: string) => { - for (const [, name, doubleQuoted, singleQuoted, bare] of attributes.matchAll(TAG_ENCODING)) { - if (name === undefined) continue; - const value = doubleQuoted ?? singleQuoted ?? bare ?? ""; - const encoding = decodeHtmlAttribute(value, true).toLowerCase(); - return encoding === "text/html" || encoding === "application/xhtml+xml"; - } - return false; -}; - -const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: string) { +// Inside inline SVG or MathML a textarea or title body is markup, so it is checked like any other. +// Leaving foreign content only on its own end tag errs towards checking more. +const rejectViewportHeight = (html: string) => { const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags); - const foreignStack: Array<{ - readonly tag: string; - readonly namespace: "svg" | "math" | "html"; - readonly integrationPoint: boolean; - readonly openPIndex: number | undefined; - readonly previousIndex: number | undefined; - }> = []; - const foreignTagIndices = new Map<string, number>(); - const popForeignTag = () => { - const entry = foreignStack.pop()!; - if (entry.previousIndex === undefined) foreignTagIndices.delete(entry.tag); - else foreignTagIndices.set(entry.tag, entry.previousIndex); - }; - const exitForeignContent = () => { - for (let current = foreignStack.at(-1); current; current = foreignStack.at(-1)) { - if (current.namespace === "html" || current.integrationPoint) break; - popForeignTag(); - } - }; + const foreign: Array<string> = []; for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) { const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = match; - const tag = tagName?.toLowerCase(); - let parent = foreignStack.at(-1); - const isMathTextException = tag === "mglyph" || tag === "malignmark"; - const mathTextParent = - parent?.namespace === "math" && - parent.integrationPoint && - MATH_TEXT_INTEGRATION_POINT.test(parent.tag); - let foreignNamespace = - parent && - parent.namespace !== "html" && - (!parent.integrationPoint || (isMathTextException && mathTextParent)) - ? parent.namespace - : undefined; if (cdata !== undefined) { - const foreignCdata = cdata === "CDATA" && parent !== undefined && parent.namespace !== "html"; - const end = foreignCdata ? "]]>" : ">"; + const end = cdata === "CDATA" && foreign.length > 0 ? "]]>" : ">"; const endIndex = html.indexOf(end, pattern.lastIndex); if (endIndex === -1) break; pattern.lastIndex = endIndex + end.length; continue; } if (closeTag !== undefined) { - const closed = closeTag.toLowerCase(); - if (closed === "p" || closed === "br") exitForeignContent(); - const index = foreignTagIndices.get(closed); - if (index !== undefined) while (foreignStack.length > index) popForeignTag(); + const index = foreign.lastIndexOf(closeTag.toLowerCase()); + if (index !== -1) foreign.length = index; continue; } const found = @@ -242,72 +190,23 @@ const rejectViewportHeight = Effect.fn("rejectViewportHeight")(function* (html: : tagAttributes !== undefined ? findViewportHeight(tagAttributes, TAG_STYLE) : findInCss(sheet ?? ""); - if (found !== undefined) return yield* new HtmlRenderViewportHeightError({ found }); + if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found })); + const tag = tagName?.toLowerCase(); if (tag === undefined) continue; - if ( - foreignNamespace !== undefined && - (FOREIGN_HTML_START_TAG.test(tag) || - (tag === "font" && - Array.from((tagAttributes ?? "").matchAll(TAG_FONT_HTML_ATTRIBUTE)).some( - ([, attr]) => attr !== undefined, - ))) - ) { - exitForeignContent(); - parent = foreignStack.at(-1); - foreignNamespace = undefined; - } - if (foreignNamespace === undefined) { - if (tag === "plaintext") break; - const endPattern = HTML_TEXT_END_PATTERNS.get(tag); - if (endPattern !== undefined) { - endPattern.lastIndex = pattern.lastIndex; - if (endPattern.exec(html) === null) break; - pattern.lastIndex = endPattern.lastIndex; - continue; - } - if (parent?.openPIndex !== undefined && HTML_P_CLOSING_TAG.test(tag)) { - const pIndex = parent.openPIndex; - while (foreignStack.length > pIndex) popForeignTag(); - parent = foreignStack.at(-1); - } - } - const opensForeign = tag === "svg" || tag === "math"; - const opensMathAnnotation = foreignNamespace === "math" && tag === "annotation-xml"; - const opensHtmlWrapper = - foreignNamespace === undefined && - !opensForeign && - (parent?.integrationPoint || parent?.namespace === "html") && - !HTML_NON_CONTAINER_TAG.test(tag); - const opensIntegrationPoint = - (foreignNamespace === "svg" && - (tag === "title" || tag === "desc" || tag === "foreignobject")) || - (foreignNamespace === "math" && MATH_TEXT_INTEGRATION_POINT.test(tag)) || - (opensMathAnnotation && mathAnnotationIntegratesHtml(tagAttributes ?? "")); - const namespace = opensHtmlWrapper - ? "html" - : tag === "svg" && parent?.tag === "annotation-xml" - ? "svg" - : (foreignNamespace ?? (opensForeign ? tag : undefined)); - if (namespace !== undefined && (opensHtmlWrapper || !selfClosing)) { - const previousIndex = foreignTagIndices.get(tag); - const nextIndex = foreignStack.length; - const openPIndex = - namespace !== "html" || HTML_P_SCOPE_BOUNDARY.test(tag) - ? undefined - : tag === "p" - ? nextIndex - : parent?.openPIndex; - foreignTagIndices.set(tag, nextIndex); - foreignStack.push({ - tag, - namespace, - integrationPoint: opensIntegrationPoint, - openPIndex, - previousIndex, - }); + if (tag === "svg" || tag === "math") { + if (!selfClosing) foreign.push(tag); + continue; } + if (foreign.length > 0) continue; + if (tag === "plaintext") break; + const endPattern = HTML_TEXT_END_PATTERNS.get(tag); + if (endPattern === undefined) continue; + endPattern.lastIndex = pattern.lastIndex; + if (endPattern.exec(html) === null) break; + pattern.lastIndex = endPattern.lastIndex; } -}); + return Effect.void; +}; export class HtmlRenderStoreError extends Schema.TaggedError<HtmlRenderStoreError>()( "HtmlRenderStoreError", From edd3adc0f776f37be2a63645656daf88f488840e Mon Sep 17 00:00:00 2001 From: Julius Marminge <julius0216@outlook.com> Date: Thu, 3 Dec 2026 21:08:18 +0000 Subject: [PATCH 8/9] fix(server): keep checking markup inside CDATA nested in SVG HTML content inside SVG (after <foreignObject><div> or a breakout tag) ends <![CDATA[ at the first '>', so skipping to ]]> hid real styles. Inside a foreign CDATA the scanner now keeps checking attributes and style blocks, and only stops end tags from leaving foreign content and text-only tags from starting a skip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- apps/server/src/htmlRender/HtmlRender.test.ts | 1 + apps/server/src/htmlRender/HtmlRender.ts | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index fcf2a7e067036..ec726ea807429 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -162,6 +162,7 @@ describe("HtmlRender", () => { '<svg><title/></svg><div style="height: 100vh">Content</div>', '<math><mtext></svg><textarea><div style="height:100vh">', '<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>', + '<svg><p><![CDATA[><div style="height:100vh">x</div>]]>', "<!example <textarea>><style>main{height:100vh}</style>", '<div></div data-example="<textarea>"><main style="height:100vh">Content</main>', ]) { diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index 2ed28e9ed84f4..1186a90db783b 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -168,19 +168,27 @@ const findViewportHeight = (source: string, pattern: RegExp) => { const rejectViewportHeight = (html: string) => { const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags); const foreign: Array<string> = []; + // HTML content nested in SVG ends a `<![CDATA[` at the first `>`, so its body is still checked; + // it only stops tags from changing the scanner's state. + let cdataEnd = -1; for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) { const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = match; + const inCdata = match.index < cdataEnd; if (cdata !== undefined) { - const end = cdata === "CDATA" && foreign.length > 0 ? "]]>" : ">"; - const endIndex = html.indexOf(end, pattern.lastIndex); + if (cdata === "CDATA" && foreign.length > 0) { + const endIndex = html.indexOf("]]>", pattern.lastIndex); + cdataEnd = endIndex === -1 ? html.length : endIndex; + continue; + } + const endIndex = html.indexOf(">", pattern.lastIndex); if (endIndex === -1) break; - pattern.lastIndex = endIndex + end.length; + pattern.lastIndex = endIndex + 1; continue; } if (closeTag !== undefined) { const index = foreign.lastIndexOf(closeTag.toLowerCase()); - if (index !== -1) foreign.length = index; + if (index !== -1 && !inCdata) foreign.length = index; continue; } const found = @@ -192,7 +200,7 @@ const rejectViewportHeight = (html: string) => { : findInCss(sheet ?? ""); if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found })); const tag = tagName?.toLowerCase(); - if (tag === undefined) continue; + if (tag === undefined || inCdata) continue; if (tag === "svg" || tag === "math") { if (!selfClosing) foreign.push(tag); continue; From 1ea5892b146a0d8a83aa8bb4896b3e3c0514e86b Mon Sep 17 00:00:00 2001 From: Julius Marminge <julius0216@outlook.com> Date: Thu, 3 Dec 2026 21:12:44 +0000 Subject: [PATCH 9/9] perf(server): scan nested CDATA openers in SVG CDATA once Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- apps/server/src/htmlRender/HtmlRender.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index 1186a90db783b..b50a43a5d8f49 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -176,6 +176,7 @@ const rejectViewportHeight = (html: string) => { match; const inCdata = match.index < cdataEnd; if (cdata !== undefined) { + if (inCdata) continue; if (cdata === "CDATA" && foreign.length > 0) { const endIndex = html.indexOf("]]>", pattern.lastIndex); cdataEnd = endIndex === -1 ? html.length : endIndex;