Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit b101742

Browse files
committed
Safe postcss values parser
1 parent 91a0c3c commit b101742

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.tape.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module.exports = {
22
'basic': {
3-
message: 'supports basic usage'
3+
message: 'supports basic usage',
4+
warnings: 1,
45
},
56
'basic:preserve-true': {
67
message: 'supports { preserve: true } usage',
8+
warnings: 1,
79
options: {
810
preserve: true
911
}

src/onCSSDeclaration.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ import onCSSFunction from './onCSSFunction'
33
import options from './options'
44

55
/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */
6-
const onCSSDeclaration = decl => {
6+
const onCSSDeclaration = (decl, { result }) => {
77
const { value: originalValue } = decl
88

99
if (hasAnyColorFunction(originalValue)) {
10-
const valueAST = parse(originalValue)
10+
let valueAST
11+
12+
try {
13+
valueAST = parse(originalValue, { ignoreUnknownWords: true })
14+
} catch (error) {
15+
decl.warn(
16+
result,
17+
`Failed to parse value '${originalValue}' as a lab or hcl function. Leaving the original value intact.`
18+
)
19+
}
20+
21+
if (typeof valueAST === 'undefined') {
22+
return
23+
}
1124

1225
valueAST.walkType('func', onCSSFunction)
1326

test/basic.css

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
color: lch(40% 68.8 34.5 / 50%);
1616
color: lch(100% 50 0);
1717
}
18+
19+
.test-unparseable-lab-function {
20+
background-image: lab(; );
21+
}

test/basic.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
color: rgba(178, 34, 34, 0.5);
1616
color: rgb(255, 216, 255);
1717
}
18+
19+
.test-unparseable-lab-function {
20+
background-image: lab(; );
21+
}

test/basic.preserve-true.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
color: rgb(255, 216, 255);
2828
color: lch(100% 50 0);
2929
}
30+
31+
.test-unparseable-lab-function {
32+
background-image: lab(; );
33+
}

0 commit comments

Comments
 (0)