Adds better comments + keepBreaks handling.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 27 Aug 2015 05:50:49 +0000 (06:50 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 27 Aug 2015 06:44:52 +0000 (07:44 +0100)
Basically we should remove a comment AND a subsequent line break when
restoring. This is again due to doing it too late, see #407.

lib/text/comments-processor.js
test/fixtures/issue-543-min.css
test/text/comments-processor-test.js

index 874a216..941fc67 100644 (file)
@@ -98,7 +98,6 @@ CommentsProcessor.prototype.escape = function (data) {
 function restore(context, data, from, isSpecial) {
   var tempData = [];
   var cursor = 0;
-  var addBreak;
 
   for (; cursor < data.length;) {
     var nextMatch = from.nextMatch(data, cursor);
@@ -110,13 +109,12 @@ function restore(context, data, from, isSpecial) {
 
     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 : '');
+      tempData.push(comment);
+
+      cursor = nextMatch.end;
     } else {
-      nextMatch.end += context.keepBreaks ? lineBreak.length : 0;
+      cursor = nextMatch.end + (context.keepBreaks && data[nextMatch.end] == lineBreak ? 1 : 0);
     }
-
-    cursor = nextMatch.end;
   }
 
   return tempData.length > 0 ?
index f4ae973..21def46 100644 (file)
@@ -1,5 +1 @@
-@font-face{src:url(../font/myfont.eot);/*! IE9 */
-/*! IE6-IE8 */
-/*! chrome, firefox */
-/*! chrome, firefox, opera, Safari, Android, iOS 4.2+ */
-src:url(../font/myfont.eot?#iefix) format("embedded-opentype"),url(../font/myfont.woff) format("woff"),url(../font/myfont.ttf) format("truetype"),url(../font/myfont.svg#myfont) format("svg");font-style:normal;font-weight:400}
\ No newline at end of file
+@font-face{src:url(../font/myfont.eot);/*! IE9 *//*! IE6-IE8 *//*! chrome, firefox *//*! chrome, firefox, opera, Safari, Android, iOS 4.2+ */src:url(../font/myfont.eot?#iefix) format("embedded-opentype"),url(../font/myfont.woff) format("woff"),url(../font/myfont.ttf) format("truetype"),url(../font/myfont.svg#myfont) format("svg");font-style:normal;font-weight:400}
\ No newline at end of file
index e301aaa..4e851f4 100644 (file)
@@ -119,7 +119,7 @@ vows.describe(CommentsProcessor)
   )
   .addBatch(
     processorContext('zero with breaks', {
-      'content and special comments': [
+      'remove comment and a break': [
         'a{}' + lineBreak + '/*! some text */' + lineBreak + 'p{}',
         'a{}' + lineBreak + '__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__' + lineBreak + 'p{}',
         'a{}' + lineBreak + 'p{}'
@@ -128,12 +128,12 @@ vows.describe(CommentsProcessor)
   )
   .addBatch(
     processorContext('one with breaks', {
-      'forces break after comments': [
+      'keeps content as is': [
         'a{}/*! some text */p{}',
         'a{}__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__p{}',
-        'a{}/*! some text */' + lineBreak + 'p{}'
+        'a{}/*! some text */p{}'
       ],
-      'if not given already comments': [
+      'keeps if not given already': [
         'a{}/*! some text */' + lineBreak + 'p{}',
         'a{}__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__' + lineBreak + 'p{}',
         'a{}/*! some text */' + lineBreak + 'p{}'