perf: CSS url, @import, @font-face, style and comment scans run in linear time (#7809)

* 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.

* 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.

* perf(producer): compile finds CSS urls, imports and backgrounds in linear time

Asset collection, head-style @import coalescing, the ancestor
background-image check, remote background and font-face url localization and
local font embedding now use the @hyperframes/parsers css-url-scans entry,
which returns the same matches as the regexes it replaces. A timed test
covers remote background discovery on 200 KB of unclosed background urls.

* perf(parsers): scan style blocks, font-face blocks, comments and mask urls in linear time

css-url-scans gains forward scans for `<style>` blocks, `@font-face { }`
blocks, `/* */` comments, the quoted-or-bare `url()` form and the
`mask-image: … url()` declaration. Each returns the same matches and groups
as the regex in its doc comment. The fuzz test now checks all fifteen scans
on 20,000 generated pages, and the timed tests cover 1 MB of each unclosed
form.

* 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.

* perf(producer): find style and font-face blocks in linear time

Remote font discovery and local font embedding read `<style>` blocks and
`@font-face` rules with the parsers scans, which return the same blocks as
the regexes they replace.

* perf(lint): read stylesheet and mask-image urls in linear time

The missing stylesheet asset and texture mask rules find `url()` values with
the parsers scans, including the mask-image fallback for a stylesheet that
does not parse.

* perf(cli): rewrite published CSS urls in linear time

Publishing rewrites stylesheet `url()` references with the parsers scan in
place of CSS_URL_RE, with the same matches.

* chore: size budgets cover the CSS scans in parsers and inspector

verify:packed-manifests measures parsers at 894,913 unpacked bytes and
inspector at 3,764,456, both just over 1% above their budgets once the
linear CSS scans are bundled. The budgets rise to 900,000 and 3,790,000.

* perf(parsers): scan stylesheet links and CSS rules in linear time

css-url-scans gains forward scans for `<link … rel="stylesheet" …>` tags and
`selector { declarations }` rules that return the same matches and groups as
the regexes in their doc comments. Both join the 20,000-page fuzz test and
the 1 MB timed tests.

* perf(core): restore stylesheet imports and list scene font faces in linear time

Bundling a stylesheet puts its comment and @import placeholders back in one
pass, so an imported sheet comes back verbatim and is not searched again.
fontFaceBlocks lists @font-face rules with the parsers scan. A fuzz test
checks the one-pass restore against the sequential one on 20,000 sheets.

* perf(producer): find stylesheet links, CSS rules and font-face blocks in linear time

Font stylesheet inlining and project font discovery find stylesheet links,
and the ancestor background check finds CSS rules, with the parsers scans.
extractFontFaceBlocks matches every brace in one pass instead of counting
depth again from each @font-face; a fuzz test checks it against the depth
count on 20,000 sheets.

* test(core): time the CSS placeholder restore directly

The bundle test with 6,000 imports was timed mostly on 6,000 file reads,
which took 2.7 s on the Windows runner. The restore is now timed on its own
over 20,000 placeholders, and a short bundle test still checks that every
import of a sheet is inlined.
This commit is contained in:
Michael Wang
2026-11-15 08:22:52 +00:00
committed by GitHub
parent f667111d17
commit ded5e3eec2
20 changed files with 1219 additions and 173 deletions
@@ -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);
});
});
+3 -2
View File
@@ -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;
@@ -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<string> {
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"),
'<html><head><link rel="stylesheet" href="style.css"></head><body><main data-composition-id="root" data-width="320" data-height="180"></main></body></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<string, string>();
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<string, string>();
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);
});
});
+43 -48
View File
@@ -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, string>): string {
return css.replace(CSS_PLACEHOLDER_RE, (placeholder) => texts.get(placeholder) ?? placeholder);
}
function withCommentsStripped<T>(
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<string, string> } {
const comments = new Map<string, string>();
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>,
): 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<string>();
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<string>();
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;
+12 -24
View File
@@ -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 `<style>` declaration value. One pattern covers all
* of them because CSS only ever spells an id reference this way in a
* property VALUE — a bare `#id` (no `url()`) is exclusively a *selector*.
*
* The captured id is the literal fragment text. A fragment is a URL, not a
* CSS identifier, so it carries no CSS escapes; the replacement keeps the
* original quoting and only swaps the id, which stays a valid fragment
* because the namespace prefix is restricted to `[A-Za-z0-9_-]`.
*/
const URL_HASH_REF_RE = /(url\(\s*)(["']?)#([^"')\s]+)\2(\s*\))/gi;
/**
* Attributes carrying a bare `#id` fragment reference rather than a
* `url(#id)` funcref — SVG's `<use>`, `<a>`, `<pattern>`, `<textPath>`,
@@ -51,9 +37,16 @@ function sanitizeNamespaceSegment(value: string): string {
return value.replace(/[^a-zA-Z0-9_-]/g, "-");
}
/**
* Rewrites each `url(#id)` funcref, quoted or bare, in a property value: `clip-path`, `filter`,
* `mask`, `fill`, `stroke`, markers, `cursor` and `mask-image`, inline or in a `<style>`. CSS
* spells an id reference this way only in a value; a bare `#id` is a selector. A fragment is a
* URL, not a CSS identifier, so it carries no escapes; the replacement keeps the quoting and
* swaps only the id, which stays a valid fragment because the prefix is `[A-Za-z0-9_-]`.
*/
function rewriteUrlHashRefs(value: string, idMap: ReadonlyMap<string, string>): string {
if (!value || !value.toLowerCase().includes("url(")) return value;
return value.replace(URL_HASH_REF_RE, (full, pre, quote, id: string, post) => {
return replaceMatches(value, urlHashRefMatches(value), (full, pre, quote, id, post) => {
const mapped = idMap.get(id);
return mapped ? `${pre}${quote}#${mapped}${quote}${post}` : full;
});
@@ -73,9 +66,7 @@ function nativeReferenceValue(
const mapped = idMap.get(value.slice(1));
return mapped ? `#${mapped}` : value;
}
URL_HASH_REF_RE.lastIndex = 0;
const hasUrl = URL_HASH_REF_RE.test(value);
URL_HASH_REF_RE.lastIndex = 0;
const hasUrl = !urlHashRefMatches(value).next().done;
return hasUrl ? rewriteUrlHashRefs(value, idMap) : null;
}
@@ -176,11 +167,8 @@ function collectUrlHashRefsFromText(
filter: ReadonlySet<string>,
out: Set<string>,
): void {
let m: RegExpExecArray | null;
URL_HASH_REF_RE.lastIndex = 0;
while ((m = URL_HASH_REF_RE.exec(text))) {
const refId = m[3]!;
if (filter.has(refId)) out.add(refId);
for (const { parts } of urlHashRefMatches(text)) {
if (filter.has(parts[3])) out.add(parts[3]);
}
}
@@ -1,3 +1,4 @@
import { rawCssUrlMatches, replaceMatches } from "@hyperframes/parsers/css-url-scans";
import { findSceneTemplate } from "@hyperframes/parsers/sub-composition-validity";
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
import {
@@ -75,7 +76,6 @@ type PendingScript =
const EXTERNAL_SCRIPT_LOAD_TIMEOUT_MS = 8000;
const BARE_RELATIVE_PATH_RE = /^(?![a-zA-Z][a-zA-Z\d+\-.]*:)(?!\/\/)(?!\/)(?!\.\.?\/).+/;
const CSS_URL_RE = /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g;
const PATH_ATTRS = ["src", "href"] as const;
/**
@@ -129,7 +129,7 @@ function rewriteRuntimeAssetPath(value: string, compositionUrl: URL | null): str
function rewriteRuntimeCssAssetUrls(cssText: string, compositionUrl: URL | null): string {
if (!compositionUrl || !cssText) return cssText;
return cssText.replace(CSS_URL_RE, (full, quote: string, rawUrl: string) => {
return replaceMatches(cssText, rawCssUrlMatches(cssText), (full, quote, rawUrl) => {
const rewritten = rewriteRuntimeAssetPath(rawUrl || "", compositionUrl);
if (rewritten === rawUrl) return full;
return `url(${quote || ""}${rewritten}${quote || ""})`;
+2 -2
View File
@@ -147,6 +147,7 @@ import {
} from "./timeline";
import { findRootCompositionElement, parseLayoutDimension } from "./compositionDimension";
import { resolveCompositionDuration } from "@hyperframes/parsers/composition-duration";
import { sceneCssUrlMatches } from "@hyperframes/parsers/css-url-scans";
import {
LOOP_INFLATED_TIMELINE_SECONDS,
MIN_VALID_TIMELINE_DURATION_SECONDS,
@@ -4286,8 +4287,7 @@ export function initSandboxRuntimeModular(): void {
const sceneUrls = (parts: Element[]): Set<string> => {
const urls = new Set<string>();
const addCssUrls = (css: string | null) => {
for (const m of (css ?? "").matchAll(/url\(\s*(['"]?)(.*?)\1\s*\)|@import\s+(['"])(.*?)\3/gi))
urls.add(m[2] ?? m[4]!);
for (const { parts: m } of sceneCssUrlMatches(css ?? "")) urls.add(m[2] ?? m[4]!);
for (const set of (css ?? "").matchAll(/image-set\((?:[^()]|\([^()]*\))*\)/gi))
for (const m of set[0].matchAll(/(['"])(.*?)\1/g)) urls.add(m[2]!);
};
+16
View File
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { fontFaceBlocks } from "./sceneParts";
describe("fontFaceBlocks", () => {
it("lists each @font-face rule", () => {
const css = "@font-face{a:1}.x{}@FONT-FACE {b:2} @font-face x{}";
expect(fontFaceBlocks(css)).toEqual(["@font-face{a:1}", "@FONT-FACE {b:2}"]);
});
it("reads 400 KB of unclosed rules in well under a second", () => {
const css = "@font-face{".repeat(36_000);
const started = performance.now();
expect(fontFaceBlocks(css)).toEqual([]);
expect(performance.now() - started).toBeLessThan(1000);
});
});
+3 -1
View File
@@ -1,3 +1,5 @@
import { fontFaceBlockMatches } from "@hyperframes/parsers/css-url-scans";
export const SCENE_PART_ATTR = "data-hf-scene";
export const SCENE_PARTS_META = "hf-scene-parts";
@@ -5,7 +7,7 @@ export const SCENE_PARTS_META = "hf-scene-parts";
export const SCENE_FONTS_ATTR = "data-hf-scene-fonts";
export const fontFaceBlocks = (css: string): string[] =>
css.match(/@font-face\s*\{[^}]*\}/gi) ?? [];
Array.from(fontFaceBlockMatches(css), ({ parts }) => parts[0]);
/** On a scene's top-level host: why the scene cannot be swapped in place, so edits to it reload. */
export const SCENE_NO_SWAP_ATTR = "data-hf-scene-no-swap";
+16
View File
@@ -548,6 +548,22 @@ describe("texture_mask_asset_not_found declarations", () => {
expect(findings[0]?.message).not.toContain("example.png");
},
);
it("reads long runs of unclosed urls in valid and malformed stylesheets in well under two seconds", async () => {
const links =
'<link rel="stylesheet" href="nested.css"><link rel="stylesheet" href="broken.css">';
const project = makeProject(validHtml().replace("<body>", `<head>${links}</head><body>`));
writeFileSync(
join(project, "nested.css"),
`a{background:${"url(".repeat(40_000)}${")".repeat(40_000)}}`,
);
writeFileSync(join(project, "broken.css"), `a{${"mask-image:url(".repeat(3_000)}`);
const started = performance.now();
await lintProject(project);
expect(performance.now() - started).toBeLessThan(2000);
});
});
describe("template shell style sources", () => {
+12 -8
View File
@@ -5,6 +5,11 @@ import { projectIgnore } from "./projectIgnore.js";
import { dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
import { isNonRelativeUrl, rewriteAssetPath } from "@hyperframes/parsers/asset-paths";
import { spansOverlap } from "@hyperframes/parsers/time-spans";
import {
bareOrQuotedCssUrlMatches,
maskImageUrlMatches,
type ScanMatch,
} from "@hyperframes/parsers/css-url-scans";
import {
compositionOriginSeconds,
cutToHostSlots,
@@ -118,9 +123,6 @@ function contentDigest(html: string): string {
}
const AUDIO_EXTENSIONS = new Set([".mp3", ".wav", ".aac", ".ogg", ".m4a", ".flac", ".opus"]);
const CSS_URL_RE = /\burl\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi;
const MASK_IMAGE_URL_RE =
/\b(?:-webkit-)?mask-image\s*:\s*[^;{}]*url\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi;
function isLocalStylesheetHref(href: string): boolean {
return !!href && !/^(https?:|data:|blob:|\/\/)/i.test(href);
@@ -662,7 +664,9 @@ function lintMissingLocalAsset(
return findings;
}
function localCssUrl(match: RegExpMatchArray): string | undefined {
type CssUrlMatch = ScanMatch<[string | undefined, string | undefined, string | undefined]>;
function localCssUrl({ parts: match }: CssUrlMatch): string | undefined {
const rawUrl = match[1] ?? match[2] ?? match[3] ?? "";
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
if (isUnresolvedAssetPlaceholder(rawUrl)) return undefined;
@@ -679,14 +683,14 @@ function lintTextureMaskAssetNotFound(
for (const { html, compSrcPath } of htmlSources) {
const file = sourceFile(projectDir, compSrcPath);
for (const cssSource of collectCssSources(projectDir, html, compSrcPath)) {
let matches: RegExpExecArray[] = [];
let matches: CssUrlMatch[] = [];
try {
postcss.parse(cssSource.content).walkDecls(/^(?:-webkit-)?mask-image$/i, (declaration) => {
matches.push(...declaration.value.matchAll(CSS_URL_RE));
matches.push(...bareOrQuotedCssUrlMatches(declaration.value));
});
} catch (error) {
if (!(error instanceof postcss.CssSyntaxError)) throw error;
matches = [...cssSource.content.matchAll(MASK_IMAGE_URL_RE)];
matches = [...maskImageUrlMatches(cssSource.content)];
}
for (const match of matches) {
const url = localCssUrl(match);
@@ -756,7 +760,7 @@ function lintLinkedStylesheetAssets(
}
root.walkDecls((declaration) => {
if (/^(?:-webkit-)?mask-image$/i.test(declaration.prop)) return;
for (const match of declaration.value.matchAll(CSS_URL_RE)) {
for (const match of bareOrQuotedCssUrlMatches(declaration.value)) {
const url = localCssUrl(match);
if (!url || url.startsWith("/")) continue;
if (
+6
View File
@@ -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",
+10
View File
@@ -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"
+227
View File
@@ -0,0 +1,227 @@
import { isDeepStrictEqual } from "node:util";
import { describe, expect, it } from "vitest";
import {
bareOrQuotedCssUrlMatches,
blockCommentMatches,
cssImportMatches,
cssRuleMatches,
fontFaceBlockMatches,
maskImageUrlMatches,
styleBlockMatches,
cssUrlMatches,
hasBackgroundImageDeclaration,
importRuleMatches,
localFontFaceUrlMatches,
rawCssUrlMatches,
remoteBackgroundUrlMatches,
remoteFontFaceUrlMatches,
replaceMatches,
sceneCssUrlMatches,
stylesheetLinkMatches,
urlHashRefMatches,
type ScanMatch,
} from "./cssUrlScans";
import { rewriteCssAssetUrls } from "./rewriteSubCompPaths";
type Scan = (source: string) => Iterable<ScanMatch<Array<string | undefined>>>;
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],
[bareOrQuotedCssUrlMatches, /\burl\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi],
[
maskImageUrlMatches,
/\b(?:-webkit-)?mask-image\s*:\s*[^;{}]*url\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi,
],
[styleBlockMatches, /<style\b[^>]*>([\s\S]*?)<\/style>/gi],
[fontFaceBlockMatches, /@font-face\s*\{([^}]*)\}/gi],
[blockCommentMatches, /\/\*[\s\S]*?\*\//g],
[stylesheetLinkMatches, /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi],
[cssRuleMatches, /([^{}]+)\{([^}]*)\}/g],
];
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",
"mask-image:",
'url("")',
"url('' )",
"-WEBKIT-mask-image",
"a-webkit-mask-image :",
"<style",
"<STYLE x>",
">",
"</style>",
"</STYLE>",
"@font-face",
"@FONT-FACE {",
"/*",
"*/",
"/*/",
"<link",
"<LINK ",
' rel="stylesheet"',
"REL='STYLESHEET'",
"xrel='stylesheet'",
"rel=\"stylesheet'",
];
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",
" ",
"mask-image:url(",
"mask-image:",
"<style",
"<style>",
"@font-face{",
"/* ",
"<link ",
"a",
"{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);
});
});
+575
View File
@@ -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<G extends Array<string | undefined>> {
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<typeof urlFinders>;
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<ScanMatch<UrlParts>> {
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> = M extends ScanMatch<infer G> ? (full: string, ...groups: G) => string : never;
/** `source.replace(pattern, replace)` for the matches a scan found in `source`. */
export function replaceMatches<M extends ScanMatch<Array<string | undefined>>>(
source: string,
matches: Iterable<M>,
replace: Replacer<M>,
): 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<ScanMatch<ImportParts>> {
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<ScanMatch<HashRefParts>> {
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<string, Finder>,
quote: { '"': finder(source, /"/g), "'": finder(source, /'/g) } as Record<string, Finder>,
wsRunStart,
};
}
type SceneFinders = ReturnType<typeof sceneFinders>;
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<ScanMatch<SceneUrlParts>> {
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<ScanMatch<[]>> {
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<ScanMatch<[url: string]>> {
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<ScanMatch<[url: string]>> {
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<string, Finder>,
bareEnd: finder(source, /[\s"')]/g),
quotedClose: finder(source, /\S/g),
bareClose: finder(source, /\S/g),
};
}
type BareOrQuotedFinders = ReturnType<typeof bareOrQuotedFinders>;
/** `\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<ScanMatch<BareOrQuotedParts>> {
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<ScanMatch<BareOrQuotedParts>> {
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 `/<style\b[^>]*>([\s\S]*?)<\/style>/gi`. */
export function* styleBlockMatches(source: string): Generator<ScanMatch<[css: string]>> {
const opener = /<style\b/gi;
const tagEnd = finder(source, />/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<ScanMatch<[body: string]>> {
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<ScanMatch<[]>> {
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 `/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi`. */
export function* stylesheetLinkMatches(source: string): Generator<ScanMatch<[]>> {
const opener = /<link\b/gi;
const tagEnd = finder(source, />/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<ScanMatch<[selector: string, declarations: string]>> {
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;
}
}
+2 -2
View File
@@ -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;
+1
View File
@@ -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",
@@ -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 = "<style".repeat(Math.floor(400_000 / 6));
const started = performance.now();
const result = await localizeRemoteFontFaces(html, "/nonexistent-download-dir");
expect(result.html).toBe(html);
expect(performance.now() - started).toBeLessThan(1000);
});
it("scans 400 KB of unclosed link tags in well under a second", async () => {
const html = "<link ".repeat(Math.floor(400_000 / 6));
const started = performance.now();
expect((await localizeRemoteFontFaces(html, "/nonexistent-download-dir")).html).toBe(html);
expect(performance.now() - started).toBeLessThan(1000);
});
it("reads 400 KB of style text with no rule in well under a second", () => {
const css = "a".repeat(400_000);
const html = `<html><head><style>${css}</style></head><body><div data-composition-id="r"></div></body></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);
});
});
+60 -81
View File
@@ -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;
}
// <style> rules: any rule carrying an image-painting background declaration
// whose selector resolves to an ancestor of the root. Selector matching goes
@@ -500,9 +508,9 @@ 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;
if (!BACKGROUND_IMAGE_DECL_PATTERN.test(`{${declarations}}`)) continue;
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();
if (!sel || sel.startsWith("@")) continue;
@@ -1025,7 +1033,6 @@ function coalesceHeadStylesAndBodyScripts(html: string): string {
});
const styleEls = head ? Array.from(head.querySelectorAll("style")) : [];
const importRe = /@import\s+url\([^)]*\)\s*;|@import\s+["'][^"']+["']\s*;/gi;
for (const run of styleEls.length > 1 ? headStyleRuns(styleEls) : []) {
const imports: string[] = [];
const cssParts: string[] = [];
@@ -1034,7 +1041,7 @@ function coalesceHeadStylesAndBodyScripts(html: string): string {
for (const el of run) {
const raw = (el.textContent || "").trim();
if (!raw) continue;
const nonImportCss = raw.replace(importRe, (match) => {
const nonImportCss = replaceMatches(raw, importRuleMatches(raw), (match) => {
const cleaned = match.trim();
if (!seenImports.has(cleaned)) {
seenImports.add(cleaned);
@@ -1481,7 +1488,7 @@ export function collectExternalAssets(
for (const styleEl of document.querySelectorAll("style")) {
const css = styleEl.textContent || "";
if (!css.includes("url(")) continue;
const rewritten = css.replace(CSS_URL_RE, (full, quote: string, rawUrl: string) => {
const rewritten = replaceMatches(css, cssUrlMatches(css), (full, quote, rawUrl) => {
const result = processPath((rawUrl || "").trim());
if (!result) return full;
return `url(${quote || ""}${result}${quote || ""})`;
@@ -1493,7 +1500,7 @@ export function collectExternalAssets(
for (const el of document.querySelectorAll("[style]")) {
const style = el.getAttribute("style") || "";
if (!style.includes("url(")) continue;
const rewritten = style.replace(CSS_URL_RE, (full, quote: string, rawUrl: string) => {
const rewritten = replaceMatches(style, cssUrlMatches(style), (full, quote, rawUrl) => {
const result = processPath((rawUrl || "").trim());
if (!result) return full;
return `url(${quote || ""}${result}${quote || ""})`;
@@ -1689,12 +1696,6 @@ export async function localizeRemoteImageSources(
);
}
// Match a remote url() inside a `background` / `background-image` CSS declaration
// (style blocks or inline style attrs). `[^;}"']*?` lets position/color tokens
// precede the url() in the shorthand while stopping at the declaration boundary.
const REMOTE_BG_URL_RE =
/background(?:-image)?\s*:\s*[^;}"']*?url\(\s*["']?(https?:\/\/[^"')]+)["']?\s*\)/gi;
/**
* Download remote CSS `background-image: url(https://...)` references and rewrite
* them to local same-origin paths.
@@ -1713,10 +1714,8 @@ export async function localizeRemoteBackgroundImages(
downloadDir: string,
): Promise<{ html: string; remoteMediaAssets: Map<string, string> }> {
const urlSet = new Set<string>();
const re = new RegExp(REMOTE_BG_URL_RE.source, REMOTE_BG_URL_RE.flags);
let m: RegExpExecArray | null;
while ((m = re.exec(html)) !== null) {
if (m[1] && !isGoogleFontsUrl(m[1])) urlSet.add(m[1]);
for (const { parts } of remoteBackgroundUrlMatches(html)) {
if (parts[1] && !isGoogleFontsUrl(parts[1])) urlSet.add(parts[1]);
}
return downloadAndRewriteUrls(
urlSet,
@@ -1730,16 +1729,6 @@ export async function localizeRemoteBackgroundImages(
);
}
// Match url("https://...") or url('https://...') inside @font-face blocks.
// We scan the full HTML (which includes <style> blocks) — matching against
// @font-face context precisely would require a CSS parser; instead we match
// any url(https?://...) that appears inside a @font-face rule by looking for
// the surrounding context. Simple pattern: capture all HTTP url() references
// that follow a @font-face opener (before the closing brace). The regex is
// applied to the CSS text extracted from <style> blocks so it can't
// accidentally match JavaScript string literals.
const REMOTE_FONTFACE_URL_RE = /url\(["']?(https?:\/\/[^"')]+)["']?\)/gi;
/**
* Download any remote font URLs from `@font-face` src declarations, rewrite
* the CSS `url(...)` references to local paths, and return a map of assets.
@@ -1787,28 +1776,36 @@ async function fetchExternalStylesheetCss(href: string): Promise<string | null>
* 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<number, number> {
const closes = new Map<number, number>();
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 `<link rel="stylesheet">` tags pointing to non-Google
* CDNs, fetch their CSS, and replace the `<link>` with an inline `<style>`
@@ -1821,13 +1818,11 @@ function extractFontFaceBlocks(css: string): string[] {
*/
// fallow-ignore-next-line complexity
async function inlineExternalFontStylesheets(html: string): Promise<string> {
const linkRe = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
const hrefRe = /\bhref=["']([^"']+)["']/i;
const linkMatches: { fullMatch: string; href: string }[] = [];
let linkMatch: RegExpExecArray | null;
while ((linkMatch = linkRe.exec(html)) !== null) {
const tag = linkMatch[0];
for (const { parts } of stylesheetLinkMatches(html)) {
const tag = parts[0];
const hrefMatch = hrefRe.exec(tag);
if (!hrefMatch?.[1]) continue;
const href = hrefMatch[1];
@@ -1871,7 +1866,9 @@ async function inlineExternalFontStylesheets(html: string): Promise<string> {
// Families a linked project stylesheet declares with an @font-face file the project ships.
function projectStylesheetFontFamilies(html: string, projectDir: string): string[] {
const families: string[] = [];
for (const [tag] of html.matchAll(/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi)) {
for (const {
parts: [tag],
} of stylesheetLinkMatches(html)) {
const href = /\bhref=["']([^"']+)["']/i.exec(tag)?.[1];
if (!href || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(href)) continue;
const sheet = resolve(projectDir, decodedUrlPath(href).replace(/^(?:\.?\/)+/, ""));
@@ -1881,7 +1878,7 @@ function projectStylesheetFontFamilies(html: string, projectDir: string): string
.exec(face)?.[1]
?.trim()
.replace(/^['"]|['"]$/g, "");
const shipsFile = [...face.matchAll(LOCAL_FONTFACE_URL_RE)].some(([, url = ""]) => {
const shipsFile = [...localFontFaceUrlMatches(face)].some(({ parts: [, url = ""] }) => {
const path = decodedUrlPath(url);
const file = path.startsWith("/")
? resolve(projectDir, `.${path}`)
@@ -1900,12 +1897,10 @@ function projectStylesheetFontFamilies(html: string, projectDir: string): string
*/
// fallow-ignore-next-line complexity
function collectFontFaceUrls(html: string): Set<string> {
const styleBlockRe = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
const urlSet = new Set<string>();
let styleMatch: RegExpExecArray | null;
while ((styleMatch = styleBlockRe.exec(html)) !== null) {
const cssText = styleMatch[1] ?? "";
for (const { parts } of styleBlockMatches(html)) {
const cssText = parts[1];
// Depth-tracking scanner to correctly handle @font-face blocks that
// contain nested braces (e.g. unicode-range fallback rules).
let i = 0;
@@ -1922,10 +1917,8 @@ function collectFontFaceUrls(html: string): Set<string> {
j++;
}
const block = cssText.slice(braceStart + 1, j - 1);
const urlRe = new RegExp(REMOTE_FONTFACE_URL_RE.source, REMOTE_FONTFACE_URL_RE.flags);
let urlMatch: RegExpExecArray | null;
while ((urlMatch = urlRe.exec(block)) !== null) {
if (urlMatch[1]) urlSet.add(urlMatch[1]);
for (const { parts } of remoteFontFaceUrlMatches(block)) {
if (parts[1]) urlSet.add(parts[1]);
}
i = j;
}
@@ -1956,12 +1949,6 @@ function localizeFontFaceUrls(
);
}
// `file:` joins data: and http(s): in the exclusion list. Without it an
// absolute `file:///abs/path/font.ttf` src was read as a project-RELATIVE path,
// resolved to `<projectDir>/file:/abs/path/...`, and the failed read was
// swallowed below — leaving the rule untouched for the browser to reject.
const LOCAL_FONTFACE_URL_RE = /url\(["']?(?!data:|file:|https?:\/\/)([^"')]+)["']?\)/gi;
/**
* Match one `url(<path>)` occurrence, with or without quotes, for a literal
* path. Exported for tests: the suffix-collision it prevents is invisible in
@@ -2002,24 +1989,16 @@ async function embedLocalFontFaces(
log: ProducerLogger = defaultLogger,
): Promise<string> {
const { fontToDataUri: toDataUri } = await import("@hyperframes/core/fonts/embed");
const styleBlockRe = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
const fontFaceRe = /@font-face\s*\{([^}]*)\}/gi;
let result = html;
const embeddedPaths = new Set<string>();
const dataUriByAbsolutePath = new Map<string, string>();
const fileBackedAbsolutePaths = new Set<string>();
let styleMatch: RegExpExecArray | null;
while ((styleMatch = styleBlockRe.exec(html)) !== null) {
const cssText = styleMatch[1] ?? "";
const ffRe = new RegExp(fontFaceRe.source, fontFaceRe.flags);
let ffMatch: RegExpExecArray | null;
while ((ffMatch = ffRe.exec(cssText)) !== null) {
const block = ffMatch[1] ?? "";
const urlRe = new RegExp(LOCAL_FONTFACE_URL_RE.source, LOCAL_FONTFACE_URL_RE.flags);
let urlMatch: RegExpExecArray | null;
while ((urlMatch = urlRe.exec(block)) !== null) {
const localPath = urlMatch[1];
for (const { parts: style } of styleBlockMatches(html)) {
for (const { parts: face } of fontFaceBlockMatches(style[1])) {
const block = face[1];
for (const { parts } of localFontFaceUrlMatches(block)) {
const localPath = parts[1];
if (!localPath || embeddedPaths.has(localPath)) continue;
const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath);
if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) continue;
+3 -3
View File
@@ -21,15 +21,15 @@
},
"@hyperframes/inspector": {
"packed": 880000,
"unpacked": 3770000
"unpacked": 3780000
},
"@hyperframes/lint": {
"packed": 191022,
"unpacked": 737342
},
"@hyperframes/parsers": {
"packed": 203731,
"unpacked": 885314
"packed": 208000,
"unpacked": 900000
},
"@hyperframes/player": {
"packed": 159000,