From fd039d6267ce8303596347e7c13b4d978c83d0f4 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Thu, 30 Jan 2014 08:04:15 +0000 Subject: [PATCH] Fixes #216 - handling 0% in rgb / rgba declarations. --- lib/clean.js | 16 ++++++++++------ test/unit-test.js | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/clean.js b/lib/clean.js index a9e616d3..e0670b04 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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(',') + ')'; }); diff --git a/test/unit-test.js b/test/unit-test.js index 78c251f1..2b663c6b 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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': [ -- 2.34.1