From: Jakub Pawlowicz Date: Fri, 30 Jan 2015 22:32:25 +0000 (+0000) Subject: Fixes #450 - color names to hex codes. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=63b6543b376c55c866abec8f4ba563d343f7678a;p=clean-css.git Fixes #450 - color names to hex codes. Regexp matching color names was too greedy and was matching some other values as well. On the downside we need to run the replacement twice. On the upside it extracts replacement functions so we get a slight speed boost. --- diff --git a/History.md b/History.md index 6fac3ec4..8e528843 100644 --- a/History.md +++ b/History.md @@ -14,6 +14,11 @@ * Fixed issue [#442](https://github.com/GoalSmashers/clean-css/issues/442) - space before adjacent `nav`. * Fixed issue [#445](https://github.com/GoalSmashers/clean-css/issues/445) - regression issue in url processor. +[3.0.8 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.7...v3.0.8) +================== + +* Fixed issue [#450](https://github.com/GoalSmashers/clean-css/issues/450) - name to hex color converting. + [3.0.7 / 2015-01-22](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.6...v3.0.7) ================== diff --git a/lib/colors/hex-name-shortener.js b/lib/colors/hex-name-shortener.js index c0862d0e..2af2f4e2 100644 --- a/lib/colors/hex-name-shortener.js +++ b/lib/colors/hex-name-shortener.js @@ -162,22 +162,25 @@ for (var name in COLORS) { toHex[name] = hex; } -var toHexPattern = new RegExp('(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig'); +var toHexPattern = new RegExp('(^| |,|\\))(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig'); var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')([^a-f0-9]|$)', 'ig'); +function hexConverter(match, prefix, colorValue, suffix) { + return prefix + toHex[colorValue.toLowerCase()] + suffix; +} + +function nameConverter(match, colorValue, suffix) { + return toName[colorValue.toLowerCase()] + suffix; +} + HexNameShortener.shorten = function (value) { var hasHex = value.indexOf('#') > -1; - var shortened = value.replace(toHexPattern, function(match, colorValue, suffix) { - return toHex[colorValue.toLowerCase()] + suffix; - }); + var shortened = value.replace(toHexPattern, hexConverter); - if (hasHex) { - shortened = shortened.replace(toNamePattern, function(match, colorValue, suffix) { - return toName[colorValue.toLowerCase()] + suffix; - }); - } + if (shortened != value) + shortened = shortened.replace(toHexPattern, hexConverter); - return shortened; + return hasHex ? shortened.replace(toNamePattern, nameConverter) : shortened; }; module.exports = HexNameShortener; diff --git a/test/selectors/optimizers/simple-test.js b/test/selectors/optimizers/simple-test.js index da90646b..813de290 100644 --- a/test/selectors/optimizers/simple-test.js +++ b/test/selectors/optimizers/simple-test.js @@ -220,6 +220,10 @@ vows.describe(SimpleOptimizer) 'partial name further down to hex': [ 'a{background:url(test.png) blueish}', ['background:url(test.png) blueish'] + ], + 'partial name as a suffix': [ + 'a{font-family:alrightsanslp-black}', + ['font-family:alrightsanslp-black'] ] }) )