Fixes #216 - handling 0% in rgb / rgba declarations.
authorGoalSmashers <jakub@goalsmashers.com>
Thu, 30 Jan 2014 08:04:15 +0000 (08:04 +0000)
committerGoalSmashers <jakub@goalsmashers.com>
Thu, 30 Jan 2014 08:04:15 +0000 (08:04 +0000)
lib/clean.js
test/unit-test.js

index a9e616d..e0670b0 100644 (file)
@@ -272,13 +272,17 @@ var minify = function(data, callback) {
     return (nonZeroPart ? '.' : '') + nonZeroPart + suffix;
   });
 
-  // restore 0% in hsl/hsla
-  replace(/(hsl|hsla)\(([^\)]+)\)/g, function(match, colorFunction, colorDef) {
+  // restore % in rgb/rgba and hsl/hsla
+  replace(/(rgb|rgba|hsl|hsla)\(([^\)]+)\)/g, function(match, colorFunction, colorDef) {
     var tokens = colorDef.split(',');
-    if (tokens[1] == '0')
-      tokens[1] = '0%';
-    if (tokens[2] == '0')
-      tokens[2] = '0%';
+    var applies = colorFunction == 'hsl' || colorFunction == 'hsla' || tokens[0].indexOf('%') > -1;
+    if (!applies)
+      return match;
+
+    if (tokens[1].indexOf('%') == -1)
+      tokens[1] += '%';
+    if (tokens[2].indexOf('%') == -1)
+      tokens[2] += '%';
     return colorFunction + '(' + tokens.join(',') + ')';
   });
 
index 78c251f..2b663c6 100644 (file)
@@ -626,6 +626,8 @@ vows.describe('clean-units').addBatch({
     'colors in ie filters': 'a{filter:chroma(color=#ffffff)}',
     'colors in ie filters 2': "a{progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000')}",
     'colors in ie filters 3': "a{progid:DXImageTransform.Microsoft.gradient(startColorstr='#DDDDDD', endColorstr='#333333')}",
+    'rgb percents': 'a{color:rgb(100%,0%,0%)}',
+    'rgba percents': 'a{color:rgba(100%,0%,0%,.5)}',
     'hsla percents': 'a{color:hsla(1,0%,0%,.5)}',
     'hsla custom ': 'a{color:hsl(80,30%,50%,.5)}',
     'hsl to hex #1': [