Improves selector cleanup.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 7 Dec 2014 21:44:58 +0000 (21:44 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 8 Dec 2014 09:42:55 +0000 (09:42 +0000)
* Removes unnecessary regexp.
* Does cleanup and removal of non-understandable selectors in one pass.

lib/selectors/optimizers/advanced.js
lib/selectors/optimizers/clean-up.js
lib/selectors/optimizers/simple.js

index 68188ae..6c3175c 100644 (file)
@@ -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 {
index 3474971..d60f79f 100644 (file)
@@ -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) {
index bf1131a..f5ce69a 100644 (file)
@@ -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) {