Skip to content

Write HTML. Render video. Built for agents.

fix(core): font setup skips parsing stylesheets that cannot name a font (#5808)

Miguel Ángel3b3007442a55
diff --git a/packages/core/src/fonts/authoredGoogleFonts.ts b/packages/core/src/fonts/authoredGoogleFonts.ts
index 75788696af..ffac007b39 100644
--- a/packages/core/src/fonts/authoredGoogleFonts.ts
+++ b/packages/core/src/fonts/authoredGoogleFonts.ts
@@ -21,6 +21,7 @@ function googleStylesheetFamilies(urlText: string): string[] {
}
function importUrls(css: string): string[] {
+ if (!/@import/i.test(css)) return [];
let root: postcss.Root;
try {
root = postcss.parse(css);
diff --git a/packages/core/src/fonts/deterministicFonts.test.ts b/packages/core/src/fonts/deterministicFonts.test.ts
index 5189a5b26d..68a1a05ba9 100644
--- a/packages/core/src/fonts/deterministicFonts.test.ts
+++ b/packages/core/src/fonts/deterministicFonts.test.ts
@@ -1,5 +1,6 @@
// @vitest-environment node
-import { beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import postcss from "postcss";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -50,6 +51,17 @@ describe("existing font-face recognition", () => {
const html = `<html><head><style>${css}</style></head><body>Hello</body></html>`;
expect(await injectDeterministicFontFaces(html, { allowSystemFontCapture: false })).toBe(html);
});
+
+ it("never parses a stylesheet that has no font-family or custom property", async () => {
+ const parse = vi.spyOn(postcss, "parse");
+ try {
+ const html = `<html><head><style>${"@font-face{{".repeat(1_000)}</style></head><body>Hello</body></html>`;
+ await injectDeterministicFontFaces(html, { allowSystemFontCapture: false });
+ expect(parse).not.toHaveBeenCalled();
+ } finally {
+ parse.mockRestore();
+ }
+ });
});
describe("Google Fonts CSS request caching", () => {
diff --git a/packages/core/src/fonts/deterministicFonts.ts b/packages/core/src/fonts/deterministicFonts.ts
index 52947b4d94..df0e1b54ec 100644
--- a/packages/core/src/fonts/deterministicFonts.ts
+++ b/packages/core/src/fonts/deterministicFonts.ts
@@ -246,6 +246,7 @@ export type FontFamilyDeclaration = {
};
function collectCssCustomProperties(css: string, customProperties: Map<string, string>): void {
+ if (!css.includes("--")) return;
const root = parseCssRoot(css);
if (!root) return;
root.walkDecls((decl) => {
@@ -272,6 +273,7 @@ function* iterateCssRootFontFamilyDeclarations(
}
function* iterateCssFontFamilyDeclarations(css: string): Generator<FontFamilyDeclaration> {
+ if (!/font-family/i.test(css)) return;
const root = parseCssRoot(css);
if (!root) return;
yield* iterateCssRootFontFamilyDeclarations(root);