diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index 25630ba204738..ec726ea807429 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -155,9 +155,19 @@ describe("HtmlRender", () => { "", `
x
`, ``, + '', + 'Example
Content
', + 'Content', + "", + '
Content
', + '`, + `${example}`, + `${example}`, + ]) { + expect(yield* htmlRender.prepare(html)).toContain(html); + } }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.ts index aa36f726bbabc..b50a43a5d8f49 100644 --- a/apps/server/src/htmlRender/HtmlRender.ts +++ b/apps/server/src/htmlRender/HtmlRender.ts @@ -105,9 +105,25 @@ export class HtmlRenderViewportHeightError extends Schema.TaggedError|$)|])((?:=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)|[^>])*)(?:>|$)([\s\S]*?)(?:<\/script\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`|$)|<\?[^>]*(?:>|$)|]*(?:>|$)` + + String.raw`|])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:|$)|])[^>]*(?:>|$)([\s\S]*?)(?:|$)` + + String.raw`|]*)${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` + + 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, + new RegExp(String.raw`])${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 = @@ -147,16 +163,56 @@ const findViewportHeight = (source: string, pattern: RegExp) => { return undefined; }; +// 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) => { - for (const [, scriptTag, script, sheet, tag] of html.matchAll(HTML_CSS_SOURCE)) { + const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags); + const foreign: Array = []; + // HTML content nested in SVG ends a ``, 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) { + if (inCdata) continue; + 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 + 1; + continue; + } + if (closeTag !== undefined) { + const index = foreign.lastIndexOf(closeTag.toLowerCase()); + if (index !== -1 && !inCdata) foreign.length = index; + continue; + } const found = script !== undefined ? (findViewportHeight(scriptTag ?? "", TAG_STYLE) ?? findViewportHeight(script, SCRIPT_STYLE)) - : tag !== undefined - ? findViewportHeight(tag, TAG_STYLE) + : tagAttributes !== undefined + ? findViewportHeight(tagAttributes, TAG_STYLE) : findInCss(sheet ?? ""); if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found })); + const tag = tagName?.toLowerCase(); + if (tag === undefined || inCdata) continue; + 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; };