Skip to content

Commit bcc5702

Browse files
authored
fix: do not crash on var(--x, ) in font shorthand (#472)
closes #471
1 parent b470cad commit bcc5702

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,17 @@ export function analyze(css, options = {}) {
528528
else if (isProperty('font', property)) {
529529
if (isSystemFont(node)) return
530530

531-
let { font_size, line_height, font_family } = destructure(node, stringifyNode, function (item) {
531+
let result = destructure(node, stringifyNode, function (item) {
532532
if (item.type === 'keyword') {
533533
valueKeywords.p(item.value, loc)
534534
}
535535
})
536536

537+
if (!result) {
538+
return this.skip
539+
}
540+
541+
let { font_size, line_height, font_family } = result
537542
if (font_family) {
538543
fontFamilies.p(font_family, loc)
539544
}

src/values/destructure-font-shorthand.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ export function isSystemFont(node) {
4040
* @param {*} stringifyNode
4141
*/
4242
export function destructure(value, stringifyNode, cb) {
43-
let font_family = Array.from({ length: 2 })
43+
/** @type {Array<import('css-tree').CssNode | undefined>} */
44+
let font_family = [,]
4445
/** @type {string | undefined} */
4546
let font_size
4647
/** @type {string | undefined} */
4748
let line_height
4849

49-
// FIXME: in case of a var(--my-stack, fam1, fam2, fam3) this forEach also loops over all children of the var()
50+
// Bail out if the value is a single var()
51+
if (value.children.first.type === 'Function' && value.children.first.name.toLowerCase() === 'var') {
52+
return null
53+
}
54+
5055
value.children.forEach(function (node, item) {
5156
let prev = item.prev ? item.prev.data : undefined
5257
let next = item.next ? item.next.data : undefined

src/values/font-families.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ FontFamilies('does not crash on 14px "Inter Var", sans-serif, 700', () => {
109109
})
110110
})
111111

112+
FontFamilies('does not crash on var(--x, )', () => {
113+
const fixture = `
114+
a {
115+
font: var(--x, );
116+
}
117+
`
118+
assert.not.throws(() => {
119+
analyze(fixture).values.fontFamilies
120+
})
121+
})
122+
112123
FontFamilies('handles system fonts', () => {
113124
// Source: https://drafts.csswg.org/css-fonts-3/#font-prop
114125
const fixture = `

0 commit comments

Comments
 (0)