Skip to content

Commit 172311e

Browse files
authored
fix(compiler): split font-family only on top-level commas (#3067)
parseFontFamilyValue() split the family stack on every comma, so `font-family: var(--brand-font, inherit)` became two tokens: `var(--brand-font` and `inherit)`. The var() guard from #1655 only skips tokens starting with `var(`, so the orphan fragment was treated as a requested family, failed every resolution path, and aborted fail-closed distributed renders with: FontFetchError: [Compiler] Unresolved fonts in fail-closed mode: inherit). Distributed renders require all fonts to be resolvable. Split on top-level commas only, so a var() expression (including a nested one) stays a single token. Quotes are tracked as well, both so parentheses inside a quoted family name cannot skew the depth counter and so a legal quoted comma no longer splits. Closes #3066
1 parent 288bd70 commit 172311e

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ describe("injectDeterministicFontFaces — failClosedFontFetch: true", () => {
253253
expect(result).toBe(html);
254254
});
255255

256+
it("does NOT throw when font-family uses a CSS var() reference with a fallback", async () => {
257+
const html = `<!doctype html><html><head><style>
258+
.title { font-family: var(--brand-font, inherit); }
259+
</style></head><body><h1 class="title">hello</h1></body></html>`;
260+
const result = await injectDeterministicFontFaces(html, {
261+
failClosedFontFetch: true,
262+
fetchImpl: makeFailingFetch(),
263+
});
264+
expect(result).toBe(html);
265+
});
266+
256267
it("resolves simple CSS var() font aliases when injecting deterministic fonts", async () => {
257268
const html = `<!doctype html><html><head><style>
258269
:root { --ui-font: "Inter"; --vowel-font: "Montserrat"; }

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,44 @@ export const GENERIC_FAMILIES: ReadonlySet<string> = new Set([
5454
* Whitespace and surrounding `"…"` / `'…'` quotes are stripped; case is
5555
* preserved. Pass each name through `normalizeFamilyName` for case-
5656
* insensitive comparisons.
57+
*
58+
* Only top-level commas split: a `var(--x, fallback)` expression stays one
59+
* token, as does a comma inside a quoted family name.
5760
*/
5861
export function parseFontFamilyValue(value: string): string[] {
59-
return value
60-
.split(",")
62+
const pieces: string[] = [];
63+
let start = 0;
64+
let depth = 0;
65+
let quote: "'" | '"' | null = null;
66+
for (let index = 0; index < value.length; index += 1) {
67+
const char = value[index];
68+
if (char === "\\") {
69+
index += 1;
70+
continue;
71+
}
72+
if (quote) {
73+
if (char === quote) quote = null;
74+
continue;
75+
}
76+
if (char === "'" || char === '"') {
77+
quote = char;
78+
continue;
79+
}
80+
if (char === "(") {
81+
depth += 1;
82+
continue;
83+
}
84+
if (char === ")") {
85+
depth = Math.max(0, depth - 1);
86+
continue;
87+
}
88+
if (char !== "," || depth !== 0) continue;
89+
pieces.push(value.slice(start, index));
90+
start = index + 1;
91+
}
92+
pieces.push(value.slice(start));
93+
94+
return pieces
6195
.map((piece) => piece.trim().replace(/^['"]/, "").replace(/['"]$/, "").trim())
6296
.filter((piece) => piece.length > 0);
6397
}

‎packages/producer/src/services/render/planValidation.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ describe("parseFontFamilyValue", () => {
169169
expect(parseFontFamilyValue(`'My Custom Font', serif`)).toEqual(["My Custom Font", "serif"]);
170170
});
171171

172+
it("keeps a comma inside a quoted family name", () => {
173+
expect(parseFontFamilyValue(`"Display, Condensed", serif`)).toEqual([
174+
"Display, Condensed",
175+
"serif",
176+
]);
177+
});
178+
179+
it("keeps a var() fallback in a single token", () => {
180+
expect(parseFontFamilyValue(`var(--brand-font, inherit), sans-serif`)).toEqual([
181+
"var(--brand-font, inherit)",
182+
"sans-serif",
183+
]);
184+
});
185+
186+
it("keeps a nested var() fallback in a single token", () => {
187+
expect(
188+
parseFontFamilyValue(`var(--brand-font, var(--fallback-font, "Inter")), sans-serif`),
189+
).toEqual([`var(--brand-font, var(--fallback-font, "Inter"))`, "sans-serif"]);
190+
});
191+
172192
it("ignores empty entries (trailing commas)", () => {
173193
expect(parseFontFamilyValue(`Inter,,sans-serif`)).toEqual(["Inter", "sans-serif"]);
174194
});

‎packages/producer/tests/distributed/css-var-fonts/src/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
top: 30%;
3636
left: 50%;
3737
transform: translateX(-50%);
38-
font-family: var(--display-font), sans-serif;
38+
font-family: var(--display-font, "Montserrat"), sans-serif;
3939
font-size: 48px;
4040
font-weight: 900;
4141
color: #e94560;

0 commit comments

Comments
 (0)