Skip to content

Commit 7c47bea

Browse files
committed
Safe postcss values parser
1 parent e9e6c85 commit 7c47bea

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

plugins/postcss-image-set-function/.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: 2,
45
},
56
'basic:no-preserve': {
67
message: 'supports { preserve: false } usage',
8+
warnings: 2,
79
options: {
810
preserve: false
911
}

plugins/postcss-image-set-function/index.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,20 @@ module.exports = function creator(opts) {
2222

2323
// if a declaration likely uses an image-set() function
2424
if (imageSetValueMatchRegExp.test(value)) {
25-
const valueAST = parse(value);
25+
let valueAST
26+
27+
try {
28+
valueAST = parse(value, { ignoreUnknownWords: true })
29+
} catch (error) {
30+
decl.warn(
31+
helpers.result,
32+
`Failed to parse value '${value}' as an image-set function. Leaving the original value intact.`
33+
)
34+
}
35+
36+
if (typeof valueAST === 'undefined') {
37+
return
38+
}
2639

2740
// process every image-set() function
2841
valueAST.walkFuncs(node => {

plugins/postcss-image-set-function/test/basic.css

+5
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@
103103
);
104104
}
105105
}
106+
107+
.test-unparseable-image-set-function {
108+
background-image: image-set(url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==) 1x, url(img/test-2x.png) 2x);
109+
background-image: image-set(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==, url(img/test-2x.png) 2x);
110+
}

plugins/postcss-image-set-function/test/basic.expect.css

+5
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,8 @@
285285
);
286286
}
287287
}
288+
289+
.test-unparseable-image-set-function {
290+
background-image: image-set(url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==) 1x, url(img/test-2x.png) 2x);
291+
background-image: image-set(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==, url(img/test-2x.png) 2x);
292+
}

plugins/postcss-image-set-function/test/basic.no-preserve.expect.css

+5
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,8 @@
230230
}
231231
}
232232
}
233+
234+
.test-unparseable-image-set-function {
235+
background-image: image-set(url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==) 1x, url(img/test-2x.png) 2x);
236+
background-image: image-set(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==, url(img/test-2x.png) 2x);
237+
}

0 commit comments

Comments
 (0)