diff --git a/packages/core/src/compiler/htmlDocument.test.ts b/packages/core/src/compiler/htmlDocument.test.ts index b5bb0ebbf9..fa73321984 100644 --- a/packages/core/src/compiler/htmlDocument.test.ts +++ b/packages/core/src/compiler/htmlDocument.test.ts @@ -11,6 +11,7 @@ import { insertRuntimeTag, hasCompositionOutsideTemplates, isFullHtmlDocument, + mapStartTagsAndStyles, parseHTMLContent, stripEmbeddedRuntimeScripts, } from "./htmlDocument.js"; @@ -63,7 +64,41 @@ function scannedChars(run: () => void): number { const workGrowth = (scan: (n: number) => void) => scannedChars(() => scan(20_000)) / scannedChars(() => scan(10_000)); +/** Lowercased copies `run` makes of any string at least `size` long. */ +function lowercasedCopies(size: number, run: () => void): number { + const proto = String.prototype as unknown as Record unknown>; + const originals = ["replace", "toLowerCase"].map((name) => [name, proto[name]!] as const); + let copies = 0; + for (const [name, original] of originals) { + proto[name] = function (this: string, ...args: unknown[]) { + if (this.length >= size) copies += 1; + return original.apply(this, args); + }; + } + try { + run(); + } finally { + for (const [name, original] of originals) proto[name] = original; + } + return copies; +} + describe("htmlDocument helpers", () => { + it("finds a large page's tags without lowercasing a copy of the page for each pass", () => { + const clip = ''; + const page = `x${clip.repeat(20_000)}`; + const copies = lowercasedCopies(page.length / 2, () => { + insertBeforeCloseTag(page, "head", ""); + insertBeforeCloseTag(page, "body", ""); + injectTagsAtHeadStart(page, ""); + findStartTags(page, "base"); + mapStartTagsAndStyles(page, { tag: (tag) => tag, style: (css) => css }); + hasCompositionOutsideTemplates(page); + stripEmbeddedRuntimeScripts(page); + }); + expect(copies).toBe(0); + }); + it("strips runtimes from large mixed-case base64 HTML within a bounded heap", () => { const moduleUrl = pathToFileURL(resolve(__dirname, "htmlDocument.ts")).href; const result = spawnSync( diff --git a/packages/core/src/compiler/htmlDocument.ts b/packages/core/src/compiler/htmlDocument.ts index 94ae0ae952..36fe8c7ffe 100644 --- a/packages/core/src/compiler/htmlDocument.ts +++ b/packages/core/src/compiler/htmlDocument.ts @@ -65,14 +65,44 @@ function lowerAscii(text: string): string { ); } +function startsWithLower(text: string, token: string, at: number): boolean { + if (at < 0 || at + token.length > text.length) return false; + for (let i = 0; i < token.length; i += 1) { + const code = text.charCodeAt(at + i); + if ((code >= 65 && code <= 90 ? code + 32 : code) !== token.charCodeAt(i)) return false; + } + return true; +} + +function indexOfLower(text: string, token: string, from: number): number { + for ( + let at = text.indexOf(token.charAt(0), from); + at !== -1; + at = text.indexOf(token.charAt(0), at + 1) + ) { + if (startsWithLower(text, token, at)) return at; + } + return -1; +} + +function lastIndexOfLower(text: string, token: string): number { + for ( + let at = text.lastIndexOf(token.charAt(0)); + at !== -1; + at = at === 0 ? -1 : text.lastIndexOf(token.charAt(0), at - 1) + ) { + if (startsWithLower(text, token, at)) return at; + } + return -1; +} + export function stripEmbeddedRuntimeScripts(html: string): string { if (!html) return html; - const loweredHtml = lowerAscii(html); let output = ""; let cursor = 0; while (cursor < html.length) { - const scriptStart = findScriptStart(loweredHtml, cursor); + const scriptStart = findScriptStart(html, cursor); if (scriptStart === -1) { output += html.slice(cursor); break; @@ -85,7 +115,7 @@ export function stripEmbeddedRuntimeScripts(html: string): string { break; } - const closeTagEnd = findScriptCloseTagEnd(loweredHtml, startTagEnd + 1); + const closeTagEnd = findScriptCloseTagEnd(html, startTagEnd + 1); const scriptEnd = closeTagEnd === -1 ? html.length : closeTagEnd; const block = html.slice(scriptStart, scriptEnd); if (!shouldStripRuntimeScriptBlock(block, html.slice(scriptStart, startTagEnd + 1))) { @@ -97,12 +127,12 @@ export function stripEmbeddedRuntimeScripts(html: string): string { return output; } -function findScriptStart(loweredHtml: string, from: number): number { - let index = loweredHtml.indexOf("" ? cursor + 1 : -1; + return html[cursor] === ">" ? cursor + 1 : -1; } function shouldStripRuntimeScriptBlock(block: string, startTag: string): boolean { @@ -183,8 +213,7 @@ export function isRuntimeFileUrl(src: string | null | undefined): boolean { function getScriptSource(block: string): string { const startTagEnd = findTagEnd(block, 1); if (startTagEnd === -1) return ""; - const loweredBlock = lowerAscii(block); - const closeTagStart = loweredBlock.lastIndexOf(" string, ): string { - const loweredSource = lowerAscii(source); const loweredToken = lowerAscii(token); let output = ""; let cursor = 0; while (cursor < source.length) { - const tokenStart = loweredSource.indexOf(loweredToken, cursor); + const tokenStart = indexOfLower(source, loweredToken, cursor); if (tokenStart === -1) { output += source.slice(cursor); break; @@ -242,21 +270,20 @@ const RAW_TEXT_TAGS = ["script", "style", "title", "textarea"] as const; type DocumentTag = "/g; -function* markupStarts(lowered: string): Generator { +function* markupStarts(html: string): Generator { const unclosedRawText = new Set(); let cursor = 0; while (cursor !== -1) { - const open = lowered.indexOf("<", cursor); + const open = html.indexOf("<", cursor); if (open === -1) return; yield open; - cursor = skipMarkup(lowered, open, unclosedRawText); + cursor = skipMarkup(html, open, unclosedRawText); } } function findDocumentTag(html: string, tag: DocumentTag): number { - const lowered = lowerAscii(html); - for (const open of markupStarts(lowered)) { - if (isTagAt(lowered, open, tag)) return open; + for (const open of markupStarts(html)) { + if (isTagAt(html, open, tag)) return open; } return -1; } @@ -265,17 +292,16 @@ export function mapStartTagsAndStyles( html: string, map: { tag: (tag: string) => string; style: (css: string) => string }, ): string { - const lowered = lowerAscii(html); let out = ""; let cursor = 0; - for (const open of markupStarts(lowered)) { - if (open < cursor || !/[a-z]/.test(lowered.charAt(open + 1))) continue; - const end = findTagEnd(lowered, open + 1); + for (const open of markupStarts(html)) { + if (open < cursor || !/[a-z]/i.test(html.charAt(open + 1))) continue; + const end = findTagEnd(html, open + 1); if (end === -1) break; out += html.slice(cursor, open) + map.tag(html.slice(open, end + 1)); cursor = end + 1; - if (!isTagAt(lowered, open, " 0 && isTagAt(lowered, open, " 0 && isTagAt(html, open, "): number { - if (lowered.startsWith("", open) || lowered.startsWith("", open)) { - return lowered.indexOf(">", open) + 1; +function skipComment(html: string, open: number): number { + if (html.startsWith("", open) || html.startsWith("", open)) { + return html.indexOf(">", open) + 1; } COMMENT_END.lastIndex = open + 4; - const end = COMMENT_END.exec(lowered); + const end = COMMENT_END.exec(html); return end ? end.index + end[0].length : -1; } function skipRawText( - lowered: string, + html: string, open: number, tagEnd: number, unclosedRawText: Set, ): number { - const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(lowered, open, `<${name}`)); + const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(html, open, `<${name}`)); if (!rawText) return tagEnd + 1; - const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(lowered, rawText, tagEnd + 1); + const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(html, rawText, tagEnd + 1); if (close !== -1) return close; unclosedRawText.add(rawText); - return lowered.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1; + return html.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1; } -function findRawTextClose(lowered: string, name: string, from: number): number { +function findRawTextClose(html: string, name: string, from: number): number { const close = ` - isTagAt(lowered, open, " + isTagAt(html, open, " 0) idInTemplate = true; - else if (isDocumentWrapper(lowered, open)) idOnWrapper = true; + else if (isDocumentWrapper(html, open)) idOnWrapper = true; else return true; } } @@ -405,7 +432,7 @@ function insertTagInHead(html: string, tag: string): string { const insertPos = htmlOpenMatch.index + htmlOpenMatch[0].length; return `${html.slice(0, insertPos)}${tag}${html.slice(insertPos)}`; } - const doctypeIdx = html.toLowerCase().indexOf("= 0) { const insertPos = html.indexOf(">", doctypeIdx) + 1; return html.slice(0, insertPos) + tag + html.slice(insertPos); diff --git a/packages/parsers/src/hfIds.ts b/packages/parsers/src/hfIds.ts index 9180f1ea07..5ce1d1a413 100644 --- a/packages/parsers/src/hfIds.ts +++ b/packages/parsers/src/hfIds.ts @@ -228,23 +228,14 @@ function emptyHfIdValue(html: string, tag: StartTag): number | null { } function outsideQuotes(open: string): string { - let quote = ""; - let out = ""; - for (const c of open) { - out += quotedChar(quote, c); - quote = quoteOf(quote, c); - } - return out; -} - -function quoteOf(quote: string, c: string): string { - if (quote) return c === quote ? "" : quote; - return c === '"' || c === "'" ? c : ""; -} - -function quotedChar(quote: string, c: string): string { - if (!quote || c === quote) return c; - return " ".repeat(c.length); + return open.replace(/"[^"]*"?|'[^']*'?/g, (quoted) => { + const closed = quoted.length > 1 && quoted.endsWith(quoted.charAt(0)); + return ( + quoted.charAt(0) + + " ".repeat(quoted.length - (closed ? 2 : 1)) + + (closed ? quoted.charAt(0) : "") + ); + }); } function spliceIds(html: string, inserts: Insert[]): string {