Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: continue if font family doesn't match in getFontDetails #88

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/css/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ export function extractFontFaceData (css: string, family?: string): NormalizedFo
for (const node of findAll(parse(css), node => node.type === 'Atrule' && node.name === 'font-face')) {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
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') {
if (family) {
const isCorrectFontFace = node.block?.children.some(child => {
if (child.type !== 'Declaration' || child.property !== 'font-family') { return false }

const value = extractCSSValue(child) as string | string[]
const slug = family.toLowerCase()
if (typeof value === 'string' && value.toLowerCase() !== slug) {
return []
if (typeof value === 'string' && value.toLowerCase() === slug) {
return true
}
if (Array.isArray(value) && value.length > 0 && value.every(v => v.toLowerCase() !== slug)) {
return []
if (Array.isArray(value) && value.length > 0 && value.some(v => v.toLowerCase() === slug)) {
return true
}
}
if (child.property in extractableKeyMap) {
return false
})

// Don't extract font face data from this `@font-face` rule if it doesn't match the specified family
if (!isCorrectFontFace) { continue }
}

const data: Partial<NormalizedFontFaceData> = {}
for (const child of node.block?.children || []) {
if (child.type === 'Declaration' && child.property in extractableKeyMap) {
const value = extractCSSValue(child) as any
data[extractableKeyMap[child.property]!] = child.property === 'src' && !Array.isArray(value) ? [value] : value
}
Expand Down