Fixes #887 - edge case in serializing comments.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 8 Jul 2017 09:16:40 +0000 (11:16 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 8 Jul 2017 09:49:18 +0000 (11:49 +0200)
Why:

* When `removeEmpty: false` is used then comments are not properly
  removed from a list of tokens.

History.md
lib/optimizer/level-1/optimize.js
test/optimizer/level-1/optimize-test.js

index d8678dd..1d2ba34 100644 (file)
@@ -6,6 +6,11 @@
 * Fixed issue [#895](https://github.com/jakubpawlowicz/clean-css/issues/895) - ignoring specific styles.
 * Fixed issue [#947](https://github.com/jakubpawlowicz/clean-css/issues/947) - selector based filtering.
 
+[4.1.6 / 2017-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.5...4.1)
+==================
+
+* Fixed issue [#887](https://github.com/jakubpawlowicz/clean-css/issues/887) - edge case in serializing comments.
+
 [4.1.5 / 2017-06-29](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.4...v4.1.5)
 ==================
 
index 1cec591..2d08bc7 100644 (file)
@@ -494,10 +494,7 @@ function optimizeBody(rule, properties, context) {
 
   restoreFromOptimizing(_properties);
   removeUnused(_properties);
-
-  if (_properties.length != properties.length) {
-    removeComments(properties, options);
-  }
+  removeComments(properties, options);
 }
 
 function removeComments(tokens, options) {
@@ -657,7 +654,7 @@ function level1Optimize(tokens, context) {
         break;
     }
 
-    if (levelOptions.removeEmpty && (token[1].length === 0 || (token[2] && token[2].length === 0))) {
+    if (token[0] == Token.COMMENT && token[1].length === 0 || levelOptions.removeEmpty && (token[1].length === 0 || (token[2] && token[2].length === 0))) {
       tokens.splice(i, 1);
       i--;
       l--;
index ac8ce88..8b85c61 100644 (file)
@@ -261,6 +261,14 @@ vows.describe('level 1 optimizations')
         'a{/* a comment */}',
         'a{}'
       ],
+      'body with comment and ignored value': [
+        '.block{/* a comment */_color: red}',
+        '.block{}'
+      ],
+      'top level comment': [
+        '/* comment */.block{}',
+        '.block{}'
+      ],
       '@media query': [
         '@media screen{}',
         '@media screen{}'