From: Jakub Pawlowicz Date: Thu, 22 Jan 2015 10:52:42 +0000 (+0000) Subject: Fixes #441 - hex to name color converting. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=5f560aeada7b556a4ed2c7bf7bec993c92389869;p=clean-css.git Fixes #441 - hex to name color converting. Apparently if a hex was a substring of a known hex -> name color conversion, then it was also replaced. Fixed by checking if hex are matched fully. --- diff --git a/History.md b/History.md index b88b7bd0..464bdab4 100644 --- a/History.md +++ b/History.md @@ -10,6 +10,11 @@ * Fixed issue [#416](https://github.com/GoalSmashers/clean-css/issues/416) - accepts hash as `minify` argument. * Fixed issue [#435](https://github.com/GoalSmashers/clean-css/issues/435) - background-clip in shorthand. +[3.0.7 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.6...v3.0.7) +================== + +* Fixed issue [#441](https://github.com/GoalSmashers/clean-css/issues/441) - hex to name color converting. + [3.0.6 / 2015-01-20](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.5...v3.0.6) ================== diff --git a/lib/colors/hex-name-shortener.js b/lib/colors/hex-name-shortener.js index fe88fd22..c0862d0e 100644 --- a/lib/colors/hex-name-shortener.js +++ b/lib/colors/hex-name-shortener.js @@ -163,7 +163,7 @@ for (var name in COLORS) { } var toHexPattern = new RegExp('(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig'); -var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')', 'ig'); +var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')([^a-f0-9]|$)', 'ig'); HexNameShortener.shorten = function (value) { var hasHex = value.indexOf('#') > -1; @@ -172,8 +172,8 @@ HexNameShortener.shorten = function (value) { }); if (hasHex) { - shortened = shortened.replace(toNamePattern, function(match, colorValue) { - return toName[colorValue.toLowerCase()]; + shortened = shortened.replace(toNamePattern, function(match, colorValue, suffix) { + return toName[colorValue.toLowerCase()] + suffix; }); } diff --git a/test/selectors/optimizers/simple-test.js b/test/selectors/optimizers/simple-test.js index f10a5e86..d5af0da7 100644 --- a/test/selectors/optimizers/simple-test.js +++ b/test/selectors/optimizers/simple-test.js @@ -188,6 +188,22 @@ vows.describe(SimpleOptimizer) 'transparent non-black hsla': [ 'a{color:rgba(240,0,0,0)}', ['color:rgba(240,0,0,0)'] + ], + 'partial hex to name': [ + 'a{color:#f00000}', + ['color:#f00000'] + ], + 'partial hex further down to name': [ + 'a{background:url(test.png) #f00000}', + ['background:url(test.png) #f00000'] + ], + 'partial name to hex': [ + 'a{color:greyish}', + ['color:greyish'] + ], + 'partial name further down to hex': [ + 'a{background:url(test.png) blueish}', + ['background:url(test.png) blueish'] ] }) )