Clarifies conditions in advanced processing.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 26 Oct 2014 11:32:38 +0000 (11:32 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 8 Dec 2014 09:39:14 +0000 (09:39 +0000)
lib/selectors/optimizers/advanced.js

index 4566776..d1da995 100644 (file)
@@ -100,7 +100,8 @@ AdvancedOptimizer.prototype.reduceNonAdjacent = function (tokens) {
     if (token.kind != 'selector')
       continue;
 
-    var selectors = token.value.length > 1 && !this.isSpecial(token.metadata.selector) ?
+    var isComplexAndNotSpecial = token.value.length > 1 && !this.isSpecial(token.metadata.selector);
+    var selectors = isComplexAndNotSpecial ?
       [token.metadata.selector].concat(token.metadata.selectorsList) :
       [token.metadata.selector];
 
@@ -114,8 +115,9 @@ AdvancedOptimizer.prototype.reduceNonAdjacent = function (tokens) {
 
       candidates[selector].push({
         where: i,
-        partial: selector != token.metadata.selector,
-        list: token.metadata.selectorsList
+        list: token.metadata.selectorsList,
+        isPartial: isComplexAndNotSpecial && j > 0,
+        isComplex: isComplexAndNotSpecial && j === 0
       });
     }
   }
@@ -133,16 +135,13 @@ AdvancedOptimizer.prototype.reduceSimpleNonAdjacentCases = function (tokens, rep
     var selector = repeated[i];
     var data = candidates[selector];
 
-    if (data.length < 2)
-      continue;
-
     /* jshint loopfunc: true */
     this.reduceSelector(tokens, selector, data, {
       filterOut: function (idx, bodies) {
-        return data[idx].partial && bodies.length === 0;
+        return data[idx].isPartial && bodies.length === 0;
       },
       callback: function (token, newBody, processedCount, tokenIdx) {
-        if (!data[processedCount - tokenIdx - 1].partial) {
+        if (!data[processedCount - tokenIdx - 1].isPartial) {
           changeBodyOf(token, newBody);
           reduced = true;
         }
@@ -158,10 +157,10 @@ AdvancedOptimizer.prototype.reduceComplexNonAdjacentCases = function (tokens, ca
 
   allSelectors:
   for (var complexSelector in candidates) {
-    if (complexSelector.indexOf(',') == -1)
+    var into = candidates[complexSelector];
+    if (!into[0].isComplex)
       continue;
 
-    var into = candidates[complexSelector];
     var intoPosition = into[into.length - 1].where;
     var intoToken = tokens[intoPosition];