Fixes #411 - stringifying properties with important comments.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 19 Dec 2014 20:33:51 +0000 (20:33 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 19 Dec 2014 20:33:51 +0000 (20:33 +0000)
History.md
lib/selectors/source-map-stringifier.js
lib/selectors/stringifier.js
test/integration-test.js
test/source-map-test.js

index 6cc98b9..5d309eb 100644 (file)
@@ -1,3 +1,8 @@
+[3.0.1 / 2014-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.0.0...v3.0.1)
+==================
+
+* Fixed issue [#411](https://github.com/GoalSmashers/clean-css/issues/411) - properties and important comments.
+
 [3.0.0 / 2014-12-18](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.22...v3.0.0)
 ==================
 
index f9a7d3e..16ca2d1 100644 (file)
@@ -38,20 +38,21 @@ Rebuilder.prototype.relativePathResolver = function (sourcePath, sourceRelativeT
 };
 
 Rebuilder.prototype.rebuildValue = function (list, separator) {
-  var lastEscaped = false;
+  var escaped = 0;
 
   for (var i = 0, l = list.length; i < l; i++) {
     var el = list[i];
 
     if (el.value.indexOf('__ESCAPED_') === 0) {
-      if (!lastEscaped)
-        this.output.pop();
       this.store(el);
-      lastEscaped = true;
+      escaped++;
+
+      if (i === l - 1 && escaped > 0)
+        this.output.splice(this.output.length - escaped - 1, 1);
     } else {
       this.store(el);
       this.store(i < l - 1 ? separator : '');
-      lastEscaped = false;
+      escaped = 0;
     }
   }
 };
index baa0010..1d9bb46 100644 (file)
@@ -7,17 +7,19 @@ function Stringifier(options, restoreCallback) {
 
 function valueRebuilder(list, separator) {
   var merged = '';
-  var lastEscaped = false;
 
   for (var i = 0, l = list.length; i < l; i++) {
     var el = list[i];
 
     if (el.value.indexOf('__ESCAPED_') === 0) {
-      merged = (lastEscaped ? merged : merged.substring(0, merged.length - 1)) + el.value;
-      lastEscaped = true;
+      merged += el.value;
+
+      if (i === l - 1) {
+        var lastSemicolonAt = merged.lastIndexOf(';');
+        merged = merged.substring(0, lastSemicolonAt) + merged.substring(lastSemicolonAt + 1);
+      }
     } else {
       merged += list[i].value + (i < l - 1 ? separator : '');
-      lastEscaped = false;
     }
   }
 
index d7fcbae..19c9c45 100644 (file)
@@ -297,12 +297,16 @@ vows.describe('integration tests').addBatch({
       ''
     ],
     'important after value': [
-      'div{color:red!important;/*!comment*/}',
-      'div{color:red!important/*!comment*/}'
+      'div{color:red;/*!comment*/}',
+      'div{color:red/*!comment*/}'
+    ],
+    'important between values': [
+      'div{color:red;/*!comment*/display:block}',
+      'div{color:red;/*!comment*/display:block}'
     ],
     'two important after value': [
-      'div{color:red!important;/*!1*//*!2*/}',
-      'div{color:red!important/*!1*//*!2*/}'
+      'div{color:red;/*!1*//*!2*/}',
+      'div{color:red/*!1*//*!2*/}'
     ]
   }),
   'escaping': cssContext({
index 5359d54..9b84cb8 100644 (file)
@@ -670,6 +670,12 @@ vows.describe('source-map')
         assert.equal(minified.styles, 'div{color:red!important/*!comment*/}');
       }
     },
+    'important comment between properties': {
+      'topic': new CleanCSS({ sourceMap: true }).minify('div { color: #f00 !important; /*!comment*/; display: block }'),
+      'has right output': function (errors, minified) {
+        assert.equal(minified.styles, 'div{color:red!important;/*!comment*/display:block}');
+      }
+    },
     'important comments after a property': {
       'topic': new CleanCSS({ sourceMap: true }).minify('div { color: #f00 !important; /*!1*//*!2*/ }'),
       'has right output': function (errors, minified) {