Fixes #959 - regression in shortening long hex values.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 2 Sep 2017 17:15:27 +0000 (19:15 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 2 Sep 2017 18:46:20 +0000 (20:46 +0200)
Introduced in #945.

History.md
lib/optimizer/level-1/optimize.js
test/optimizer/level-1/optimize-test.js

index 56bfe1c..50d7f84 100644 (file)
@@ -9,6 +9,7 @@
 [4.1.8 / 2017-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.7...4.1)
 ==================
 
+* Fixed issue [#959](https://github.com/jakubpawlowicz/clean-css/issues/959) - regression in shortening long hex values.
 * Fixed issue [#960](https://github.com/jakubpawlowicz/clean-css/issues/960) - better explanation of `efficiency` stat.
 
 [4.1.7 / 2017-07-14](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.6...v4.1.7)
index 574267c..d54fb53 100644 (file)
@@ -31,6 +31,7 @@ var DEFAULT_ROUNDING_PRECISION = require('../../options/rounding-precision').DEF
 var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/;
 var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/;
 
+var HEX_VALUE_PATTERN = /[0-9a-f]/i;
 var PROPERTY_NAME_PATTERN = /^(?:\-chrome\-|\-[\w\-]+\w|\w[\w\-]+\w|\-\-\S+)$/;
 var IMPORT_PREFIX_PATTERN = /^@import/i;
 var QUOTED_PATTERN = /^('.*'|".*")$/;
@@ -100,11 +101,15 @@ function optimizeColors(name, value, compatibility) {
     .replace(/hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/g, function (match, hue, saturation, lightness) {
       return shortenHsl(hue, saturation, lightness);
     })
-    .replace(/(^|[^='"])#([0-9a-f]{6})($|[^0-9a-f])/gi, function (match, prefix, color, suffix) {
-      if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) {
-        return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase() + suffix;
+    .replace(/(^|[^='"])#([0-9a-f]{6})/gi, function (match, prefix, color, at, inputValue) {
+      var suffix = inputValue[at + match.length];
+
+      if (suffix && HEX_VALUE_PATTERN.test(suffix)) {
+        return match;
+      } else if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) {
+        return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase();
       } else {
-        return (prefix + '#' + color).toLowerCase() + suffix;
+        return (prefix + '#' + color).toLowerCase();
       }
     })
     .replace(/(^|[^='"])#([0-9a-f]{3})/gi, function (match, prefix, color) {
index 9dd549d..7f2d477 100644 (file)
@@ -401,6 +401,10 @@ vows.describe('level 1 optimizations')
         'a{color:#FFF}',
         'a{color:#fff}'
       ],
+      'uppercase long hex to lowercase hex inside gradient 1234': [
+        '.block{background-image:linear-gradient(to top,#AABBCC,#FFFFFF)}',
+        '.block{background-image:linear-gradient(to top,#abc,#fff)}'
+      ],
       '4-value hex': [
         '.block{color:#0f0a}',
         '.block{color:#0f0a}'