From 16642d2a772c31a947ca23d4dc05cb2a50df276d Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 7 Dec 2014 21:44:58 +0000 Subject: [PATCH] Improves selector cleanup. * Removes unnecessary regexp. * Does cleanup and removal of non-understandable selectors in one pass. --- lib/selectors/optimizers/advanced.js | 2 +- lib/selectors/optimizers/clean-up.js | 15 ++++++++------- lib/selectors/optimizers/simple.js | 23 +---------------------- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/lib/selectors/optimizers/advanced.js b/lib/selectors/optimizers/advanced.js index 68188aed..6c3175ce 100644 --- a/lib/selectors/optimizers/advanced.js +++ b/lib/selectors/optimizers/advanced.js @@ -75,7 +75,7 @@ AdvancedOptimizer.prototype.mergeAdjacent = function (tokens) { !this.isSpecial(token.metadata.selector) && !this.isSpecial(lastToken.metadata.selector)) { changeSelectorOf( lastToken, - CleanUp.selectors(lastToken.value.concat(token.value)) + CleanUp.selectors(lastToken.value.concat(token.value), false) ); forRemoval.push(i); } else { diff --git a/lib/selectors/optimizers/clean-up.js b/lib/selectors/optimizers/clean-up.js index 3474971e..d60f79f3 100644 --- a/lib/selectors/optimizers/clean-up.js +++ b/lib/selectors/optimizers/clean-up.js @@ -7,27 +7,28 @@ function selectorSorter(s1, s2) { } var CleanUp = { - selectors: function (selectors) { + selectors: function (selectors, removeUnsupported) { var plain = []; var tokenized = []; for (var i = 0, l = selectors.length; i < l; i++) { var selector = selectors[i]; - var value = selector.value; - var reduced = value - .replace(/\s/g, ' ') - .replace(/\s{2,}/g, ' ') + var reduced = selector.value + .replace(/\s+/g, ' ') .replace(/ ?, ?/g, ',') .replace(/\s*([>\+\~])\s*/g, '$1') .trim(); - if (value.indexOf('*') > -1) { + if (removeUnsupported && (reduced.indexOf('*+html ') != -1 || reduced.indexOf('*:first-child+html ') != -1)) + continue; + + if (reduced.indexOf('*') > -1) { reduced = reduced .replace(/\*([:#\.\[])/g, '$1') .replace(/^(\:first\-child)?\+html/, '*$1+html'); } - if (value.indexOf('[') > -1) + if (reduced.indexOf('[') > -1) reduced = reduced.replace(/\[([^\]]+)\]/g, removeWhitespace); if (plain.indexOf(reduced) == -1) { diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index bf1131a4..f5ce69ae 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -27,27 +27,6 @@ function SimpleOptimizer(options) { options.updateMetadata = this.options.advanced; } -function removeUnsupported(selectors, options) { - if (options.compatibility.selectors.ie7Hack) - return selectors; - - var supported = []; - var values = []; - for (var i = 0, l = selectors.tokenized.length; i < l; i++) { - var selector = selectors.tokenized[i]; - - if (selector.value.indexOf('*+html ') === -1 && selector.value.indexOf('*:first-child+html ') === -1) { - supported.push(selector); - values.push(selector.value); - } - } - - return { - tokenized: supported, - list: values - }; -} - var valueMinifiers = { 'background': function (value) { return value == 'none' || value == 'transparent' ? '0 0' : value; @@ -241,7 +220,7 @@ SimpleOptimizer.prototype.optimize = function(tokens) { break; if (token.kind == 'selector') { - var newSelectors = removeUnsupported(CleanUp.selectors(token.value), options); + var newSelectors = CleanUp.selectors(token.value, !options.compatibility.selectors.ie7Hack); token.value = newSelectors.tokenized; if (token.value.length === 0) { -- 2.34.1