2
2
* Module dependencies.
3
3
*/
4
4
var postcss = require ( "postcss" )
5
- var balanced = require ( "balanced-match " )
5
+ var parser = require ( "postcss-value-parser " )
6
6
var colorFn = require ( "css-color-function" )
7
7
var helpers = require ( "postcss-message-helpers" )
8
8
@@ -17,7 +17,7 @@ module.exports = postcss.plugin("postcss-color-function", function() {
17
17
}
18
18
19
19
decl . value = helpers . try ( function transformColorValue ( ) {
20
- return transformColor ( decl . value , decl . source )
20
+ return transformColor ( decl . value )
21
21
} , decl . source )
22
22
} )
23
23
}
@@ -29,22 +29,13 @@ module.exports = postcss.plugin("postcss-color-function", function() {
29
29
* @param {String } string declaration value
30
30
* @return {String } converted declaration value to rgba()
31
31
*/
32
- function transformColor ( string , source ) {
33
- var index = string . search ( / ( ^ | [ ^ \w \- ] ) c o l o r \( / )
32
+ function transformColor ( string ) {
33
+ return parser ( string ) . walk ( function ( node ) {
34
+ if ( node . type !== "function" || node . value !== "color" ) {
35
+ return
36
+ }
34
37
35
- if ( index === - 1 ) {
36
- return string
37
- }
38
-
39
- // NOTE: regexp search beginning of line of non char symbol before `color(`.
40
- // Offset used for second case.
41
- index = index === 0 ? index : index + 1
42
-
43
- var fn = string . slice ( index )
44
- var balancedMatches = balanced ( "(" , ")" , fn )
45
- if ( ! balancedMatches ) {
46
- throw new Error ( "Missing closing parentheses in '" + string + "'" , source )
47
- }
48
-
49
- return string . slice ( 0 , index ) + colorFn . convert ( "color(" + balancedMatches . body + ")" ) + transformColor ( balancedMatches . post )
38
+ node . value = colorFn . convert ( parser . stringify ( node ) )
39
+ node . type = "word"
40
+ } ) . toString ( )
50
41
}
0 commit comments