Skip to content

Commit

Permalink
fix: use parsed font-family to compare
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 12, 2024
1 parent 4df8c4c commit e5ae638
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/css/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ const extractableKeyMap: Record<string, keyof NormalizedFontFaceData> = {
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<NormalizedFontFaceData> = {}
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
Expand Down

0 comments on commit e5ae638

Please sign in to comment.