Fixes removing unused items.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 27 Aug 2015 06:37:29 +0000 (07:37 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 27 Aug 2015 06:44:52 +0000 (07:44 +0100)
Items should be removed by position as it is not guaranteed that
there are no comments somewhere in between. Affects both modes - source
maps on & off.

lib/properties/remove-unused.js
test/properties/remove-unused-test.js

index 9d8cf64..08cb9b6 100644 (file)
@@ -1,7 +1,9 @@
 function removeUnused(properties) {
   for (var i = properties.length - 1; i >= 0; i--) {
-    if (properties[i].unused)
-      properties[i].all.splice(i, 1);
+    var property = properties[i];
+
+    if (property.unused)
+      property.all.splice(property.position, 1);
   }
 }
 
index 59dbd7b..82ab47c 100644 (file)
@@ -22,6 +22,25 @@ vows.describe(removeUnused)
         assert.lengthOf(properties, 1);
         assert.equal(properties[0][0], 'color');
       }
+    },
+    'it respects comments': {
+      'topic': function () {
+        var properties = [
+          [['background'], ['none']],
+          '__ESCAPED_COMMENT_CLEAN_CSS0__',
+          [['color'], ['red']]
+        ];
+        var _properties = wrapForOptimizing(properties);
+        _properties[1].unused = true;
+
+        removeUnused(_properties);
+        return properties;
+      },
+      'it has one property left': function (properties) {
+        assert.lengthOf(properties, 2);
+        assert.equal(properties[0][0], 'background');
+        assert.equal(properties[1], '__ESCAPED_COMMENT_CLEAN_CSS0__');
+      }
     }
   })
   .export(module);