Reworked shortening named colours to hex.
authorGoalSmashers <jakub@goalsmashers.com>
Sun, 18 Nov 2012 07:16:20 +0000 (07:16 +0000)
committerGoalSmashers <jakub@goalsmashers.com>
Sun, 18 Nov 2012 07:25:22 +0000 (07:25 +0000)
lib/clean.js
test/data/big-min.css
test/unit-test.js

index 79b9a58..83f062a 100644 (file)
@@ -121,12 +121,12 @@ var CleanCSS = {
     });
 
     // replace standard colors with hex values (only if color name is longer then hex value)
-    replace(/(color|background):(\w+)/g, function(match, property, colorName) {
-      if (CleanCSS.colors[colorName])
-        return property + ':' + CleanCSS.colors[colorName];
-      else
-        return match;
-    });
+    var pattern = "(" + Object.keys(CleanCSS.colors).join('|') + ")";
+    var colorSwitcher = function(match, prefix, colorName, suffix) {
+      return prefix + CleanCSS.colors[colorName] + suffix;
+    };
+    replace(new RegExp("(.)" + pattern + "([;\\}!\)])", 'g'), colorSwitcher);
+    replace(new RegExp("(,)" + pattern + "(,)", 'g'), colorSwitcher);
 
     // replace #f00 with red as it's shorter
     replace(/([: ,\(])#f00/g, '$1red');
index 1dda627..0aa8ba2 100644 (file)
@@ -2103,7 +2103,7 @@ a.god:hover{background:#3c3c3c;color:#fff;text-decoration:none}
 #bar-liberation .god .godenabled a{background:#3c3c3c;color:#fff}
 #bar-liberation .god a.godenter{background:url(http://s0.libe.com/libe/img/common/icon_godenter.png?9ffa63824b5c) no-repeat center center #fff}
 #bar-liberation .god a.godquit{background:url(http://s0.libe.com/libe/img/common/icon_godquit.png?a59104f30cfb) no-repeat center center #000}
-#bar-liberation .god a.godquit:hover{background:url(http://s0.libe.com/libe/img/common/icon_godenter.png?9ffa63824b5c) no-repeat center center yellow}
+#bar-liberation .god a.godquit:hover{background:url(http://s0.libe.com/libe/img/common/icon_godenter.png?9ffa63824b5c) no-repeat center center #ff0}
 #bar-liberation .god a.jumptoadmin{background:url(http://s0.libe.com/back/img/icon_home.png?c1de55b52ccc) no-repeat center center #fff}
 #bar-liberation .god a.jumptoedit{background:url(http://s0.libe.com/back/img/icon_changelink.png?4a31d309d5db) no-repeat center center #fff}
 #mainContent .god{font-size:10px;padding:6px;border-radius:2px;-moz-border-radius:4px;-webkit-border-radius:4px}
index 9c5a2e5..0dc5cae 100644 (file)
@@ -230,7 +230,7 @@ vows.describe('clean-units').addBatch({
       'a{outline:0}'
     ],
     'display:none not changed': 'a{display:none}',
-    'longer background declaration not changed': 'html{background:none repeat scroll 0 0 white}',
+    'longer background declaration not changed': 'html{background:none repeat scroll 0 0 #fff}',
     'mixed zeros not changed': 'div{margin:0 0 1px 2px}',
     'mixed zeros not changed #2': 'div{padding:0 1px 0 3px}',
     'mixed zeros not changed #3': 'div{padding:10px 0 0 1px}',
@@ -381,6 +381,14 @@ vows.describe('clean-units').addBatch({
       'a{color:white;border-color:black;background-color:fuchsia}p{background:yellow}',
       'a{color:#fff;border-color:#000;background-color:#f0f}p{background:#ff0}'
     ],
+    'color names to hex values with important': [
+      'a{color:white !important}',
+      'a{color:#fff!important}'
+    ],
+    'color names to hex values in gradients': [
+      'p{background:linear-gradient(-90deg,black,white)}',
+      'p{background:linear-gradient(-90deg,#000,#fff)}'
+    ],
     'hex value to color name if shorter': [
       'p{color:#f00}',
       'p{color:red}'
@@ -397,9 +405,18 @@ vows.describe('clean-units').addBatch({
       'p{background:-webkit-gradient(linear, left top, left bottom, from(#000), to(#f00))}',
       'p{background:-webkit-gradient(linear,left top,left bottom,from(#000),to(red))}'
     ],
-    'border color': [
-      'p{border:1px solid #f94311}',
-      'p{border:1px solid #f94311}'
+    'border color - keep unchanged': 'p{border:1px solid #f94311}',
+    'border color - hex to name': [
+      'p{border:1em dotted #f00}',
+      'p{border:1em dotted red}'
+    ],
+    'border color - name to hex': [
+      'p{border:1em dotted white}',
+      'p{border:1em dotted #fff}'
+    ],
+    'border color - rgb': [
+      'p{border:1em dotted rgb(255,0,0)}',
+      'p{border:1em dotted red}'
     ],
     'colors and colons': 'a{background-image:linear-gradient(top,red,#e6e6e6)}',
     'colors and parentheses': 'a{background-image:-webkit-gradient(linear,0 0,0 100%,from(#fff),to(#e6e6e6))}'