Skip to content

Commit

Permalink
Fixes #959 - regression in shortening long hex values.
Browse files Browse the repository at this point in the history
Introduced in #945.
  • Loading branch information
jakubpawlowicz committed Sep 2, 2017
1 parent e30dac5 commit 0f2d4fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions History.md
@@ -1,6 +1,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)
Expand Down
13 changes: 9 additions & 4 deletions lib/optimizer/level-1/optimize.js
Expand Up @@ -29,6 +29,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 = /^('.*'|".*")$/;
Expand Down Expand Up @@ -98,11 +99,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) {
Expand Down
4 changes: 4 additions & 0 deletions test/optimizer/level-1/optimize-test.js
Expand Up @@ -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}'
Expand Down

0 comments on commit 0f2d4fb

Please sign in to comment.