Simplifies advanced processing & property optimizer interface.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 6 Oct 2014 14:45:53 +0000 (15:45 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:46 +0000 (21:22 +0100)
* Previously property optimizer accepted a string with selectors & returned a similar list.
* Since advanced optimizer operates on arrays now so does property optimizer.

lib/properties/optimizer.js
lib/properties/token.js
lib/selectors/optimizers/advanced.js

index ad380de..a04d73f 100644 (file)
@@ -117,16 +117,10 @@ module.exports = function Optimizer(compatibility, aggressiveMerging, context) {
   }
 
   var tokenize = function(body, selector) {
-    var tokens = body.split(';');
     var keyValues = [];
 
-    if (tokens.length === 0 || (tokens.length == 1 && tokens[0].indexOf(IE_BACKSLASH_HACK) == -1 && tokens[0][tokens[0].length - 1] != ':'))
-      return;
-
-    for (var i = 0, l = tokens.length; i < l; i++) {
-      var token = tokens[i];
-      if (token === '')
-        continue;
+    for (var i = 0, l = body.length; i < l; i++) {
+      var token = body[i];
 
       var firstColon = token.indexOf(':');
       var property = token.substring(0, firstColon);
@@ -242,12 +236,11 @@ module.exports = function Optimizer(compatibility, aggressiveMerging, context) {
   var rebuild = function(tokens) {
     var flat = [];
 
-
     for (var i = 0, l = tokens.length; i < l; i++) {
       flat.push(tokens[i][0] + ':' + tokens[i][1]);
     }
 
-    return flat.join(';');
+    return flat;
   };
 
   var compact = function (input) {
@@ -264,20 +257,14 @@ module.exports = function Optimizer(compatibility, aggressiveMerging, context) {
   };
 
   return {
-    process: function(body, allowAdjacent, skipCompacting, selector) {
-      var result = body;
-
-      var tokens = tokenize(body, selector);
-      if (tokens) {
-        var optimized = optimize(tokens, allowAdjacent);
-        result = rebuild(optimized);
-      }
-
-      if (!skipCompacting && processableInfo.implementedFor.test(result)) {
-        result = compact(result);
-      }
-
-      return result;
+    process: function(selector, body, allowAdjacent, skipCompacting) {
+      var tokenized = tokenize(body, selector);
+      var optimized = optimize(tokenized, allowAdjacent);
+      var rebuilt = rebuild(optimized);
+
+      return !skipCompacting && processableInfo.implementedFor.test(rebuilt) ?
+        compact(rebuilt) :
+        rebuilt;
     }
   };
 };
index 209c545..ff4e514 100644 (file)
@@ -99,7 +99,7 @@ module.exports = (function() {
     // Breaks up a string of CSS property declarations into tokens so that they can be handled more easily
     Token.tokenize = function (input) {
       // Split the input by semicolons and parse the parts
-      var tokens = input.split(';').map(Token.tokenizeOne);
+      var tokens = input.map(Token.tokenizeOne);
       return tokens;
     };
 
@@ -110,7 +110,7 @@ module.exports = (function() {
         tokens = [tokens];
       }
 
-      var result = '';
+      var result = [];
 
       // This step takes care of putting together the components of shorthands
       // NOTE: this is necessary to do for every shorthand, otherwise we couldn't remove their default values
@@ -124,19 +124,10 @@ module.exports = (function() {
           continue;
         }
 
-        if (t.prop)
-          result += t.prop + ':';
-
-        if (t.value)
-          result += t.value;
-
-        if (t.isImportant)
-          result += important;
-
-        result += ';';
+        result.push(t.prop + ':' + t.value + (t.isImportant ? important : ''));
       }
 
-      return result.substr(0, result.length - 1);
+      return result;
     };
 
     // Gets the final (detokenized) length of the given tokens
index a4cfe61..3b1f1b4 100644 (file)
@@ -62,8 +62,7 @@ AdvancedOptimizer.prototype.mergeAdjacent = function (tokens) {
     // TODO: broken due to joining/splitting
     if (lastToken.selector && token.selector.join(',') == lastToken.selector.join(',')) {
       var joinAt = [lastToken.body.length];
-      // TODO: broken due to joining/splitting
-      lastToken.body = this.propertyOptimizer.process(lastToken.body.concat(token.body).join(';'), joinAt, false, token.selector.join(',')).split(';');
+      lastToken.body = this.propertyOptimizer.process(token.selector, lastToken.body.concat(token.body), joinAt, false);
       forRemoval.push(i);
       // TODO: broken due to joining/splitting
     } else if (lastToken.body && token.body.join(';') == lastToken.body.join(';') && !this.isSpecial(token.selector.join(',')) && !this.isSpecial(lastToken.selector.join(','), this.options)) {
@@ -97,7 +96,6 @@ AdvancedOptimizer.prototype.reduceNonAdjacent = function (tokens) {
       [complexSelector.join(',')];
 
     for (var j = 0, m = selectors.length; j < m; j++) {
-      // TODO: broken due to joining/splitting
       var selector = selectors[j];
 
       if (!candidates[selector])
@@ -206,20 +204,19 @@ AdvancedOptimizer.prototype.reduceSelector = function (tokens, selector, data, o
     var where = data[j].where;
     var token = tokens[where];
     var body = token.body;
-    // TODO: broken due to joining/splitting
-    bodies.push(body.join(';'));
+
+    bodies = bodies.concat(body);
     splitBodies.push(body);
     processedTokens.push(where);
   }
 
-  for (j = 0, m = bodies.length; j < m; j++) {
-    if (bodies[j].length > 0)
+  for (j = 0, m = splitBodies.length; j < m; j++) {
+    if (splitBodies[j].length > 0)
       joinsAt.push((joinsAt[j - 1] || 0) + splitBodies[j].length);
   }
 
-  // TODO: broken due to joining/splitting
-  var optimizedBody = this.propertyOptimizer.process(bodies.join(';'), joinsAt, true, selector);
-  var optimizedProperties = optimizedBody.split(';');
+  var optimizedBody = this.propertyOptimizer.process(selector, bodies, joinsAt, true);
+  var optimizedProperties = optimizedBody;
 
   var processedCount = processedTokens.length;
   var propertyIdx = optimizedProperties.length - 1;
@@ -249,8 +246,7 @@ function optimizeProperties(tokens, propertyOptimizer) {
     var token = tokens[i];
 
     if (token.selector) {
-      // TODO: broken due to joining/splitting
-      token.body = propertyOptimizer.process(token.body.join(';'), false, false, token.selector.join(',')).split(';');
+      token.body = propertyOptimizer.process(token.selector, token.body, false, false);
     } else if (token.block) {
       optimizeProperties(token.body, propertyOptimizer);
     }