Skips empty removal if advanced processing is enabled.
authorGoalSmashers <jakub@goalsmashers.com>
Sun, 17 Nov 2013 14:33:13 +0000 (15:33 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sun, 17 Nov 2013 22:06:26 +0000 (23:06 +0100)
* Since advanced processing tokenizes data we can rebuild it without empty elements.

History.md
lib/clean.js
lib/selectors/optimizer.js
test/unit-test.js

index 3edec0c..385cdb5 100644 (file)
@@ -1,6 +1,7 @@
 [2.1.0 / 2013-xx-xx (UNRELEASED)](https://github.com/GoalSmashers/clean-css/compare/v2.0.0...HEAD)
 ==================
 
+* Skips empty removal if advanced processing is enabled.
 * Fixed issue [#161](https://github.com/GoalSmashers/clean-css/issues/161) - improves tokenizer performance.
 * Fixed issue [#163](https://github.com/GoalSmashers/clean-css/issues/163) - round pixels to 2nd decimal place.
 * Fixed issue [#165](https://github.com/GoalSmashers/clean-css/issues/165) - extra space after trailing parenthesis.
index 262c2b3..bb277b2 100644 (file)
@@ -303,9 +303,11 @@ module.exports = function(options) {
         data.replace(new RegExp('@charset [^;]+;(' + lineBreak + ')?', 'g'), '').trim();
     });
 
-    replace(function removeEmptySelectors() {
-      data = new EmptyRemoval(data).process();
-    });
+    if (options.noAdvanced) {
+      replace(function removeEmptySelectors() {
+        data = new EmptyRemoval(data).process();
+      });
+    }
 
     // trim spaces at beginning and end
     data = data.trim();
index 307057a..5b018be 100644 (file)
@@ -200,17 +200,25 @@ module.exports = function Optimizer(data, options) {
   };
 
   var rebuild = function(tokens) {
-    return (Array.isArray(tokens) ? tokens : [tokens])
-      .map(function(token) {
-        if (typeof token == 'string')
-          return token;
-
-        if (token.block)
-          return token.block + '{' + rebuild(token.body) + '}';
-        else
-          return token.selector + '{' + token.body + '}';
-      })
-      .join(options.keepBreaks ? options.lineBreak : '');
+    var rebuilt = [];
+
+    tokens = Array.isArray(tokens) ? tokens : [tokens];
+    for (var i = 0, l = tokens.length; i < l; i++) {
+      var token = tokens[i];
+
+      if (typeof token == 'string') {
+        rebuilt.push(token);
+        continue;
+      }
+
+      var name = token.block || token.selector;
+      var body = token.block ? rebuild(token.body) : token.body;
+
+      if (body.length > 0)
+        rebuilt.push(name + '{' + body + '}');
+    }
+
+    return rebuilt.join(options.keepBreaks ? options.lineBreak : '');
   };
 
   return {
index 06b2f3a..5fa9beb 100644 (file)
@@ -949,6 +949,16 @@ title']{display:block}",
       ''
     ]
   }),
+  'empty with disabled advanced optimizations': cssContext({
+    'selector': [
+      'a{}p{}',
+      ''
+    ],
+    'media': [
+      '@media screen{}',
+      ''
+    ]
+  }, { noAdvanced: true }),
   '@import': cssContext({
     'empty': [
       "@import url();",