Fixes #549 - keeping special comments in source maps.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 25 Apr 2015 08:33:54 +0000 (09:33 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 25 Apr 2015 08:42:33 +0000 (09:42 +0100)
Since in source maps we restore content multiple times, the total
important comment counter was not kept as a state thus handled
incorrectly.

History.md
lib/text/comments-processor.js
test/source-map-test.js

index ba02a06..f4461ef 100644 (file)
@@ -14,6 +14,7 @@
 
 * Fixed issue [#543](https://github.com/jakubpawlowicz/clean-css/issues/543) - better "comment in body" handling.
 * Fixed issue [#548](https://github.com/jakubpawlowicz/clean-css/issues/548) - regression in font minifying.
+* Fixed issue [#549](https://github.com/jakubpawlowicz/clean-css/issues/549) - special comments in source maps.
 
 [3.2.4 / 2015-04-24](https://github.com/jakubpawlowicz/clean-css/compare/v3.2.3...v3.2.4)
 ==================
index dd0b07c..874a216 100644 (file)
@@ -12,6 +12,7 @@ function CommentsProcessor(context, keepSpecialComments, keepBreaks, saveWaypoin
   this.specialComments = new EscapeStore('COMMENT_SPECIAL');
 
   this.context = context;
+  this.restored = 0;
   this.keepAll = keepSpecialComments == '*';
   this.keepOne = keepSpecialComments == '1' || keepSpecialComments === 1;
   this.keepBreaks = keepBreaks;
@@ -96,7 +97,6 @@ CommentsProcessor.prototype.escape = function (data) {
 
 function restore(context, data, from, isSpecial) {
   var tempData = [];
-  var restored = 0;
   var cursor = 0;
   var addBreak;
 
@@ -108,8 +108,8 @@ function restore(context, data, from, isSpecial) {
     tempData.push(data.substring(cursor, nextMatch.start));
     var comment = from.restore(nextMatch.match);
 
-    if (isSpecial && (context.keepAll || (context.keepOne && restored === 0))) {
-      restored++;
+    if (isSpecial && (context.keepAll || (context.keepOne && context.restored === 0))) {
+      context.restored++;
       addBreak = context.keepBreaks && data[nextMatch.end] != '\n' && data.lastIndexOf('\r\n', nextMatch.end + 1) != nextMatch.end;
       tempData.push(comment, addBreak ? lineBreak : '');
     } else {
index ec6c8f0..f369d13 100644 (file)
@@ -1033,6 +1033,14 @@ vows.describe('source-map')
       'has right output': function (errors, minified) {
         assert.equal(minified.styles, 'div{color:red!important/*!1*//*!2*/}');
       }
+    },
+    'important comments after a property with remove comments': {
+      'topic': function () {
+        return new CleanCSS({ sourceMap: true, keepSpecialComments: 1 }).minify('div { color: #f00 !important; /*!1*/} /*!2*/ a{/*!3*/}');
+      },
+      'has right output': function (errors, minified) {
+        assert.equal(minified.styles, 'div{color:red!important/*!1*/}a{}');
+      }
     }
   })
   .addBatch({