3
3
*/
4
4
var balanced = require ( "balanced-match" )
5
5
var colorFn = require ( "css-color-function" )
6
+ var helpers = require ( "postcss-message-helpers" )
6
7
7
8
/**
8
9
* PostCSS plugin to transform color()
9
10
*/
10
11
module . exports = function plugin ( ) {
11
12
return function ( style ) {
12
- style . eachDecl ( function transformDecl ( dec ) {
13
- if ( ! dec . value ) {
13
+ style . eachDecl ( function transformDecl ( decl ) {
14
+ if ( ! decl . value || decl . value . indexOf ( "color(" ) === - 1 ) {
14
15
return
15
16
}
16
17
17
- dec . value = transform ( dec . value , dec . source )
18
+ decl . value = helpers . try ( function transformColorValue ( ) {
19
+ return transformColor ( decl . value , decl . source )
20
+ } , decl . source )
18
21
} )
19
22
}
20
23
}
21
24
22
- /**
23
- * Transform colors to rgb() or rgba() on a declaration value
24
- *
25
- * @param {String } string
26
- * @return {String }
27
- */
28
- function transform ( string , source ) {
29
- // order of transformation is important
30
-
31
- try {
32
- if ( string . indexOf ( "color(" ) > - 1 ) {
33
- string = transformColor ( string , source )
34
- }
35
- }
36
- catch ( e ) {
37
- throw new Error ( gnuMessage ( e . message , source ) )
38
- }
39
-
40
- return string
41
- }
42
-
43
25
/**
44
26
* Transform color() to rgb()
45
27
*
@@ -55,19 +37,8 @@ function transformColor(string, source) {
55
37
var fn = string . slice ( index )
56
38
var balancedMatches = balanced ( "(" , ")" , fn )
57
39
if ( ! balancedMatches ) {
58
- throw new SyntaxError ( gnuMessage ( "Missing closing parentheses in '" + string + "'" , source ) )
40
+ throw new Error ( "Missing closing parentheses in '" + string + "'" , source )
59
41
}
60
42
61
43
return string . slice ( 0 , index ) + colorFn . convert ( "color(" + balancedMatches . body + ")" ) + transformColor ( balancedMatches . post )
62
44
}
63
-
64
-
65
- /**
66
- * return GNU style message
67
- *
68
- * @param {String } message
69
- * @param {Object } source
70
- */
71
- function gnuMessage ( message , source ) {
72
- return ( source ? ( source . file ? source . file : "<css input>" ) + ":" + source . start . line + ":" + source . start . column + " " : "" ) + message
73
- }
0 commit comments