Skip to content

Commit 3610d94

Browse files
authored
fix(producer): bound existing font-face recognition scans (#3718)
1 parent e78da30 commit 3610d94

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎packages/producer/src/services/deterministicFonts.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
import { describe, expect, it } from "bun:test";
2-
import { FONT_ALIASES, FONT_ALIAS_KEYS } from "./deterministicFonts.js";
2+
import {
3+
FONT_ALIASES,
4+
FONT_ALIAS_KEYS,
5+
injectDeterministicFontFaces,
6+
} from "./deterministicFonts.js";
7+
8+
describe("existing font-face recognition", () => {
9+
it.each([
10+
['@font-face { font-family: "Existing Font"; src: url(font.woff2); }', false],
11+
["@FONT-FACE\n{ FONT-FAMILY : 'EXISTING FONT'; }", false],
12+
['@font-face { font-family:\u00a0"Existing Font"; }', false],
13+
['@font-face { font-family:; font-family: "Existing Font"; }', false],
14+
[
15+
'@font-face { font-family: "Other Font"; } @font-face { font-family: "Existing Font"; }',
16+
false,
17+
],
18+
['@font-face { font-family: "Existing Font" }', true],
19+
['@font-face { font-family: "Existing Font";', true],
20+
['@font-face { font-family: "Other Font"; font-family: "Existing Font"; }', true],
21+
['@font-face { font-family: ; font-family: "Existing Font"; }', true],
22+
])("preserves authored font and fallback decisions for %j", async (css, needsFallback) => {
23+
const html = `<html><head><style>${css}</style></head><body><p style="font-family: 'Existing Font';">Hello</p></body></html>`;
24+
let requests = 0;
25+
const fetchImpl = Object.assign(
26+
async () => {
27+
requests += 1;
28+
return new Response("", { status: 404 });
29+
},
30+
{ preconnect: fetch.preconnect },
31+
);
32+
expect(
33+
await injectDeterministicFontFaces(html, { fetchImpl, allowSystemFontCapture: false }),
34+
).toBe(html);
35+
expect(requests > 0).toBe(needsFallback);
36+
});
37+
38+
it.each([
39+
"@font-face{{".repeat(100_000),
40+
"@font-face{{font-family:" + " ".repeat(100_000),
41+
"@font-face{{font-family::;" + "font-family::;".repeat(100_000),
42+
])("handles a long incomplete font-face suffix", async (css) => {
43+
const html = `<html><head><style>${css}</style></head><body>Hello</body></html>`;
44+
expect(await injectDeterministicFontFaces(html, { allowSystemFontCapture: false })).toBe(html);
45+
});
46+
});
347

448
describe("FONT_ALIASES cross-platform coverage", () => {
549
it("maps macOS sans-serif system fonts to inter", () => {

‎packages/producer/src/services/deterministicFonts.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,26 @@ function fontDataUri(
438438

439439
function extractExistingFontFaces(html: string): Set<string> {
440440
const families = new Set<string>();
441-
const fontFaceRegex = /@font-face\s*\{[\s\S]*?font-family\s*:\s*([^;]+);[\s\S]*?\}/gi;
442-
for (const match of html.matchAll(fontFaceRegex)) {
443-
const raw = match[1] || "";
444-
const normalized = normalizeFamilyName(raw);
445-
if (normalized) {
446-
families.add(normalized);
441+
const opening = /@font-face\s*\{/gi;
442+
const family = /font-family\s*:/gi;
443+
while (opening.exec(html)) {
444+
family.lastIndex = opening.lastIndex;
445+
let declaration = family.exec(html);
446+
// The original matcher requires at least one character before ';'.
447+
while (declaration && html[family.lastIndex] === ";") {
448+
declaration = family.exec(html);
447449
}
450+
// If this opener has no complete family/semicolon/closer suffix, no later
451+
// opener can have one. Never retry the same unmatched suffix.
452+
if (!declaration) break;
453+
const valueStart = family.lastIndex;
454+
const semicolon = html.indexOf(";", valueStart);
455+
if (semicolon < 0) break;
456+
const end = html.indexOf("}", semicolon + 1);
457+
if (end < 0) break;
458+
const normalized = normalizeFamilyName(html.slice(valueStart, semicolon));
459+
if (normalized) families.add(normalized);
460+
opening.lastIndex = end + 1;
448461
}
449462
return families;
450463
}

0 commit comments

Comments
 (0)