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,23 +29,11 @@ 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' ) return
34
35
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
- var leadingSpacings = balancedMatches . pre . match ( / ^ \s * / ) [ 0 ]
49
-
50
- return string . slice ( 0 , index ) + leadingSpacings + colorFn . convert ( "color(" + balancedMatches . body + ")" ) + transformColor ( balancedMatches . post )
36
+ node . value = colorFn . convert ( parser . stringify ( node ) )
37
+ node . type = 'word'
38
+ } ) . toString ( )
51
39
}
0 commit comments