From e5ae638762dd62026ef5b6cf4507651375431445 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Mar 2024 16:09:03 -0700 Subject: [PATCH] fix: use parsed font-family to compare --- src/css/parse.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/css/parse.ts b/src/css/parse.ts index 9dcb13d..8b31e1c 100644 --- a/src/css/parse.ts +++ b/src/css/parse.ts @@ -16,17 +16,22 @@ const extractableKeyMap: Record = { export function extractFontFaceData (css: string, family?: string): NormalizedFontFaceData[] { const fontFaces: NormalizedFontFaceData[] = [] - if (family) { - const fontFaceRegex = new RegExp(`@font-face\\s*{[^}]*font-family:\\s*["']${family.toLowerCase()}["'][^}]*}`, 'g'); - css = css.match(fontFaceRegex)!.join('\n') - } - for (const node of findAll(parse(css), node => node.type === 'Atrule' && node.name === 'font-face')) { if (node.type !== 'Atrule' || node.name !== 'font-face') { continue } const data: Partial = {} for (const child of node.block?.children || []) { if (child.type !== 'Declaration') { continue } + if (family && child.property === 'font-family') { + const value = extractCSSValue(child) as string | string[] + const slug = family.toLowerCase() + if (typeof value === 'string' && value.toLowerCase() !== slug) { + return [] + } + if (Array.isArray(value) && value.length > 0 && value.every(v => v.toLowerCase() !== slug)) { + return [] + } + } if (child.property in extractableKeyMap) { const value = extractCSSValue(child) as any data[extractableKeyMap[child.property]!] = child.property === 'src' && !Array.isArray(value) ? [value] : value