33 */
44var balanced = require ( "balanced-match" )
55var colorFn = require ( "css-color-function" )
6+ var helpers = require ( "postcss-message-helpers" )
67
78/**
89 * PostCSS plugin to transform color()
910 */
1011module . exports = function plugin ( ) {
1112 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 ) {
1415 return
1516 }
1617
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 )
1821 } )
1922 }
2023}
2124
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-
4325/**
4426 * Transform color() to rgb()
4527 *
@@ -55,19 +37,8 @@ function transformColor(string, source) {
5537 var fn = string . slice ( index )
5638 var balancedMatches = balanced ( "(" , ")" , fn )
5739 if ( ! balancedMatches ) {
58- throw new SyntaxError ( gnuMessage ( "Missing closing parentheses in '" + string + "'" , source ) )
40+ throw new Error ( "Missing closing parentheses in '" + string + "'" , source )
5941 }
6042
6143 return string . slice ( 0 , index ) + colorFn . convert ( "color(" + balancedMatches . body + ")" ) + transformColor ( balancedMatches . post )
6244}
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