Skip to content

Write HTML. Render video. Built for agents.

fix(producer): bound existing font-face recognition scans (#3718)

James Russo3610d94a9850
diff --git a/packages/producer/src/services/deterministicFonts.test.ts b/packages/producer/src/services/deterministicFonts.test.ts
index 3dabac055a..f76fee312b 100644
--- a/packages/producer/src/services/deterministicFonts.test.ts
+++ b/packages/producer/src/services/deterministicFonts.test.ts
@@ -1,5 +1,49 @@
import { describe, expect, it } from "bun:test";
-import { FONT_ALIASES, FONT_ALIAS_KEYS } from "./deterministicFonts.js";
+import {
+ FONT_ALIASES,
+ FONT_ALIAS_KEYS,
+ injectDeterministicFontFaces,
+} from "./deterministicFonts.js";
+
+describe("existing font-face recognition", () => {
+ it.each([
+ ['@font-face { font-family: "Existing Font"; src: url(font.woff2); }', false],
+ ["@FONT-FACE\n{ FONT-FAMILY : 'EXISTING FONT'; }", false],
+ ['@font-face { font-family:\u00a0"Existing Font"; }', false],
+ ['@font-face { font-family:; font-family: "Existing Font"; }', false],
+ [
+ '@font-face { font-family: "Other Font"; } @font-face { font-family: "Existing Font"; }',
+ false,
+ ],
+ ['@font-face { font-family: "Existing Font" }', true],
+ ['@font-face { font-family: "Existing Font";', true],
+ ['@font-face { font-family: "Other Font"; font-family: "Existing Font"; }', true],
+ ['@font-face { font-family: ; font-family: "Existing Font"; }', true],
+ ])("preserves authored font and fallback decisions for %j", async (css, needsFallback) => {
+ const html = `<html><head><style>${css}</style></head><body><p style="font-family: 'Existing Font';">Hello</p></body></html>`;
+ let requests = 0;
+ const fetchImpl = Object.assign(
+ async () => {
+ requests += 1;
+ return new Response("", { status: 404 });
+ },
+ { preconnect: fetch.preconnect },
+ );
+ expect(
+ await injectDeterministicFontFaces(html, { fetchImpl, allowSystemFontCapture: false }),
+ ).toBe(html);
+ expect(requests > 0).toBe(needsFallback);
+ });
+
+ it.each([
+ "@font-face{{".repeat(100_000),
+ "@font-face{{font-family:" + " ".repeat(100_000),
+ "@font-face{{font-family::;" + "font-family::;".repeat(100_000),
+ ])("handles a long incomplete font-face suffix", async (css) => {
+ const html = `<html><head><style>${css}</style></head><body>Hello</body></html>`;
+ expect(await injectDeterministicFontFaces(html, { allowSystemFontCapture: false })).toBe(html);
+ });
+});
describe("FONT_ALIASES cross-platform coverage", () => {
it("maps macOS sans-serif system fonts to inter", () => {
diff --git a/packages/producer/src/services/deterministicFonts.ts b/packages/producer/src/services/deterministicFonts.ts
index 4c1c20bfaa..923d5fa7d4 100644
--- a/packages/producer/src/services/deterministicFonts.ts
+++ b/packages/producer/src/services/deterministicFonts.ts
@@ -438,13 +438,26 @@ function fontDataUri(
function extractExistingFontFaces(html: string): Set<string> {
const families = new Set<string>();
- const fontFaceRegex = /@font-face\s*\{[\s\S]*?font-family\s*:\s*([^;]+);[\s\S]*?\}/gi;
- for (const match of html.matchAll(fontFaceRegex)) {
- const raw = match[1] || "";
- const normalized = normalizeFamilyName(raw);
- if (normalized) {
- families.add(normalized);
+ const opening = /@font-face\s*\{/gi;
+ const family = /font-family\s*:/gi;
+ while (opening.exec(html)) {
+ family.lastIndex = opening.lastIndex;
+ let declaration = family.exec(html);
+ // The original matcher requires at least one character before ';'.
+ while (declaration && html[family.lastIndex] === ";") {
+ declaration = family.exec(html);
}
+ // If this opener has no complete family/semicolon/closer suffix, no later
+ // opener can have one. Never retry the same unmatched suffix.
+ if (!declaration) break;
+ const valueStart = family.lastIndex;
+ const semicolon = html.indexOf(";", valueStart);
+ if (semicolon < 0) break;
+ const end = html.indexOf("}", semicolon + 1);
+ if (end < 0) break;
+ const normalized = normalizeFamilyName(html.slice(valueStart, semicolon));
+ if (normalized) families.add(normalized);
+ opening.lastIndex = end + 1;
}
return families;
}