Fixes #441 - hex to name color converting.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 22 Jan 2015 10:52:42 +0000 (10:52 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 22 Jan 2015 10:59:23 +0000 (10:59 +0000)
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.

History.md
lib/colors/hex-name-shortener.js
test/selectors/optimizers/simple-test.js

index b88b7bd..464bdab 100644 (file)
 * 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)
 ==================
 
index fe88fd2..c0862d0 100644 (file)
@@ -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;
     });
   }
 
index f10a5e8..d5af0da 100644 (file)
@@ -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']
       ]
     })
   )