From ea1e42324ea6404bde9e624bd1670ecd0ecd6a36 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Wed, 20 May 2015 21:39:50 +0100 Subject: [PATCH] Fixes #562 - optimizing invalid color values. We'll leave them as is for now as it's too tricky to remove them at this point. --- History.md | 1 + lib/selectors/optimizers/simple.js | 5 ++++- test/selectors/optimizers/simple-test.js | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index e174d7dd..bf67f5cc 100644 --- a/History.md +++ b/History.md @@ -12,6 +12,7 @@ * Fixed issue [#448](https://github.com/jakubpawlowicz/clean-css/issues/448) - rebasing no protocol URIs. * Fixed issue [#517](https://github.com/jakubpawlowicz/clean-css/issues/517) - turning off color optimizations. * Fixed issue [#542](https://github.com/jakubpawlowicz/clean-css/issues/542) - space after closing brace in IE. +* Fixed issue [#562](https://github.com/jakubpawlowicz/clean-css/issues/562) - optimizing invalid color values. * Fixed issue [#563](https://github.com/jakubpawlowicz/clean-css/issues/563) - `background:inherit` restoring. * Fixed issue [#570](https://github.com/jakubpawlowicz/clean-css/issues/570) - rebasing "no-url()" imports. * Fixed issue [#574](https://github.com/jakubpawlowicz/clean-css/issues/574) - rewriting internal URLs. diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index 6582d837..9ad66a15 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -144,7 +144,10 @@ function colorMininifier(_, value, compatibility) { }) .replace(/(rgb|rgba|hsl|hsla)\(([^\)]+)\)/g, function(match, colorFunction, colorDef) { var tokens = colorDef.split(','); - var applies = colorFunction == 'hsl' || colorFunction == 'hsla' || tokens[0].indexOf('%') > -1; + var applies = (colorFunction == 'hsl' && tokens.length == 3) || + (colorFunction == 'hsla' && tokens.length == 4) || + (colorFunction == 'rgb' && tokens.length == 3 && colorDef.indexOf('%') > 0) || + (colorFunction == 'rgba' && tokens.length == 4 && colorDef.indexOf('%') > 0); if (!applies) return match; diff --git a/test/selectors/optimizers/simple-test.js b/test/selectors/optimizers/simple-test.js index 55e6610b..76bfdc77 100644 --- a/test/selectors/optimizers/simple-test.js +++ b/test/selectors/optimizers/simple-test.js @@ -237,6 +237,14 @@ vows.describe(SimpleOptimizer) 'partial name as a suffix': [ 'a{font-family:alrightsanslp-black}', [['font-family', 'alrightsanslp-black']] + ], + 'invalid rgba declaration - color': [ + 'a{color:rgba(255 0 0)}', + [['color', 'rgba(255 0 0)']] + ], + 'invalid rgba declaration - background': [ + 'a{background:rgba(255 0 0)}', + [['background', 'rgba(255 0 0)']] ] }) ) -- 2.34.1