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(" 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, "