22 * Module dependencies.
33 */
44var postcss = require ( "postcss" )
5- var balanced = require ( "balanced-match " )
5+ var parser = require ( "postcss-value-parser " )
66var colorFn = require ( "css-color-function" )
77var helpers = require ( "postcss-message-helpers" )
88
@@ -17,7 +17,7 @@ module.exports = postcss.plugin("postcss-color-function", function() {
1717 }
1818
1919 decl . value = helpers . try ( function transformColorValue ( ) {
20- return transformColor ( decl . value , decl . source )
20+ return transformColor ( decl . value )
2121 } , decl . source )
2222 } )
2323 }
@@ -29,22 +29,13 @@ module.exports = postcss.plugin("postcss-color-function", function() {
2929 * @param {String } string declaration value
3030 * @return {String } converted declaration value to rgba()
3131 */
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+ }
3437
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 ( )
5041}
0 commit comments