Skip to content

Commit 9dcdb09

Browse files
committed
fix(core): bundled fonts named only in a CSS font shorthand load
1 parent 0f2f70b commit 9dcdb09

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// @vitest-environment node
2+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
3+
import { mkdtempSync, rmSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { injectDeterministicFontFaces } from "./deterministicFonts.js";
7+
8+
let previousCacheDir: string | undefined;
9+
let testCacheDir: string;
10+
11+
beforeAll(() => {
12+
previousCacheDir = process.env.HYPERFRAMES_FONT_CACHE_DIR;
13+
testCacheDir = mkdtempSync(join(tmpdir(), "hf-font-shorthand-"));
14+
process.env.HYPERFRAMES_FONT_CACHE_DIR = testCacheDir;
15+
});
16+
17+
afterAll(() => {
18+
if (previousCacheDir === undefined) delete process.env.HYPERFRAMES_FONT_CACHE_DIR;
19+
else process.env.HYPERFRAMES_FONT_CACHE_DIR = previousCacheDir;
20+
rmSync(testCacheDir, { recursive: true, force: true });
21+
});
22+
23+
const offline = (async () => {
24+
throw new Error("offline");
25+
}) as unknown as typeof fetch;
26+
27+
const inject = (css: string) =>
28+
injectDeterministicFontFaces(`<html><head><style>${css}</style></head><body>x</body></html>`, {
29+
fetchImpl: offline,
30+
allowSystemFontCapture: false,
31+
});
32+
33+
describe("a bundled family named only in the font shorthand loads", () => {
34+
it.each([
35+
["size and family", "p { font: 24px Inter, sans-serif; }"],
36+
["weight, size, line-height", "p { font: 700 24px/1.2 Inter, sans-serif; }"],
37+
["style, variant, weight, size", 'p { font: italic small-caps bold 1.5rem "Inter", serif; }'],
38+
["a keyword size", "p { font: bold large Inter; }"],
39+
["a calc() size", "p { font: calc(10px + 2vw) Inter; }"],
40+
])("%s", async (_case, css) => {
41+
expect(await inject(css)).toMatch(/@font-face\s*\{[^}]*font-family:\s*"Inter"/i);
42+
});
43+
44+
it("ignores a system-font keyword", async () => {
45+
expect(await inject("p { font: caption; }")).not.toContain("@font-face");
46+
});
47+
});

‎packages/core/src/fonts/deterministicFonts.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,32 @@ function collectCssCustomProperties(css: string, customProperties: Map<string, s
263263
});
264264
}
265265

266+
const FONT_SIZE_KEYWORD =
267+
/^(?:xx-small|x-small|small|medium|large|x-large|xx-large|xxx-large|smaller|larger)$/i;
268+
const FONT_SIZE_TOKEN = /^(?:\d*\.?\d+[a-z%]+|(?:calc|min|max|clamp|var)\(.*\))(?:\/\S+)?$/i;
269+
270+
/** The family list of a `font` shorthand: what follows its `<size>[/<line-height>]`, after any style, variant, weight
271+
* and stretch. A system-font keyword (`caption`, `menu`) has no size and names no family. */
272+
function fontShorthandFamilies(value: string): string {
273+
for (const token of value.matchAll(/(?:[^\s(]|\([^)]*\))+/g)) {
274+
const size = token[0].split("/")[0] ?? "";
275+
if (FONT_SIZE_KEYWORD.test(size) || FONT_SIZE_TOKEN.test(token[0])) {
276+
return value.slice(token.index + token[0].length).trim();
277+
}
278+
}
279+
return "";
280+
}
281+
266282
function* iterateCssRootFontFamilyDeclarations(
267283
root: postcss.Root,
268284
): Generator<FontFamilyDeclaration> {
269285
const declarations: FontFamilyDeclaration[] = [];
270286
root.walkDecls((decl) => {
271-
if (decl.prop.toLowerCase() !== "font-family") return;
287+
const prop = decl.prop.toLowerCase();
288+
if (prop !== "font-family" && prop !== "font") return;
272289
if (isFontFaceDeclaration(decl)) return;
273-
const declaration = decl.value;
290+
const declaration = prop === "font" ? fontShorthandFamilies(decl.value) : decl.value;
291+
if (!declaration) return;
274292
declarations.push({
275293
surface: "font-family",
276294
declaration,
@@ -281,7 +299,7 @@ function* iterateCssRootFontFamilyDeclarations(
281299
}
282300

283301
function* iterateCssFontFamilyDeclarations(css: string): Generator<FontFamilyDeclaration> {
284-
if (!/font-family/i.test(css)) return;
302+
if (!/font(?:-family)?\s*:/i.test(css)) return;
285303
const root = parseCssRoot(css);
286304
if (!root) return;
287305
yield* iterateCssRootFontFamilyDeclarations(root);

0 commit comments

Comments
 (0)