Skip to content

Commit

Permalink
fix(parse): handle unquoted multi-word font families
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 21, 2024
1 parent 856fad8 commit 5b72725
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 11 additions & 0 deletions playground/pages/edges.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
'Open Sans'
</div>
</template>

<style scoped>
div {
font-family: 'Open Sans';
}
</style>
2 changes: 1 addition & 1 deletion playground/pages/providers/local.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

<style scoped>
div {
font-family: 'CustomFont', sans-serif;
font-family: 'Custom Font', sans-serif;
}
</style>
12 changes: 11 additions & 1 deletion src/css/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,25 @@ export function extractFontFamilies (node: Declaration) {
}

const families = [] as string[]
// Use buffer strategy to handle unquoted 'minified' font-family names
let buffer = ''
for (const child of node.value.children) {
if (child.type === 'Identifier' && !genericCSSFamilies.has(child.name as GenericCSSFamily) && !globalCSSValues.has(child.name)) {
families.push(child.name)
buffer = buffer ? `${buffer} ${child.name}` : child.name
}
if (buffer && child.type === 'Operator' && child.value === ',') {
families.push(buffer)
buffer = ''
}
if (child.type === 'String') {
families.push(child.value)
}
}

if (buffer) {
families.push(buffer)
}

return families
}

Expand Down
7 changes: 3 additions & 4 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ await setup({
})

describe('providers', async () => {
// TODO: investigate regression with spaces in local font-family
it('generates inlined font face rules for `local` provider', async () => {
const html = await $fetch('/providers/local')
expect(extractFontFaces('CustomFont', html)).toMatchInlineSnapshot(`
expect(extractFontFaces('Custom Font', html)).toMatchInlineSnapshot(`
[
"@font-face {
font-family: 'CustomFont';
font-family: 'Custom Font';
src: url("/custom-font.woff2") format(woff2);
font-display: swap;
font-weight: 400;
Expand Down Expand Up @@ -74,7 +73,7 @@ describe('providers', async () => {

it('should allow overriding providers with `none`', async () => {
const html = await $fetch('/providers/none')
expect(extractFontFaces('CustomFont', html)).toMatchInlineSnapshot(`[]`)
expect(extractFontFaces('Custom Font', html)).toMatchInlineSnapshot(`[]`)
})

it('should allow defining custom providers', async () => {
Expand Down

0 comments on commit 5b72725

Please sign in to comment.