Skip to content

Commit 349edb4

Browse files
authored
Loosen the valueParser to avoid throing many errors (#36)
1 parent 1169a83 commit 349edb4

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

src/analyzer/values/colors.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ function nodeIsKeyword(node) {
3232
function extractColorsFromDeclaration(declaration) {
3333
const colors = []
3434

35-
valueParser(declaration.value).parse().walk(node => {
36-
if (nodeIsHexColor(node) || nodeIsColorFn(node) || nodeIsKeyword(node)) {
37-
return colors.push(node)
38-
}
39-
})
35+
valueParser(declaration.value, {loose: true})
36+
.parse()
37+
.walk(node => {
38+
if (nodeIsHexColor(node) || nodeIsColorFn(node) || nodeIsKeyword(node)) {
39+
return colors.push(node)
40+
}
41+
})
4042

4143
if (colors.length > 0) {
4244
declaration.colors = colors.map(color => color.toString().trim())

test/analyzer/values/input.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
/* comment */
1515
}
1616

17+
/* Custom properties and calc() and complex values */
18+
:root {
19+
--my-custom-property: 12px;
20+
height: var(--my-custom-property);
21+
width: calc(100px + 2%);
22+
grid-column: 1/-1; /* Used to cause an Error */
23+
}
24+
25+
/* Colors */
1726
.color4 { color: #aff034; }
1827
.color5 { color: rgb(100, 200, 10); }
1928
.color6 { color: rgba(100, 200, 10, 0.5); }

test/analyzer/values/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"total": 69,
2+
"total": 73,
33
"fontfamilies": {
44
"total": 18,
55
"totalUnique": 12,
@@ -121,7 +121,7 @@
121121
"count": 1
122122
}
123123
],
124-
"share": 0.057971014492753624
124+
"share": 0.0547945205479452
125125
},
126126
"colors": {
127127
"total": 22,

test/parser/declarations.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ const test = require('ava')
22
const parser = require('../../src/parser')
33

44
test('basic declarations are parsed', async t => {
5-
const fixture = 'html, body {color:red; font-size : 12px; a: whatever;}'
5+
const fixture = `
6+
html, body {
7+
color:red;
8+
font-size : 12px;
9+
a: whatever;
10+
}
11+
`
612
const actual = await parser(fixture)
713
const expected = [{
814
property: 'color',
@@ -22,7 +28,12 @@ test('basic declarations are parsed', async t => {
2228
})
2329

2430
test('!important is parsed', async t => {
25-
const fixture = 'html { color: red !important; content: \'!important\'; color: blue; }'
31+
const fixture = `html {
32+
color: red !important;
33+
content: '!important';
34+
color: blue;
35+
}
36+
`
2637
const actual = await parser(fixture)
2738
const expected = [{
2839
property: 'color',
@@ -40,3 +51,45 @@ test('!important is parsed', async t => {
4051

4152
t.deepEqual(actual.declarations, expected)
4253
})
54+
55+
test('custom properties are parsed', async t => {
56+
const fixture = `
57+
:root {
58+
--my-custom-property: 12px;
59+
width: var(--my-custom-property);
60+
}
61+
`
62+
const actual = await parser(fixture)
63+
const expected = [{
64+
property: '--my-custom-property',
65+
value: '12px',
66+
important: false
67+
}, {
68+
property: 'width',
69+
value: 'var(--my-custom-property)',
70+
important: false
71+
}]
72+
73+
t.deepEqual(actual.declarations, expected)
74+
})
75+
76+
test('calc() is parsed', async t => {
77+
const fixture = `
78+
.el {
79+
width: calc(100px + 3%);
80+
font-size: calc(3em + 20vmin);
81+
}
82+
`
83+
const actual = await parser(fixture)
84+
const expected = [{
85+
property: 'width',
86+
value: 'calc(100px + 3%)',
87+
important: false
88+
}, {
89+
property: 'font-size',
90+
value: 'calc(3em + 20vmin)',
91+
important: false
92+
}]
93+
94+
t.deepEqual(actual.declarations, expected)
95+
})

0 commit comments

Comments
 (0)