|
1 | 1 | 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 | +}); |
3 | 47 |
|
4 | 48 | describe("FONT_ALIASES cross-platform coverage", () => { |
5 | 49 | it("maps macOS sans-serif system fonts to inter", () => { |
|
0 commit comments