Skip to content

Commit

Permalink
Fixes #986 - CSS 4 colors in level 2 optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubpawlowicz committed Aug 1, 2018
1 parent 8f52600 commit ec259be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -5,6 +5,7 @@
* Fixed issue [#861](https://github.com/jakubpawlowicz/clean-css/issues/861) - new `transition` property optimizer.
* Fixed issue [#895](https://github.com/jakubpawlowicz/clean-css/issues/895) - ignoring specific styles.
* Fixed issue [#947](https://github.com/jakubpawlowicz/clean-css/issues/947) - selector based filtering.
* Fixed issue [#986](https://github.com/jakubpawlowicz/clean-css/issues/986) - level 2 optimizations and CSS 4 colors.
* Fixed issue [#1038](https://github.com/jakubpawlowicz/clean-css/issues/1038) - `font-variation-settings` quoting.
* Fixes ReDOS vulnerabilities in validator code.

Expand Down
9 changes: 6 additions & 3 deletions lib/optimizer/validator.js
Expand Up @@ -8,16 +8,19 @@ var decimalRegex = /[0-9]/;
var functionAnyRegex = new RegExp('^' + functionAnyRegexStr + '$', 'i');
var hslColorRegex = /^hsl\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31}\)|hsla\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+\s{0,31}\)$/;
var identifierRegex = /^(\-[a-z0-9_][a-z0-9\-_]*|[a-z][a-z0-9\-_]*)$/i;
var longHexColorRegex = /^#[0-9a-f]{6}$/i;
var namedEntityRegex = /^[a-z]+$/i;
var prefixRegex = /^-([a-z0-9]|-)*$/i;
var rgbColorRegex = /^rgb\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31}\)|rgba\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\.\d]+\s{0,31}\)$/;
var shortHexColorRegex = /^#[0-9a-f]{3}$/i;
var timingFunctionRegex = /^(cubic\-bezier|steps)\([^\)]+\)$/;
var validTimeUnits = ['ms', 's'];
var urlRegex = /^url\([\s\S]+\)$/i;
var variableRegex = new RegExp('^' + variableRegexStr + '$', 'i');

var eightValueColorRegex = /^#[0-9a-f]{8}$/i;
var fourValueColorRegex = /^#[0-9a-f]{4}$/i;
var sixValueColorRegex = /^#[0-9a-f]{6}$/i;
var threeValueColorRegex = /^#[0-9a-f]{3}$/i;

var DECIMAL_DOT = '.';
var MINUS_SIGN = '-';
var PLUS_SIGN = '+';
Expand Down Expand Up @@ -365,7 +368,7 @@ function isFunction(value) {
}

function isHexColor(value) {
return shortHexColorRegex.test(value) || longHexColorRegex.test(value);
return threeValueColorRegex.test(value) || fourValueColorRegex.test(value) || sixValueColorRegex.test(value) || eightValueColorRegex.test(value);

This comment has been minimized.

Copy link
@alfaproject

alfaproject Aug 2, 2018

This would be more efficient: return /^#(?:(?:[0-9a-fA-F]{2}){2,4}|(?:[0-9a-fA-F]){3})$/.test(value)

This comment has been minimized.

Copy link
@jakubpawlowicz

jakubpawlowicz Aug 3, 2018

Author Collaborator

Don't you need 4 clauses inside regex - for 3, 4, 6, and 8 value hex?

This comment has been minimized.

Copy link
@alfaproject

alfaproject Aug 3, 2018

Sorry, fixed it: https://regexr.com/3tdfg

This comment has been minimized.

Copy link
@jakubpawlowicz

jakubpawlowicz Aug 6, 2018

Author Collaborator

👍 Would you mind sending a PR?

}

function isHslColor(value) {
Expand Down
12 changes: 12 additions & 0 deletions test/optimizer/level-2/optimize-test.js
Expand Up @@ -106,6 +106,18 @@ vows.describe('level 2 optimizer')
]
}, { level: 2 })
)
.addBatch(
optimizerContext('colors', {
'four value colors': [
'.block{border:1px solid #0001}',
'.block{border:1px solid #0001}'
],
'eight value colors': [
'.block{border:1px solid #00000001}',
'.block{border:1px solid #00000001}'
]
}, { level: 2 })
)
.addBatch(
optimizerContext('unit merging', {
'font-size': [
Expand Down

0 comments on commit ec259be

Please sign in to comment.