Skip to content

Commit e1d572d

Browse files
authored
upgrade postcss-vaues-parser to v3 (#118)
1 parent ab64853 commit e1d572d

File tree

4 files changed

+70
-70
lines changed

4 files changed

+70
-70
lines changed

package-lock.json

Lines changed: 58 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"src"
2727
],
2828
"scripts": {
29-
"test": "xo && nyc ava test/analyzer test/parser"
29+
"test": "xo && nyc ava test"
3030
},
3131
"xo": {
3232
"space": true,
@@ -48,7 +48,6 @@
4848
"dependencies": {
4949
"color-sorter": "^3.0.0",
5050
"css-at-supports-browser-h4cks-analyzer": "^1.0.0",
51-
"css-color-names": "^1.0.0",
5251
"css-media-query-browser-h4cks-analyzer": "^1.0.0",
5352
"css-property-browser-h4cks-analyzer": "^1.1.0",
5453
"css-selector-browser-h4cks-analyzer": "^1.1.0",
@@ -59,7 +58,7 @@
5958
"is-vendor-prefixed": "^1.0.0",
6059
"path": "^0.12.7",
6160
"postcss": "^7.0.14",
62-
"postcss-values-parser": "^2.0.1",
61+
"postcss-values-parser": "^3.0.2",
6362
"specificity": "^0.4.1",
6463
"string-natural-compare": "^2.0.3",
6564
"tinycolor2": "^1.4.1",

src/analyzer/values/colors.js

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
1-
const valueParser = require('postcss-values-parser')
2-
const cssColorNames = require('css-color-names')
1+
const {parse} = require('postcss-values-parser')
32
const tinycolor = require('tinycolor2')
43
const colorSorter = require('color-sorter')
54

65
const uniquer = require('../../utils/uniquer')
76

8-
const CSS_COLOR_KEYWORDS = Object.keys(cssColorNames).map(color =>
9-
color.toLowerCase()
10-
)
11-
const CSS_COLOR_FUNCTIONS = ['hsl', 'hsla', 'rgb', 'rgba']
12-
13-
function prepareValue(value) {
14-
return value
15-
.toString()
16-
.toLowerCase()
17-
.trim()
18-
}
19-
20-
function nodeIsHexColor(node) {
21-
return node.isColor
22-
}
23-
24-
function nodeIsColorFn(node) {
25-
return (
26-
node.type === 'func' &&
27-
CSS_COLOR_FUNCTIONS.includes(prepareValue(node.value))
28-
)
29-
}
30-
31-
function nodeIsKeyword(node) {
32-
return node.type === 'word' && CSS_COLOR_KEYWORDS.includes(prepareValue(node))
33-
}
34-
357
function extractColorsFromDeclaration(declaration) {
368
const colors = []
379

38-
valueParser(declaration.value, {loose: true})
39-
.parse()
40-
.walk(node => {
41-
if (nodeIsHexColor(node) || nodeIsColorFn(node) || nodeIsKeyword(node)) {
42-
return colors.push(node)
43-
}
44-
})
10+
parse(declaration.value, {loose: true}).walk(node => {
11+
if (node.isColor) {
12+
return colors.push(node)
13+
}
14+
})
4515

4616
if (colors.length > 0) {
4717
declaration.colors = colors.map(color => color.toString().trim())

test/analyzer/values/colors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ test('It finds RGB(A) colors', t => {
169169
[
170170
'rgb(100, 200, 10)',
171171
'rgba(100, 200, 10, 0.5)',
172-
'rgba(100, 200, 11, .5)',
172+
'RGBA(100, 200, 11, .5)',
173173
'rgba(2,2,2,.2)'
174174
].map(value => ({
175175
property: 'color',
@@ -181,7 +181,7 @@ test('It finds RGB(A) colors', t => {
181181
unique: [
182182
{count: 1, value: 'rgb(100, 200, 10)'},
183183
{count: 1, value: 'rgba(100, 200, 10, 0.5)'},
184-
{count: 1, value: 'rgba(100, 200, 11, .5)'},
184+
{count: 1, value: 'RGBA(100, 200, 11, .5)'},
185185
{count: 1, value: 'rgba(2,2,2,.2)'}
186186
],
187187
totalUnique: 4,
@@ -197,7 +197,7 @@ test('It finds RGB(A) colors', t => {
197197

198198
test('It finds HSL(A) colors', t => {
199199
const actual = analyze(
200-
['hsl(100, 20%, 30%)', 'hsla(100, 20%, 30%, 0.5)'].map(value => ({
200+
['hsl(100, 20%, 30%)', 'Hsla(100, 20%, 30%, 0.5)'].map(value => ({
201201
property: 'color',
202202
value
203203
}))
@@ -206,7 +206,7 @@ test('It finds HSL(A) colors', t => {
206206
total: 2,
207207
unique: [
208208
{count: 1, value: 'hsl(100, 20%, 30%)'},
209-
{count: 1, value: 'hsla(100, 20%, 30%, 0.5)'}
209+
{count: 1, value: 'Hsla(100, 20%, 30%, 0.5)'}
210210
],
211211
totalUnique: 2,
212212
duplicates: {

0 commit comments

Comments
 (0)