From 67cb8336a33f9081d92a1969bd04e9c08870a282 Mon Sep 17 00:00:00 2001 From: Petr Miko Date: Tue, 25 Feb 2025 18:57:00 +0100 Subject: [PATCH 1/2] fix: retrieving font family via getPropertyValue, accounting for missing font family --- src/embed-webfonts.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/embed-webfonts.ts b/src/embed-webfonts.ts index a84a699d..a6b06878 100644 --- a/src/embed-webfonts.ts +++ b/src/embed-webfonts.ts @@ -3,6 +3,8 @@ import { toArray } from './util' import { fetchAsDataURL } from './dataurl' import { shouldEmbed, embedResources } from './embed-resources' +const PROPERTY_FONT_FAMILY = 'font-family' + interface Metadata { url: string cssText: string @@ -202,7 +204,8 @@ async function parseWebFontRules( return getWebFontRules(cssRules) } -function normalizeFontFamily(font: string) { +function normalizeFontFamily(font?: string) { + if (font === undefined) return '' return font.trim().replace(/["']/g, '') } @@ -210,7 +213,8 @@ function getUsedFonts(node: HTMLElement) { const fonts = new Set() function traverse(node: HTMLElement) { const fontFamily = - node.style.fontFamily || getComputedStyle(node).fontFamily + node.style.getPropertyValue(PROPERTY_FONT_FAMILY) || + getComputedStyle(node).getPropertyValue(PROPERTY_FONT_FAMILY) fontFamily.split(',').forEach((font) => { fonts.add(normalizeFontFamily(font)) }) @@ -234,7 +238,11 @@ export async function getWebFontCSS( const cssTexts = await Promise.all( rules .filter((rule) => - usedFonts.has(normalizeFontFamily(rule.style.fontFamily)), + usedFonts.has( + normalizeFontFamily( + rule.style.getPropertyValue(PROPERTY_FONT_FAMILY), + ), + ), ) .map((rule) => { const baseUrl = rule.parentStyleSheet From 746b875645d1c34d36a609952f9ceb19956bad85 Mon Sep 17 00:00:00 2001 From: Petr Miko Date: Sun, 2 Mar 2025 13:34:40 +0100 Subject: [PATCH 2/2] fix(embedded webfonts): cr change - removed redundant undefined check --- src/embed-webfonts.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/embed-webfonts.ts b/src/embed-webfonts.ts index a6b06878..39bce8bb 100644 --- a/src/embed-webfonts.ts +++ b/src/embed-webfonts.ts @@ -204,8 +204,7 @@ async function parseWebFontRules( return getWebFontRules(cssRules) } -function normalizeFontFamily(font?: string) { - if (font === undefined) return '' +function normalizeFontFamily(font: string) { return font.trim().replace(/["']/g, '') }