From 1ade8f658cb85b6ccec65dcb3836c736264061f4 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 14 Nov 2026 21:33:08 -0800 Subject: [PATCH 01/13] perf(parsers): find CSS url() references in linear time A new css-url-scans entry finds what each CSS url( regex in the compile path and the runtime finds, match for match and group for group, with forward scans. The regexes restart from every unclosed url(, @import or background: and read to the end of the text each time, so their time grows with the square or cube of the input. The scans read each character a bounded number of times. rewriteCssAssetUrls uses the scan in place of CSS_URL_RE, which stays exported for existing callers. A fuzz test checks 20,000 generated pages against the regexes, and timed tests cover 1 MB of each unclosed form. The parsers packed-size budget rises to cover the new entry. --- packages/parsers/package-subpaths.json | 6 + packages/parsers/package.json | 10 + packages/parsers/src/cssUrlScans.test.ts | 180 +++++++++ packages/parsers/src/cssUrlScans.ts | 396 ++++++++++++++++++++ packages/parsers/src/rewriteSubCompPaths.ts | 4 +- packages/parsers/tsup.config.ts | 1 + scripts/package-size-budgets.json | 2 +- 7 files changed, 596 insertions(+), 3 deletions(-) create mode 100644 packages/parsers/src/cssUrlScans.test.ts create mode 100644 packages/parsers/src/cssUrlScans.ts diff --git a/packages/parsers/package-subpaths.json b/packages/parsers/package-subpaths.json index 0cbe9eb1ff9..37a02ee123b 100644 --- a/packages/parsers/package-subpaths.json +++ b/packages/parsers/package-subpaths.json @@ -68,6 +68,12 @@ "types": "./dist/assets.d.ts", "environments": ["bun", "node"] }, + "./css-url-scans": { + "source": "./src/cssUrlScans.ts", + "runtime": "./dist/cssUrlScans.js", + "types": "./dist/cssUrlScans.d.ts", + "environments": ["browser", "bun", "node"] + }, "./composition": { "source": "./src/composition.ts", "runtime": "./dist/composition.js", diff --git a/packages/parsers/package.json b/packages/parsers/package.json index 3f872e48587..f0b7942df89 100644 --- a/packages/parsers/package.json +++ b/packages/parsers/package.json @@ -76,6 +76,12 @@ "import": "./src/assets.ts", "types": "./src/assets.ts" }, + "./css-url-scans": { + "bun": "./src/cssUrlScans.ts", + "node": "./dist/cssUrlScans.js", + "import": "./src/cssUrlScans.ts", + "types": "./src/cssUrlScans.ts" + }, "./composition": { "bun": "./src/composition.ts", "node": "./dist/composition.js", @@ -217,6 +223,10 @@ "import": "./dist/assets.js", "types": "./dist/assets.d.ts" }, + "./css-url-scans": { + "import": "./dist/cssUrlScans.js", + "types": "./dist/cssUrlScans.d.ts" + }, "./composition": { "import": "./dist/composition.js", "types": "./dist/composition.d.ts" diff --git a/packages/parsers/src/cssUrlScans.test.ts b/packages/parsers/src/cssUrlScans.test.ts new file mode 100644 index 00000000000..7e4c115222b --- /dev/null +++ b/packages/parsers/src/cssUrlScans.test.ts @@ -0,0 +1,180 @@ +import { isDeepStrictEqual } from "node:util"; +import { describe, expect, it } from "vitest"; +import { + cssImportMatches, + cssUrlMatches, + hasBackgroundImageDeclaration, + importRuleMatches, + localFontFaceUrlMatches, + rawCssUrlMatches, + remoteBackgroundUrlMatches, + remoteFontFaceUrlMatches, + replaceMatches, + sceneCssUrlMatches, + urlHashRefMatches, + type ScanMatch, +} from "./cssUrlScans"; +import { rewriteCssAssetUrls } from "./rewriteSubCompPaths"; + +type Scan = (source: string) => Iterable>>; + +const SCANS: Array<[Scan, RegExp]> = [ + [cssUrlMatches, /\burl\(\s*(["']?)([^)"'\s](?:[^)"']*[^)"'\s])?)\1\s*\)/g], + [rawCssUrlMatches, /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g], + [ + cssImportMatches, + /@import\s+(?:url\(\s*(["']?)([^)"']+)\1\s*\)|(["'])([^"']+)\3)\s*([^;]*);\s*/g, + ], + [urlHashRefMatches, /(url\(\s*)(["']?)#([^"')\s]+)\2(\s*\))/gi], + [sceneCssUrlMatches, /url\(\s*(['"]?)(.*?)\1\s*\)|@import\s+(['"])(.*?)\3/gi], + [importRuleMatches, /@import\s+url\([^)]*\)\s*;|@import\s+["'][^"']+["']\s*;/gi], + [ + remoteBackgroundUrlMatches, + /background(?:-image)?\s*:\s*[^;}"']*?url\(\s*["']?(https?:\/\/[^"')]+)["']?\s*\)/gi, + ], + [remoteFontFaceUrlMatches, /url\(["']?(https?:\/\/[^"')]+)["']?\)/gi], + [localFontFaceUrlMatches, /url\(["']?(?!data:|file:|https?:\/\/)([^"')]+)["']?\)/gi], +]; +const BACKGROUND_IMAGE_DECL = + /(?:^|;|\{)\s*background(?:-image)?\s*:[^;}]*(?:\bgradient\s*\(|url\s*\()/i; + +const TOKENS = [ + "url(", + "URL(", + "uRl(", + "xurl(", + "url", + "@import", + "@IMPORT ", + "@import ", + "@import url(", + "@import 'a", + '@import "b c"', + "@import\turl( '", + "background", + "BACKGROUND-IMAGE", + "-image", + "background:", + "background: url(", + "background-image:url( 'https://", + " url(http://a", + "url(https://", + "url('http://x", + "url(#a)", + "url(", + "gradient(", + "xgradient(", + "-gradient (", + "http://", + "HTTPS://", + "https:/", + "data:", + "file:", + "#", + "#a", + '"', + "'", + ")", + "(", + ";", + "{", + "}", + ":", + " ", + " ", + "\n", + "\t", + "\r", + "\u2028", + "\u00a0", + "a", + "b c", + "_", + "\\", + "-", + "/x.png", +]; + +function pages(count: number): string[] { + let seed = 7; + const next = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648; + return Array.from({ length: count }, () => + Array.from( + { length: 1 + Math.floor(next() * 24) }, + () => TOKENS[Math.floor(next() * TOKENS.length)], + ).join(""), + ); +} + +function disagreements(page: string): string[] { + const out: string[] = []; + for (const [scan, pattern] of SCANS) { + const scanned = [...scan(page)].map((m) => [m.index, ...m.parts]); + const matched = [...page.matchAll(pattern)].map((m) => [m.index, ...m]); + if (!isDeepStrictEqual(scanned, matched)) out.push(`${scan.name}: ${JSON.stringify(page)}`); + } + if (hasBackgroundImageDeclaration(page) !== BACKGROUND_IMAGE_DECL.test(page)) + out.push(`hasBackgroundImageDeclaration: ${JSON.stringify(page)}`); + return out; +} + +describe("CSS url scans", () => { + it("find every match the regexes they replace find, groups included", () => { + expect(pages(20_000).flatMap(disagreements)).toEqual([]); + }); + + it("find quoted urls with parentheses, the other quote, spaces, escapes and an empty url( )", () => { + const css = `a{b:url( "x (1).png" )} c{d:url('say "hi"')} e{f:url(\\(g\\))} h{i:url( )}`; + for (const [scan, pattern] of SCANS.slice(0, 2)) { + expect([...scan(css)].map((m) => m.parts)).toEqual( + [...css.matchAll(pattern)].map((m) => [...m]), + ); + } + }); + + it.each([ + "{background:xgradient(1px)}", + "{background:-gradient (1px)}", + "a;background-image : url (x)", + "{background:red;url(x)}", + ])("tell a background image in %j as the regex does", (css) => { + expect(hasBackgroundImageDeclaration(css)).toBe(BACKGROUND_IMAGE_DECL.test(css)); + }); + + it("replace like String.prototype.replace with a function", () => { + const css = `a{b:url(x.png)} c{d:url( 'y.png' )}`; + const replace = (full: string, quote: string, url: string) => `url(${quote}<${url}>${quote})`; + expect(replaceMatches(css, cssUrlMatches(css), replace)).toBe( + css.replace(SCANS[0]![1], replace), + ); + }); + + // At 1 MB each regex above runs for minutes or more on these pages; the scans take milliseconds. + it.each([ + "url(", + "url( ", + "url(a ", + "url('", + "url(#a", + "@import url(", + "@import 'a", + "{background:", + "background:url(http://", + "url(http://", + "url(a", + " ", + ])("finish a 1 MB page of unclosed %j in well under a second", (unit) => { + const page = unit.repeat(Math.floor(1_000_000 / unit.length)); + const started = performance.now(); + for (const [scan] of SCANS) Array.from(scan(page)); + hasBackgroundImageDeclaration(page); + expect(performance.now() - started).toBeLessThan(1000); + }); + + it("let rewriteCssAssetUrls finish a 1 MB page of unclosed url( in well under a second", () => { + const css = "url(".repeat(250_000); + const started = performance.now(); + expect(rewriteCssAssetUrls(css, "compositions/scene.html")).toBe(css); + expect(performance.now() - started).toBeLessThan(1000); + }); +}); diff --git a/packages/parsers/src/cssUrlScans.ts b/packages/parsers/src/cssUrlScans.ts new file mode 100644 index 00000000000..36c4f6eb103 --- /dev/null +++ b/packages/parsers/src/cssUrlScans.ts @@ -0,0 +1,396 @@ +// Forward scans that find what the CSS `url(` regex in each doc comment finds, in linear time. + +/** One match as `matchAll` reports it: where it starts, then the full text and each group. */ +export interface ScanMatch> { + index: number; + parts: [string, ...G]; +} + +type Finder = (start: number) => number; +type UrlArgument = { end: number; quote: string; url: string }; +type UrlParts = [quote: string, url: string]; +type ImportParts = [ + urlQuote: string | undefined, + url: string | undefined, + quote: string | undefined, + path: string | undefined, + media: string, +]; +type HashRefParts = [pre: string, quote: string, id: string, post: string]; +type SceneUrlParts = [ + urlQuote: string | undefined, + url: string | undefined, + quote: string | undefined, + path: string | undefined, +]; + +const WS = /\s/; +const WORD = /\w/; +const BACKGROUND = /background/iy; +const IMAGE_SUFFIX = /-image/iy; +const URL_OPEN = /url\(/iy; +const REMOTE_SCHEME = /https?:\/\//iy; +const NON_LOCAL_SCHEME = /data:|file:|https?:\/\//iy; + +const isWs = (char: string | undefined): boolean => char !== undefined && WS.test(char); +const isQuote = (char: string | undefined): boolean => char === '"' || char === "'"; + +function endOfMatchAt(pattern: RegExp, source: string, index: number): number { + pattern.lastIndex = index; + return pattern.test(source) ? pattern.lastIndex : -1; +} + +/** Caches `search` so nondecreasing starts never rescan what an earlier call already passed. */ +function memoForward(search: Finder): Finder { + let from = 0; + let at = -1; + return (start) => { + if (start < from || start > at) { + from = start; + at = search(start); + } + return at; + }; +} + +/** The first index at or after a start where the global `pattern` matches, else `source.length`. */ +function finder(source: string, pattern: RegExp): Finder { + return memoForward((start) => { + pattern.lastIndex = start; + return pattern.exec(source)?.index ?? source.length; + }); +} + +function firstAccepted(source: string, candidates: Finder, accept: (i: number) => boolean): Finder { + return memoForward((start) => { + let i = candidates(start); + while (i < source.length && !accept(i)) i = candidates(i + 1); + return i; + }); +} + +function urlFinders(source: string) { + return { + ws: finder(source, /\S/g), + stop: finder(source, /[)"']/g), + close: finder(source, /\S/g), + }; +} +type UrlFinders = ReturnType; + +function quotedArgument( + source: string, + w: number, + f: UrlFinders, + trimmed: boolean, +): UrlArgument | null { + const quote = source[w]!; + const e = f.stop(w + 1); + if (e === w + 1 || source[e] !== quote) return null; + if (trimmed && (isWs(source[w + 1]) || isWs(source[e - 1]))) return null; + const z = f.close(e + 1); + return source[z] === ")" ? { end: z + 1, quote, url: source.slice(w + 1, e) } : null; +} + +/** `\s*(["']?)([^)"']+)\1\s*\)` from `start`. */ +function rawUrlArgument(source: string, start: number, f: UrlFinders): UrlArgument | null { + const w = f.ws(start); + if (isQuote(source[w])) return quotedArgument(source, w, f, false); + if (source[w] === ")") return w > start ? { end: w + 1, quote: "", url: source[w - 1]! } : null; + const e = f.stop(w); + return source[e] === ")" ? { end: e + 1, quote: "", url: source.slice(w, e) } : null; +} + +/** `\s*(["']?)([^)"'\s](?:[^)"']*[^)"'\s])?)\1\s*\)` from `start`. */ +function trimmedUrlArgument(source: string, start: number, f: UrlFinders): UrlArgument | null { + const w = f.ws(start); + if (isQuote(source[w])) return quotedArgument(source, w, f, true); + const e = f.stop(w); + if (source[w] === ")" || source[e] !== ")") return null; + let j = e; + while (isWs(source[j - 1])) j--; + return { end: e + 1, quote: "", url: source.slice(w, j) }; +} + +function* urlMatches( + source: string, + argument: (source: string, start: number, f: UrlFinders) => UrlArgument | null, +): Generator> { + const opener = /\burl\(/g; + const f = urlFinders(source); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const arg = argument(source, open.index + 4, f); + if (!arg) continue; + yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.quote, arg.url] }; + opener.lastIndex = arg.end; + } +} + +/** Matches of `/\burl\(\s*(["']?)([^)"'\s](?:[^)"']*[^)"'\s])?)\1\s*\)/g`. */ +export const cssUrlMatches = (source: string) => urlMatches(source, trimmedUrlArgument); + +/** Matches of `/\burl\(\s*(["']?)([^)"']+)\1\s*\)/g`. */ +export const rawCssUrlMatches = (source: string) => urlMatches(source, rawUrlArgument); + +type Replacer = M extends ScanMatch ? (full: string, ...groups: G) => string : never; + +/** `source.replace(pattern, replace)` for the matches a scan found in `source`. */ +export function replaceMatches>>( + source: string, + matches: Iterable, + replace: Replacer, +): string { + const call = replace as (...parts: M["parts"]) => string; + let out = ""; + let kept = 0; + for (const { index, parts } of matches) { + out += source.slice(kept, index) + call(...parts); + kept = index + parts[0].length; + } + return out + source.slice(kept); +} + +function importTarget(source: string, w: number, f: UrlFinders, quoteStop: Finder) { + if (source.startsWith("url(", w)) { + const arg = rawUrlArgument(source, w + 4, f); + return arg && { end: arg.end, groups: [arg.quote, arg.url, undefined, undefined] as const }; + } + const quote = source[w]; + if (!isQuote(quote)) return null; + const e = quoteStop(w + 1); + if (e === w + 1 || source[e] !== quote) return null; + return { end: e + 1, groups: [undefined, undefined, quote, source.slice(w + 1, e)] as const }; +} + +/** Matches of `/@import\s+(?:url\(\s*(["']?)([^)"']+)\1\s*\)|(["'])([^"']+)\3)\s*([^;]*);\s*\/g`. */ +export function* cssImportMatches(source: string): Generator> { + const opener = /@import/g; + const ws = finder(source, /\S/g); + const quoteStop = finder(source, /["']/g); + const semicolon = finder(source, /;/g); + const tail = finder(source, /\S/g); + const f = urlFinders(source); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const w = ws(open.index + 7); + const target = w > open.index + 7 ? importTarget(source, w, f, quoteStop) : null; + if (!target) continue; + const media = tail(target.end); + const semi = semicolon(media); + if (semi === source.length) continue; + const end = tail(semi + 1); + const full = source.slice(open.index, end); + yield { index: open.index, parts: [full, ...target.groups, source.slice(media, semi)] }; + opener.lastIndex = end; + } +} + +/** Matches of `/(url\(\s*)(["']?)#([^"')\s]+)\2(\s*\))/gi`. */ +export function* urlHashRefMatches(source: string): Generator> { + const opener = /url\(/gi; + const ws = finder(source, /\S/g); + const idEnd = finder(source, /[\s"')]/g); + const close = finder(source, /\S/g); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const w = ws(open.index + 4); + const quote = isQuote(source[w]) ? source[w]! : ""; + const hash = w + quote.length; + const e = source[hash] === "#" ? idEnd(hash + 1) : hash + 1; + if (e === hash + 1 || (quote && source[e] !== quote)) continue; + const post = e + quote.length; + const z = close(post); + if (source[z] !== ")") continue; + const full = source.slice(open.index, z + 1); + const id = source.slice(hash + 1, e); + yield { + index: open.index, + parts: [full, source.slice(open.index, w), quote, id, source.slice(post, z + 1)], + }; + opener.lastIndex = z + 1; + } +} + +function sceneFinders(source: string) { + const closeWs = finder(source, /\S/g); + const closer = (quote: string) => + firstAccepted( + source, + finder(source, new RegExp(quote, "g")), + (j) => source[closeWs(j + 1)] === ")", + ); + let runEnd = -1; + let runStart = -1; + const wsRunStart = (end: number) => { + if (end !== runEnd) { + for (runEnd = runStart = end; isWs(source[runStart - 1]); ) runStart--; + } + return runStart; + }; + return { + ws: finder(source, /\S/g), + lineEnd: finder(source, /[\n\r\u2028\u2029]/g), + paren: finder(source, /\)/g), + close: finder(source, /\S/g), + closer: { '"': closer('"'), "'": closer("'") } as Record, + quote: { '"': finder(source, /"/g), "'": finder(source, /'/g) } as Record, + wsRunStart, + }; +} +type SceneFinders = ReturnType; +type SceneMatch = { end: number; groups: SceneUrlParts }; + +function sceneUrlAt(source: string, open: number, f: SceneFinders): SceneMatch | null { + const w = f.ws(open + 4); + const quote = source[w]!; + const line = f.lineEnd(w); + const closing = isQuote(quote) ? f.closer[quote]!(w + 1) : line; + if (closing < line) { + const groups: SceneUrlParts = [quote, source.slice(w + 1, closing), undefined, undefined]; + return { end: f.close(closing + 1) + 1, groups }; + } + const paren = f.paren(w); + if (paren === source.length) return null; + const bodyEnd = Math.max(w, f.wsRunStart(paren)); + if (bodyEnd > line) return null; + return { end: paren + 1, groups: ["", source.slice(w, bodyEnd), undefined, undefined] }; +} + +function sceneImportAt(source: string, open: number, f: SceneFinders): SceneMatch | null { + const w = f.ws(open + 7); + const quote = source[w]!; + if (w === open + 7 || !isQuote(quote)) return null; + const closing = f.quote[quote]!(w + 1); + if (closing >= f.lineEnd(w + 1)) return null; + return { end: closing + 1, groups: [undefined, undefined, quote, source.slice(w + 1, closing)] }; +} + +/** Matches of `/url\(\s*(['"]?)(.*?)\1\s*\)|@import\s+(['"])(.*?)\3/gi`. */ +export function* sceneCssUrlMatches(source: string): Generator> { + const opener = /url\(|@import/gi; + const f = sceneFinders(source); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const at = open[0].length === 4 ? sceneUrlAt : sceneImportAt; + const match = at(source, open.index, f); + if (!match) continue; + yield { index: open.index, parts: [source.slice(open.index, match.end), ...match.groups] }; + opener.lastIndex = match.end; + } +} + +/** Where the `url(…)` or quoted target of `@import\s+` at `w` closes, or -1. */ +function importRuleClose(source: string, w: number, paren: Finder, quote: Finder): number { + const url = endOfMatchAt(URL_OPEN, source, w); + if (url >= 0) return paren(url) < source.length ? paren(url) : -1; + if (!isQuote(source[w])) return -1; + const close = quote(w + 1); + return close > w + 1 && close < source.length ? close : -1; +} + +/** Matches of `/@import\s+url\([^)]*\)\s*;|@import\s+["'][^"']+["']\s*;/gi`. */ +export function* importRuleMatches(source: string): Generator> { + const opener = /@import/gi; + const ws = finder(source, /\S/g); + const paren = finder(source, /\)/g); + const quote = finder(source, /["']/g); + const tail = finder(source, /\S/g); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const w = ws(open.index + 7); + const close = w > open.index + 7 ? importRuleClose(source, w, paren, quote) : -1; + const z = close < 0 ? -1 : tail(close + 1); + if (source[z] !== ";") continue; + yield { index: open.index, parts: [source.slice(open.index, z + 1)] }; + opener.lastIndex = z + 1; + } +} + +/** Where `background(-image)?\s*:` at `at` ends past its colon, or -1. */ +function backgroundValueStart(source: string, at: number, ws: Finder): number { + let k = endOfMatchAt(BACKGROUND, source, at); + if (k < 0) return -1; + k = Math.max(k, endOfMatchAt(IMAGE_SUFFIX, source, k)); + const colon = ws(k); + return source[colon] === ":" ? colon + 1 : -1; +} + +/** `/(?:^|;|\{)\s*background(?:-image)?\s*:[^;}]*(?:\bgradient\s*\(|url\s*\()/i.test(source)`. */ +export function hasBackgroundImageDeclaration(source: string): boolean { + const ws = finder(source, /\S/g); + const colonWs = finder(source, /\S/g); + const valueEnd = finder(source, /[;}]/g); + const tokenWs = finder(source, /\S/g); + const imageValue = firstAccepted(source, finder(source, /gradient|url/gi), (i) => { + const gradient = source[i] === "g" || source[i] === "G"; + if (gradient && WORD.test(source[i - 1] ?? "")) return false; + return source[tokenWs(i + (gradient ? 8 : 3))] === "("; + }); + const opener = /[;{]/g; + for (let start: number | null = 0; start !== null; ) { + const value = backgroundValueStart(source, ws(start), colonWs); + if (value >= 0 && imageValue(value) < valueEnd(value)) return true; + const next = opener.exec(source); + start = next ? next.index + 1 : null; + } + return false; +} + +/** `\s*["']?(BODY)["']?\s*\)` from `start`, whitespace only with `f.ws`, BODY from `head` on as `[^"')]+`. */ +function bareUrlArgument( + source: string, + start: number, + f: { ws?: Finder; stop: Finder; close?: Finder }, + head: (at: number) => number, +): UrlArgument | null { + const w = f.ws ? f.ws(start) : start; + const from = isQuote(source[w]) ? w + 1 : w; + const body = head(from); + const e = body < 0 ? body : f.stop(body); + if (e <= body) return null; + const close = isQuote(source[e]) ? (f.close ? f.close(e + 1) : e + 1) : e; + return source[close] === ")" ? { end: close + 1, quote: "", url: source.slice(from, e) } : null; +} + +const remoteHead = (source: string) => (at: number) => endOfMatchAt(REMOTE_SCHEME, source, at); +const localHead = (source: string) => (at: number) => + endOfMatchAt(NON_LOCAL_SCHEME, source, at) < 0 ? at : -1; + +/** Matches of `/background(?:-image)?\s*:\s*[^;}"']*?url\(\s*["']?(https?:\/\/[^"')]+)["']?\s*\)/gi`. */ +export function* remoteBackgroundUrlMatches(source: string): Generator> { + const opener = /background/gi; + const ws = finder(source, /\S/g); + const valueEnd = finder(source, /[;}"']/g); + const f = urlFinders(source); + const head = remoteHead(source); + const urlAt = (open: number) => bareUrlArgument(source, open + 4, f, head); + const firstUrl = firstAccepted(source, finder(source, /url\(/gi), (i) => urlAt(i) !== null); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const value = backgroundValueStart(source, open.index, ws); + if (value < 0) continue; + const start = ws(value); + const url = firstUrl(start); + const arg = url < valueEnd(start) ? urlAt(url) : null; + if (!arg) continue; + yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.url] }; + opener.lastIndex = arg.end; + } +} + +function* fontFaceUrlMatches( + source: string, + head: (at: number) => number, +): Generator> { + const opener = /url\(/gi; + const f = { stop: finder(source, /[)"']/g) }; + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const arg = bareUrlArgument(source, open.index + 4, f, head); + if (!arg) continue; + yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.url] }; + opener.lastIndex = arg.end; + } +} + +/** Matches of `/url\(["']?(https?:\/\/[^"')]+)["']?\)/gi`. */ +export const remoteFontFaceUrlMatches = (source: string) => + fontFaceUrlMatches(source, remoteHead(source)); + +/** Matches of `/url\(["']?(?!data:|file:|https?:\/\/)([^"')]+)["']?\)/gi`. */ +export const localFontFaceUrlMatches = (source: string) => + fontFaceUrlMatches(source, localHead(source)); diff --git a/packages/parsers/src/rewriteSubCompPaths.ts b/packages/parsers/src/rewriteSubCompPaths.ts index 618d8f256af..167ce71f559 100644 --- a/packages/parsers/src/rewriteSubCompPaths.ts +++ b/packages/parsers/src/rewriteSubCompPaths.ts @@ -16,11 +16,11 @@ // submodule so Windows builds don't emit backslash-separated paths (or worse, // drive-letter-prefixed artifacts from `resolve("/", ...)`). import { posix } from "path"; +import { cssUrlMatches, replaceMatches } from "./cssUrlScans.js"; import { rewriteSrcsetUrls } from "./srcset.js"; const { join, resolve, dirname } = posix; import { - CSS_URL_RE, PATH_ATTRS, isNonRelativeUrl, splitUrlSuffix, @@ -154,7 +154,7 @@ export function rewriteCssAssetUrls( assetExists?: AssetExists, ): string { if (!cssText) return cssText; - return cssText.replace(CSS_URL_RE, (full, quote: string, rawUrl: string) => { + return replaceMatches(cssText, cssUrlMatches(cssText), (full, quote, rawUrl) => { const urlValue = (rawUrl || "").trim(); const rewritten = rewriteAssetPath(compSrcPath, urlValue, assetExists); if (rewritten === urlValue) return full; diff --git a/packages/parsers/tsup.config.ts b/packages/parsers/tsup.config.ts index ae978755aba..04ffdc153f8 100644 --- a/packages/parsers/tsup.config.ts +++ b/packages/parsers/tsup.config.ts @@ -12,6 +12,7 @@ export default defineConfig({ gsapParser: "src/gsapParser.ts", slideshow: "src/slideshow/index.ts", assets: "src/assets.ts", + cssUrlScans: "src/cssUrlScans.ts", composition: "src/composition.ts", compositionContract: "src/compositionContract.ts", compositionDuration: "src/compositionDuration.ts", diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index f9c03c3aa50..67a5ade472f 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -28,7 +28,7 @@ "unpacked": 737342 }, "@hyperframes/parsers": { - "packed": 203731, + "packed": 208000, "unpacked": 885314 }, "@hyperframes/player": { From 04c68e5d45498bdbb5d8fec3226452a11aeaa8f6 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 14 Nov 2026 21:33:32 -0800 Subject: [PATCH 02/13] perf(core): bundler, SVG id namespacing and runtime find CSS urls in linear time The bundler's url() inlining, CSS-file rebasing and @import inlining and hoisting, the SVG url(#id) rewrite, the runtime's sub-composition url() rewrite and the scene swap's url and @import list now use the @hyperframes/parsers css-url-scans entry. Each returns the same matches as the regex it replaces, in time linear in the CSS length. --- packages/core/src/compiler/htmlBundler.ts | 42 ++++++++++--------- .../core/src/compiler/svgIdNamespacing.ts | 36 ++++++---------- .../core/src/runtime/compositionLoader.ts | 4 +- packages/core/src/runtime/init.ts | 4 +- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 867ecbb3746..684df137e43 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -41,7 +41,13 @@ import { decodedUrlPath, splitUrlSuffix, } from "@hyperframes/parsers/asset-paths"; -import { CSS_URL_RE, isNonRelativeUrl } from "./assetPaths.js"; +import { + cssImportMatches, + cssUrlMatches, + rawCssUrlMatches, + replaceMatches, +} from "@hyperframes/parsers/css-url-scans"; +import { isNonRelativeUrl } from "./assetPaths.js"; import { rewriteSrcsetUrls } from "./srcset.js"; import { compileHtml, type MediaDurationProber } from "./htmlCompiler"; import { @@ -146,9 +152,6 @@ function safeReadFile(filePath: string): string | null { } } -const CSS_IMPORT_RE = - /@import\s+(?:url\(\s*(["']?)([^)"']+)\1\s*\)|(["'])([^"']+)\3)\s*([^;]*);\s*/g; - const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g; function withCommentsStripped( @@ -180,7 +183,7 @@ function rebaseCssUrls(css: string, cssFileDir: string, projectDir: string): str const resolvedRoot = resolve(projectDir); const resolvedDir = resolve(cssFileDir); if (resolvedDir === resolvedRoot) return css; - return css.replace(CSS_URL_RE, (full, quote: string, urlValue: string) => { + return replaceMatches(css, cssUrlMatches(css), (full, quote, urlValue) => { const decoded = decodeCssEscapes(urlValue); if (!decoded || !isRelativeUrl(decoded)) return full; const { basePath, suffix } = splitUrlSuffix(decoded); @@ -277,8 +280,9 @@ function inlineCssFile( ): string { const { result: strippedCss, restore: restoreComments } = withCommentsStripped(css, (s) => s); const importPlaceholders: string[] = []; - const withPlaceholders = strippedCss.replace( - CSS_IMPORT_RE, + const withPlaceholders = replaceMatches( + strippedCss, + cssImportMatches(strippedCss), (full, _q1, urlPath, _q2, barePath, mediaQuery) => { const importPath = urlPath ?? barePath; if (!importPath || !isRelativeUrl(importPath)) return full; @@ -507,14 +511,11 @@ function rewriteColorGradingLutWithInlinedAssets(value: string, inlineAsset: Ass function rewriteCssUrlsWithInlinedAssets(cssText: string, inlineAsset: AssetInliner): string { if (!cssText) return cssText; - return cssText.replace( - /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g, - (_full, quote: string, rawUrl: string) => { - const maybeInlined = inlineAsset((rawUrl || "").trim()); - if (!maybeInlined) return _full; - return `url(${quote || ""}${maybeInlined}${quote || ""})`; - }, - ); + return replaceMatches(cssText, rawCssUrlMatches(cssText), (_full, quote, rawUrl) => { + const maybeInlined = inlineAsset((rawUrl || "").trim()); + if (!maybeInlined) return _full; + return `url(${quote || ""}${maybeInlined}${quote || ""})`; + }); } /** @@ -716,7 +717,8 @@ function joinCssHoistingImports(sheets: string[]): string { const imports = new Set(); const cssParts: string[] = []; for (const sheet of sheets) { - const rest = sheet.trim().replace(CSS_IMPORT_RE, (match) => { + const trimmed = sheet.trim(); + const rest = replaceMatches(trimmed, cssImportMatches(trimmed), (match) => { imports.add(match.trim()); return ""; }); @@ -730,9 +732,11 @@ function placeSceneStylesLikeRender(document: Document): void { const styles = [...document.querySelectorAll("head style")]; const imports = new Set(); for (const el of styles) { - el.textContent = (el.textContent || "") - .replace(CSS_IMPORT_RE, (match) => (imports.add(match.trim()), "")) - .trim(); + const css = el.textContent || ""; + el.textContent = replaceMatches(css, cssImportMatches(css), (match) => { + imports.add(match.trim()); + return ""; + }).trim(); } styles.slice(1).reduce((previous, el) => (previous.after(el), el), styles[0]!); if (imports.size === 0) return; diff --git a/packages/core/src/compiler/svgIdNamespacing.ts b/packages/core/src/compiler/svgIdNamespacing.ts index 95940b8eaa4..4016e6733b5 100644 --- a/packages/core/src/compiler/svgIdNamespacing.ts +++ b/packages/core/src/compiler/svgIdNamespacing.ts @@ -6,6 +6,7 @@ * data-hf-authored-id for script lookup compatibility. */ +import { replaceMatches, urlHashRefMatches } from "@hyperframes/parsers/css-url-scans"; import postcss from "postcss"; import { escapeCssIdentifier } from "./selectorIdTokens"; import { @@ -22,21 +23,6 @@ const ID_ATTR = "id"; * generalized from "the composition root" to any renamed descendant. */ export const SVG_AUTHORED_ID_ATTR = "data-hf-authored-id"; -/** - * Matches a `url(#id)` funcref value — quoted or bare — as used by - * `clip-path`, `filter`, `mask`, `fill`, `stroke`, `marker-start/mid/end`, - * `cursor`, `mask-image`, and equally by any of those written into an inline - * `style` attribute or a `", + "", + "@font-face", + "@FONT-FACE {", + "/*", + "*/", + "/*/", ]; function pages(count: number): string[] { @@ -163,6 +191,12 @@ describe("CSS url scans", () => { "url(http://", "url(a", " ", + "mask-image:url(", + "mask-image:", + "", + "@font-face{", + "/* ", ])("finish a 1 MB page of unclosed %j in well under a second", (unit) => { const page = unit.repeat(Math.floor(1_000_000 / unit.length)); const started = performance.now(); diff --git a/packages/parsers/src/cssUrlScans.ts b/packages/parsers/src/cssUrlScans.ts index 36c4f6eb103..c3dd14cf28d 100644 --- a/packages/parsers/src/cssUrlScans.ts +++ b/packages/parsers/src/cssUrlScans.ts @@ -1,4 +1,4 @@ -// Forward scans that find what the CSS `url(` regex in each doc comment finds, in linear time. +// Forward scans that find what the CSS regex in each doc comment finds, in linear time. /** One match as `matchAll` reports it: where it starts, then the full text and each group. */ export interface ScanMatch> { @@ -394,3 +394,143 @@ export const remoteFontFaceUrlMatches = (source: string) => /** Matches of `/url\(["']?(?!data:|file:|https?:\/\/)([^"')]+)["']?\)/gi`. */ export const localFontFaceUrlMatches = (source: string) => fontFaceUrlMatches(source, localHead(source)); + +type BareOrQuotedParts = [ + double: string | undefined, + single: string | undefined, + bare: string | undefined, +]; +type BareOrQuotedArgument = { end: number; groups: BareOrQuotedParts }; + +function bareOrQuotedFinders(source: string) { + return { + ws: finder(source, /\S/g), + quote: { '"': finder(source, /"/g), "'": finder(source, /'/g) } as Record, + bareEnd: finder(source, /[\s"')]/g), + quotedClose: finder(source, /\S/g), + bareClose: finder(source, /\S/g), + }; +} +type BareOrQuotedFinders = ReturnType; + +/** `\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)` from `start`. */ +function bareOrQuotedArgument( + source: string, + start: number, + f: BareOrQuotedFinders, +): BareOrQuotedArgument | null { + const w = f.ws(start); + const quote = source[w]!; + if (isQuote(quote)) { + const e = f.quote[quote]!(w + 1); + if (e === w + 1 || e === source.length) return null; + const body = source.slice(w + 1, e); + const z = f.quotedClose(e + 1); + const groups: BareOrQuotedParts = + quote === '"' ? [body, undefined, undefined] : [undefined, body, undefined]; + return source[z] === ")" ? { end: z + 1, groups } : null; + } + const e = f.bareEnd(w); + const z = e === w ? -1 : f.bareClose(e); + return source[z] === ")" + ? { end: z + 1, groups: [undefined, undefined, source.slice(w, e)] } + : null; +} + +/** Matches of `/\burl\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi`. */ +export function* bareOrQuotedCssUrlMatches( + source: string, +): Generator> { + const opener = /\burl\(/gi; + const f = bareOrQuotedFinders(source); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const arg = bareOrQuotedArgument(source, open.index + 4, f); + if (!arg) continue; + yield { index: open.index, parts: [source.slice(open.index, arg.end), ...arg.groups] }; + opener.lastIndex = arg.end; + } +} + +/** The last `url(…)` that matches before each nondecreasing limit, as a backtracking `[^;{}]*` finds it. */ +function lastUrlBefore(source: string) { + const f = bareOrQuotedFinders(source); + let acceptedArg: BareOrQuotedArgument | null = null; + const nextUrl = firstAccepted(source, finder(source, /url\(/gi), (i) => { + acceptedArg = bareOrQuotedArgument(source, i + 4, f); + return acceptedArg !== null; + }); + let last: { at: number; arg: BareOrQuotedArgument } | null = null; + let next = nextUrl(0); + let nextArg = acceptedArg as BareOrQuotedArgument | null; + return (limit: number) => { + while (next < limit && nextArg) { + last = { at: next, arg: nextArg }; + next = nextUrl(next + 1); + nextArg = acceptedArg; + } + return last; + }; +} + +/** Matches of `/\b(?:-webkit-)?mask-image\s*:\s*[^;{}]*url\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi`. */ +export function* maskImageUrlMatches(source: string): Generator> { + const opener = /\b(?:-webkit-)?mask-image/gi; + const ws = finder(source, /\S/g); + const valueEnd = finder(source, /[;{}]/g); + const urlBefore = lastUrlBefore(source); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const colon = ws(opener.lastIndex); + const value = source[colon] === ":" ? ws(colon + 1) : -1; + const url = value < 0 ? null : urlBefore(valueEnd(value)); + if (!url || url.at < value) continue; + const { end, groups } = url.arg; + yield { index: open.index, parts: [source.slice(open.index, end), ...groups] }; + opener.lastIndex = end; + } +} + +/** Matches of `/]*>([\s\S]*?)<\/style>/gi`. */ +export function* styleBlockMatches(source: string): Generator> { + const opener = //g); + const closer = finder(source, /<\/style>/gi); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const body = tagEnd(open.index + 6) + 1; + const close = body > source.length ? source.length : closer(body); + if (close === source.length) return; + yield { + index: open.index, + parts: [source.slice(open.index, close + 8), source.slice(body, close)], + }; + opener.lastIndex = close + 8; + } +} + +/** Matches of `/@font-face\s*\{([^}]*)\}/gi`. */ +export function* fontFaceBlockMatches(source: string): Generator> { + const opener = /@font-face/gi; + const ws = finder(source, /\S/g); + const brace = finder(source, /\}/g); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const w = ws(open.index + 10); + const close = source[w] === "{" ? brace(w + 1) : source.length; + if (close === source.length) continue; + yield { + index: open.index, + parts: [source.slice(open.index, close + 1), source.slice(w + 1, close)], + }; + opener.lastIndex = close + 1; + } +} + +/** Matches of `/\/\*[\s\S]*?\*\//g`. */ +export function* blockCommentMatches(source: string): Generator> { + const opener = /\/\*/g; + const closer = finder(source, /\*\//g); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const close = closer(open.index + 2); + if (close === source.length) return; + yield { index: open.index, parts: [source.slice(open.index, close + 2)] }; + opener.lastIndex = close + 2; + } +} From 2ac8430eb4314c5cb0c34d08bbb94eb02d8ce1a1 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 14 Nov 2026 21:52:57 -0800 Subject: [PATCH 05/13] perf(core): strip and restore stylesheet comments in linear time Bundling a stylesheet finds its comments with the parsers comment scan and puts them back in one pass over the placeholders. A comment comes back verbatim, including text such as `$'` that a string replacement pattern would expand. --- .../compiler/htmlBundler.cssComments.test.ts | 33 +++++++++++++++++++ packages/core/src/compiler/htmlBundler.ts | 22 +++++-------- 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 packages/core/src/compiler/htmlBundler.cssComments.test.ts diff --git a/packages/core/src/compiler/htmlBundler.cssComments.test.ts b/packages/core/src/compiler/htmlBundler.cssComments.test.ts new file mode 100644 index 00000000000..f8d7bbc87e3 --- /dev/null +++ b/packages/core/src/compiler/htmlBundler.cssComments.test.ts @@ -0,0 +1,33 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import { bundleToSingleHtml } from "./htmlBundler"; + +async function bundleWithStylesheet(css: string): Promise { + const dir = mkdtempSync(join(tmpdir(), "hf-css-comments-")); + try { + writeFileSync(join(dir, "style.css"), css); + writeFileSync( + join(dir, "index.html"), + '
', + ); + return await bundleToSingleHtml(dir, { runtime: "placeholder" }); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +} + +describe("stylesheet comments in the bundle", () => { + it("keep their text verbatim, replacement patterns included", async () => { + const css = "/* $' $& $$ */ .a { color: red } /* b */"; + expect(await bundleWithStylesheet(css)).toContain(css); + }); + + it("bundle 400 KB of comments and unclosed comment openers in well under two seconds", async () => { + const css = "/**/".repeat(50_000) + "/* ".repeat(66_000); + const started = performance.now(); + expect(await bundleWithStylesheet(css)).toContain("/**//**/"); + expect(performance.now() - started).toBeLessThan(2000); + }); +}); diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 684df137e43..5aefecc0b9f 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -42,6 +42,7 @@ import { splitUrlSuffix, } from "@hyperframes/parsers/asset-paths"; import { + blockCommentMatches, cssImportMatches, cssUrlMatches, rawCssUrlMatches, @@ -152,26 +153,21 @@ function safeReadFile(filePath: string): string | null { } } -const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g; +const COMMENT_PLACEHOLDER_RE = /\/\*__hf_c\d+__\*\//g; function withCommentsStripped( css: string, fn: (stripped: string) => T, ): { result: T; restore: (s: string) => string } { - const comments: string[] = []; - const stripped = css.replace(CSS_COMMENT_RE, (m) => { - const idx = comments.length; - comments.push(m); - return `/*__hf_c${idx}__*/`; + const comments = new Map(); + const stripped = replaceMatches(css, blockCommentMatches(css), (m) => { + const placeholder = `/*__hf_c${comments.size}__*/`; + comments.set(placeholder, m); + return placeholder; }); const result = fn(stripped); - const restore = (s: string) => { - let out = s; - for (let i = 0; i < comments.length; i++) { - out = out.replace(`/*__hf_c${i}__*/`, comments[i]!); - } - return out; - }; + const restore = (s: string) => + s.replace(COMMENT_PLACEHOLDER_RE, (placeholder) => comments.get(placeholder) ?? placeholder); return { result, restore }; } From 2fa653ca0ff287fe009355ead4f485330788667f Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 14 Nov 2026 21:53:05 -0800 Subject: [PATCH 06/13] perf(producer): find style and font-face blocks in linear time Remote font discovery and local font embedding read `
`; + const started = performance.now(); + expect(detectAncestorBackgroundImage(html)).toBe(false); + expect(performance.now() - started).toBeLessThan(1000); + }); +}); + +/** The depth-counting extraction the linear one replaces. */ +function rescanningFontFaceBlocks(css: string): string[] { + const blocks: string[] = []; + const atRule = /@font-face\s*/gi; + for (let m = atRule.exec(css); m; m = atRule.exec(css)) { + const openIdx = css.indexOf("{", m.index + m[0].length); + if (openIdx === -1) break; + let depth = 1; + let i = openIdx + 1; + for (; i < css.length && depth > 0; i++) { + if (css[i] === "{") depth++; + else if (css[i] === "}") depth--; + } + if (depth === 0) { + blocks.push(css.slice(m.index, i)); + atRule.lastIndex = i; + } + } + return blocks; +} + +const PIECES = [ + "@font-face", + "@FONT-FACE ", + "@font-face{", + "{", + "}", + " ", + "\n", + "a", + "src:url(x)", + ";", +]; + +describe("@font-face block extraction", () => { + it("returns what the depth-counting extraction returned on 20,000 generated sheets", () => { + let seed = 5; + const next = (n: number) => + Math.floor(((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648) * n); + const differences: string[] = []; + for (let run = 0; run < 20_000; run++) { + const css = Array.from({ length: 1 + next(24) }, () => PIECES[next(PIECES.length)]).join(""); + if ( + JSON.stringify(extractFontFaceBlocks(css)) !== JSON.stringify(rescanningFontFaceBlocks(css)) + ) + differences.push(css); + } + expect(differences).toEqual([]); + }); + + it("reads 400 KB of unclosed rules in well under a second", () => { + const css = "@font-face{".repeat(36_000); + const started = performance.now(); + expect(extractFontFaceBlocks(css)).toEqual([]); + expect(performance.now() - started).toBeLessThan(1000); + }); }); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 4510cd0ff9f..be0da6a630c 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1,5 +1,6 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths"; import { + cssRuleMatches, cssUrlMatches, fontFaceBlockMatches, hasBackgroundImageDeclaration, @@ -9,6 +10,7 @@ import { remoteFontFaceUrlMatches, replaceMatches, styleBlockMatches, + stylesheetLinkMatches, } from "@hyperframes/parsers/css-url-scans"; // fallow-ignore-file code-duplication complexity /** @@ -506,8 +508,8 @@ export function detectAncestorBackgroundImage(html: string): boolean { // covered, not just literal `body`/`html`. for (const styleEl of document.querySelectorAll("style")) { const css = styleEl.textContent ?? ""; - for (const rule of css.matchAll(/([^{}]+)\{([^}]*)\}/g)) { - const [, selectorList = "", declarations = ""] = rule; + for (const { parts: rule } of cssRuleMatches(css)) { + const [, selectorList, declarations] = rule; if (!hasBackgroundImageDeclaration(`{${declarations}}`)) continue; for (const selector of selectorList.split(",")) { const sel = selector.trim(); @@ -1774,28 +1776,36 @@ async function fetchExternalStylesheetCss(href: string): Promise * Uses a depth-tracking scan instead of `[^}]*` so blocks with nested * descriptor values that happen to contain `}` (rare but valid in * `format(...)` hints with custom idents) are captured correctly. + * + * @internal exported for unit testing only */ -function extractFontFaceBlocks(css: string): string[] { +export function extractFontFaceBlocks(css: string): string[] { const blocks: string[] = []; + const closes = matchingBraces(css); const atRule = /@font-face\s*/gi; - let m: RegExpExecArray | null; - while ((m = atRule.exec(css)) !== null) { - const openIdx = css.indexOf("{", m.index + m[0].length); - if (openIdx === -1) break; - let depth = 1; - let i = openIdx + 1; - for (; i < css.length && depth > 0; i++) { - if (css[i] === "{") depth++; - else if (css[i] === "}") depth--; - } - if (depth === 0) { - blocks.push(css.slice(m.index, i)); - atRule.lastIndex = i; - } + let open = -1; + for (let m = atRule.exec(css); m; m = atRule.exec(css)) { + if (open < atRule.lastIndex) open = css.indexOf("{", atRule.lastIndex); + if (open === -1) break; + const close = closes.get(open); + if (close === undefined) continue; + blocks.push(css.slice(m.index, close + 1)); + atRule.lastIndex = close + 1; } return blocks; } +/** The `}` that closes each `{` that has one, as a depth count from that `{` finds it. */ +function matchingBraces(css: string): Map { + const closes = new Map(); + const unclosed: number[] = []; + for (let i = 0; i < css.length; i++) { + if (css[i] === "{") unclosed.push(i); + else if (css[i] === "}" && unclosed.length > 0) closes.set(unclosed.pop()!, i); + } + return closes; +} + /** * Find all external `` tags pointing to non-Google * CDNs, fetch their CSS, and replace the `` with an inline `