Skip to content

Commit 3b30074

Browse files
fix(core): font setup skips parsing stylesheets that cannot name a font (#5808)
* fix(core): font setup skips parsing stylesheets that cannot name a font * fix(core): drop the comment the ratchet rejects
1 parent 8066772 commit 3b30074

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function googleStylesheetFamilies(urlText: string): string[] {
2121
}
2222

2323
function importUrls(css: string): string[] {
24+
if (!/@import/i.test(css)) return [];
2425
let root: postcss.Root;
2526
try {
2627
root = postcss.parse(css);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @vitest-environment node
2-
import { beforeEach, describe, expect, it } from "vitest";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import postcss from "postcss";
34
import { mkdtempSync, rmSync } from "node:fs";
45
import { tmpdir } from "node:os";
56
import { join } from "node:path";
@@ -50,6 +51,17 @@ describe("existing font-face recognition", () => {
5051
const html = `<html><head><style>${css}</style></head><body>Hello</body></html>`;
5152
expect(await injectDeterministicFontFaces(html, { allowSystemFontCapture: false })).toBe(html);
5253
});
54+
55+
it("never parses a stylesheet that has no font-family or custom property", async () => {
56+
const parse = vi.spyOn(postcss, "parse");
57+
try {
58+
const html = `<html><head><style>${"@font-face{{".repeat(1_000)}</style></head><body>Hello</body></html>`;
59+
await injectDeterministicFontFaces(html, { allowSystemFontCapture: false });
60+
expect(parse).not.toHaveBeenCalled();
61+
} finally {
62+
parse.mockRestore();
63+
}
64+
});
5365
});
5466

5567
describe("Google Fonts CSS request caching", () => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export type FontFamilyDeclaration = {
246246
};
247247

248248
function collectCssCustomProperties(css: string, customProperties: Map<string, string>): void {
249+
if (!css.includes("--")) return;
249250
const root = parseCssRoot(css);
250251
if (!root) return;
251252
root.walkDecls((decl) => {
@@ -272,6 +273,7 @@ function* iterateCssRootFontFamilyDeclarations(
272273
}
273274

274275
function* iterateCssFontFamilyDeclarations(css: string): Generator<FontFamilyDeclaration> {
276+
if (!/font-family/i.test(css)) return;
275277
const root = parseCssRoot(css);
276278
if (!root) return;
277279
yield* iterateCssRootFontFamilyDeclarations(root);

0 commit comments

Comments
 (0)