diff --git a/packages/cli/src/utils/publishProject.cssUrls.test.ts b/packages/cli/src/utils/publishProject.cssUrls.test.ts new file mode 100644 index 0000000000..1679efb740 --- /dev/null +++ b/packages/cli/src/utils/publishProject.cssUrls.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { rewriteCssUrls } from "./publishProject.js"; + +describe("publish CSS url rewriting", () => { + it("rewrites the urls a resolver claims", () => { + const css = `a{background:url( "img/a.png" )} b{background:url(b.png)}`; + const out = rewriteCssUrls(css, "/p", "index.html", (url) => + url === "b.png" ? "assets/b.png" : null, + ); + expect(out).toEqual({ + css: `a{background:url( "img/a.png" )} b{background:url(assets/b.png)}`, + modified: true, + }); + }); + + it("scans 1 MB of unclosed url( in well under a second", () => { + const css = "url(".repeat(250_000); + const started = performance.now(); + expect(rewriteCssUrls(css, "/p", "index.html", () => null)).toEqual({ css, modified: false }); + expect(performance.now() - started).toBeLessThan(1000); + }); +}); diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index 79d595de35..65aa25d350 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -14,7 +14,8 @@ import { parseHTML } from "linkedom"; import AdmZip from "adm-zip"; import type { Ignore } from "ignore"; import { projectIgnore } from "@hyperframes/lint"; -import { CSS_URL_RE, isNonRelativeUrl, isPathInside, rewriteSrcsetUrls } from "@hyperframes/core"; +import { isNonRelativeUrl, isPathInside, rewriteSrcsetUrls } from "@hyperframes/core"; +import { cssUrlMatches, replaceMatches } from "@hyperframes/parsers/css-url-scans"; import { decodeUrlPathVariants, encodeUrlPath } from "@hyperframes/parsers"; import { buildAuthHeaders } from "../auth/client.js"; import { tryResolveCredential } from "../auth/index.js"; @@ -559,7 +560,7 @@ export function rewriteCssUrls( resolveTarget: HtmlAttributeResolver, ): { css: string; modified: boolean } { let modified = false; - const rewritten = css.replace(CSS_URL_RE, (full, quote: string, rawUrl: string) => { + const rewritten = replaceMatches(css, cssUrlMatches(css), (full, quote, rawUrl) => { const target = resolveTarget((rawUrl || "").trim(), referrerAbsDir); if (!target) return full; modified = true; 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 0000000000..e3c6f7261b --- /dev/null +++ b/packages/core/src/compiler/htmlBundler.cssComments.test.ts @@ -0,0 +1,106 @@ +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, restoreCssPlaceholders } from "./htmlBundler"; + +async function bundleWithStylesheet(css: string, imported = ""): Promise { + const dir = mkdtempSync(join(tmpdir(), "hf-css-comments-")); + try { + writeFileSync(join(dir, "style.css"), css); + writeFileSync(join(dir, "part.css"), imported); + 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); + }); + + it("inline imported stylesheets verbatim, replacement patterns included", async () => { + const part = '.b { content: "$\' $&" }'; + expect(await bundleWithStylesheet('@import "part.css"; .a { color: red }', part)).toContain( + part, + ); + }); + + it("inline every import of a sheet imported many times", async () => { + const css = '@import "part.css"; '.repeat(50); + expect((await bundleWithStylesheet(css, ".b{}")).split(".b{}")).toHaveLength(51); + }); +}); + +/** The restore it replaces: each comment, then each import, swapped in at its first occurrence. */ +function sequentialRestore(css: string, comments: string[], imports: string[]): string { + let out = css; + comments.forEach((text, i) => (out = out.replace(`/*__hf_c${i}__*/`, text))); + imports.forEach((text, i) => { + out = out.replace(`/*__hf_import_${comments.length + i}__*/`, text); + }); + return out; +} + +const PIECES = [ + "a", + " ", + "{", + "}", + ";", + "/*", + "*/", + "/**/", + "url(x)", + "\n", + "@media x {", + "_hf_", + "c1", +]; + +describe("CSS placeholder restore", () => { + it("puts back what the sequential restore put back on 20,000 generated sheets", () => { + let seed = 3; + const next = (n: number) => + Math.floor(((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648) * n); + const text = () => Array.from({ length: next(6) }, () => PIECES[next(PIECES.length)]).join(""); + const differences: string[] = []; + for (let run = 0; run < 20_000; run++) { + const comments = Array.from({ length: next(4) }, () => `/*${text()}*/`); + const imports = Array.from({ length: next(4) }, text); + const placeholders = new Map(); + comments.forEach((c, i) => placeholders.set(`/*__hf_c${i}__*/`, c)); + imports.forEach((c, i) => placeholders.set(`/*__hf_import_${comments.length + i}__*/`, c)); + const keys = [...placeholders.keys()].sort(() => next(2) - 0.5); + const css = keys.map((key) => text() + key).join("") + text(); + if (restoreCssPlaceholders(css, placeholders) !== sequentialRestore(css, comments, imports)) + differences.push(css); + } + expect(differences).toEqual([]); + }); + + it("puts back 20,000 placeholders of 200 bytes each in well under a second", () => { + const placeholders = new Map(); + for (let i = 0; i < 20_000; i++) + placeholders.set(`/*__hf_import_${i}__*/`, `.b{}/*${"x".repeat(192)}*/`); + const css = [...placeholders.keys()].join(" "); + const started = performance.now(); + expect(restoreCssPlaceholders(css, placeholders)).toHaveLength( + css.length - [...placeholders.keys()].join("").length + 20_000 * 200, + ); + expect(performance.now() - started).toBeLessThan(1000); + }); +}); diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 867ecbb374..73c335551b 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -41,7 +41,14 @@ import { decodedUrlPath, splitUrlSuffix, } from "@hyperframes/parsers/asset-paths"; -import { CSS_URL_RE, isNonRelativeUrl } from "./assetPaths.js"; +import { + blockCommentMatches, + 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,30 +153,20 @@ function safeReadFile(filePath: string): string | null { } } -const CSS_IMPORT_RE = - /@import\s+(?:url\(\s*(["']?)([^)"']+)\1\s*\)|(["'])([^"']+)\3)\s*([^;]*);\s*/g; +const CSS_PLACEHOLDER_RE = /\/\*__hf_(?:c|import_)\d+__\*\//g; -const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g; +export function restoreCssPlaceholders(css: string, texts: ReadonlyMap): string { + return css.replace(CSS_PLACEHOLDER_RE, (placeholder) => texts.get(placeholder) ?? placeholder); +} -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}__*/`; +function withCommentsStripped(css: string): { stripped: string; comments: Map } { + 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; - }; - return { result, restore }; + return { stripped, comments }; } function resolveRelativeUrlPath(fromDir: string, basePath: string): string { @@ -180,7 +177,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); @@ -275,10 +272,10 @@ function inlineCssFile( projectDir: string, activeImports: Set, ): string { - const { result: strippedCss, restore: restoreComments } = withCommentsStripped(css, (s) => s); - const importPlaceholders: string[] = []; - const withPlaceholders = strippedCss.replace( - CSS_IMPORT_RE, + const { stripped: strippedCss, comments: placeholders } = withCommentsStripped(css); + const withPlaceholders = replaceMatches( + strippedCss, + cssImportMatches(strippedCss), (full, _q1, urlPath, _q2, barePath, mediaQuery) => { const importPath = urlPath ?? barePath; if (!importPath || !isRelativeUrl(importPath)) return full; @@ -294,17 +291,15 @@ function inlineCssFile( activeImports.delete(resolved); const trimmedMedia = (mediaQuery || "").trim(); const block = trimmedMedia ? `@media ${trimmedMedia} {\n${inlined}\n}\n` : inlined + "\n"; - const idx = importPlaceholders.length; - importPlaceholders.push(block); - return `/*__hf_import_${idx}__*/`; + const placeholder = `/*__hf_import_${placeholders.size}__*/`; + placeholders.set(placeholder, block); + return placeholder; }, ); - let rebased = rebaseCssUrls(withPlaceholders, cssFileDir, projectDir); - rebased = restoreComments(rebased); - for (let i = 0; i < importPlaceholders.length; i++) { - rebased = rebased.replace(`/*__hf_import_${i}__*/`, importPlaceholders[i]!); - } - return rebased; + return restoreCssPlaceholders( + rebaseCssUrls(withPlaceholders, cssFileDir, projectDir), + placeholders, + ); } function safeReadFileBuffer(filePath: string): Buffer | null { @@ -507,14 +502,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 +708,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 +723,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 95940b8eaa..4016e6733b 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 {", + "/*", + "*/", + "/*/", + " (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", + " ", + "mask-image:url(", + "mask-image:", + "", + "@font-face{", + "/* ", + " { + 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 0000000000..c0655d309c --- /dev/null +++ b/packages/parsers/src/cssUrlScans.ts @@ -0,0 +1,575 @@ +// 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> { + 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)); + +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; + } +} + +/** Matches of `/]*\brel=["']stylesheet["'][^>]*>/gi`. */ +export function* stylesheetLinkMatches(source: string): Generator> { + const opener = //g); + const rel = finder(source, /\brel=["']stylesheet["']/gi); + for (let open = opener.exec(source); open; open = opener.exec(source)) { + const end = tagEnd(open.index + 5); + if (end === source.length) return; + if (rel(open.index + 5) > end) continue; + yield { index: open.index, parts: [source.slice(open.index, end + 1)] }; + opener.lastIndex = end + 1; + } +} + +/** Matches of `/([^{}]+)\{([^}]*)\}/g`. */ +export function* cssRuleMatches( + source: string, +): Generator> { + const ruleStart = finder(source, /[^{}]/g); + const brace = finder(source, /[{}]/g); + const close = finder(source, /\}/g); + for (let at = 0; at < source.length; ) { + const start = ruleStart(at); + const open = brace(start); + if (source[open] !== "{") { + at = open + 1; + continue; + } + const end = close(open + 1); + if (end === source.length) return; + const selector = source.slice(start, open); + yield { + index: start, + parts: [source.slice(start, end + 1), selector, source.slice(open + 1, end)], + }; + at = end + 1; + } +} diff --git a/packages/parsers/src/rewriteSubCompPaths.ts b/packages/parsers/src/rewriteSubCompPaths.ts index 618d8f256a..167ce71f55 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 ae978755ab..04ffdc153f 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/packages/producer/src/services/htmlCompiler.cssScans.test.ts b/packages/producer/src/services/htmlCompiler.cssScans.test.ts new file mode 100644 index 0000000000..5348ec3211 --- /dev/null +++ b/packages/producer/src/services/htmlCompiler.cssScans.test.ts @@ -0,0 +1,98 @@ +import { describe, expect, it } from "vitest"; +import { + detectAncestorBackgroundImage, + extractFontFaceBlocks, + localizeRemoteBackgroundImages, + localizeRemoteFontFaces, +} from "./htmlCompiler.js"; + +describe("remote CSS asset discovery", () => { + it("scans 200 KB of unclosed background urls in well under a second", async () => { + const html = "background:url(http://".repeat(Math.floor(200_000 / 22)); + const started = performance.now(); + const result = await localizeRemoteBackgroundImages(html, "/nonexistent-download-dir"); + expect(result.html).toBe(html); + expect(performance.now() - started).toBeLessThan(1000); + }); + + it("scans 400 KB of unclosed style tags for font faces in well under a second", async () => { + const html = " { + const html = " { + const css = "a".repeat(400_000); + const html = `
`; + 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 9c05aea584..be0da6a630 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1,4 +1,17 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths"; +import { + cssRuleMatches, + cssUrlMatches, + fontFaceBlockMatches, + hasBackgroundImageDeclaration, + importRuleMatches, + localFontFaceUrlMatches, + remoteBackgroundUrlMatches, + remoteFontFaceUrlMatches, + replaceMatches, + styleBlockMatches, + stylesheetLinkMatches, +} from "@hyperframes/parsers/css-url-scans"; // fallow-ignore-file code-duplication complexity /** * HTML Compiler for Producer @@ -23,7 +36,6 @@ import { extractResolvedMedia, clampDurations, shouldClampResolvedMediaDuration, - CSS_URL_RE, isNonRelativeUrl, parseStrictFiniteTimingNumber, readMediaStart, @@ -462,10 +474,6 @@ export function detectThreeDTransformUsage(html: string): boolean { return THREE_D_CONTEXT_PATTERN.test(html); } -/** A background declaration whose value paints an image (gradient or url). */ -const BACKGROUND_IMAGE_DECL_PATTERN = - /(?:^|;|\{)\s*background(?:-image)?\s*:[^;}]*(?:\bgradient\s*\(|url\s*\()/i; - /** * Background-image signals on ancestors of the composition root. * drawElementImage only paints the captured subtree; drawElementService's @@ -492,7 +500,7 @@ export function detectAncestorBackgroundImage(html: string): boolean { // Inline styles on the ancestor chain. for (const el of ancestors) { const style = el.getAttribute("style"); - if (style && BACKGROUND_IMAGE_DECL_PATTERN.test(`{${style}}`)) return true; + if (style && hasBackgroundImageDeclaration(`{${style}}`)) return true; } //