File tree Expand file tree Collapse file tree 2 files changed +37
-11
lines changed Expand file tree Collapse file tree 2 files changed +37
-11
lines changed Original file line number Diff line number Diff line change @@ -10,15 +10,22 @@ var helpers = require("postcss-message-helpers")
10
10
* PostCSS plugin to transform color()
11
11
*/
12
12
module . exports = postcss . plugin ( "postcss-color-function" , function ( ) {
13
- return function ( style ) {
13
+ return function ( style , result ) {
14
14
style . walkDecls ( function transformDecl ( decl ) {
15
15
if ( ! decl . value || decl . value . indexOf ( "color(" ) === - 1 ) {
16
16
return
17
17
}
18
18
19
- decl . value = helpers . try ( function transformColorValue ( ) {
20
- return transformColor ( decl . value )
21
- } , decl . source )
19
+ try {
20
+ decl . value = helpers . try ( function transformColorValue ( ) {
21
+ return transformColor ( decl . value )
22
+ } , decl . source )
23
+ } catch ( error ) {
24
+ decl . warn ( result , error . message , {
25
+ word : decl . value ,
26
+ index : decl . index ,
27
+ } )
28
+ }
22
29
} )
23
30
}
24
31
} )
Original file line number Diff line number Diff line change @@ -23,12 +23,31 @@ test("color()", function(t) {
23
23
t . end ( )
24
24
} )
25
25
26
- test ( "throw errors" , function ( t ) {
27
- t . throws ( function ( ) {
28
- return postcss ( plugin ( ) ) . process ( read ( filename ( "fixtures/error" ) ) ) . css
29
- } ,
30
- / U n a b l e t o p a r s e c o l o r f r o m s t r i n g / ,
31
- "throws a readable error when a color can't be parsed" )
26
+ test ( "logs warning when color() value cannot be parsed" , function ( t ) {
27
+ postcss ( plugin ( ) ) . process ( read ( filename ( "fixtures/error" ) ) )
28
+ . then ( function ( result ) {
29
+ var warnings = result . warnings ( ) ;
30
+ t . equals ( warnings . length , 1 , "expected only 1 warning" ) ;
32
31
33
- t . end ( )
32
+ var warning = warnings [ 0 ]
33
+ t . equals (
34
+ warning . plugin ,
35
+ "postcss-color-function" ,
36
+ "expected `warning.plugin` to match this plugin's name"
37
+ )
38
+
39
+ t . equals (
40
+ warning . word ,
41
+ "color(blurp a(+10%))" ,
42
+ "expected `warning.word` to match color() declaration"
43
+ )
44
+
45
+ t . equals (
46
+ warning . text ,
47
+ "<css input>:2:3: Unable to parse color from string \"blurp\"" ,
48
+ "expected `warning.text` to contain a readable error when a color can't be parsed"
49
+ )
50
+
51
+ t . end ( )
52
+ } )
34
53
} )
You can’t perform that action at this time.
0 commit comments