From: Jakub Pawlowicz Date: Thu, 27 Aug 2015 05:50:49 +0000 (+0100) Subject: Adds better comments + keepBreaks handling. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=59dcc7c259fb281465d8a3563b77ed72c8de5365;p=clean-css.git Adds better comments + keepBreaks handling. Basically we should remove a comment AND a subsequent line break when restoring. This is again due to doing it too late, see #407. --- diff --git a/lib/text/comments-processor.js b/lib/text/comments-processor.js index 874a216b..941fc670 100644 --- a/lib/text/comments-processor.js +++ b/lib/text/comments-processor.js @@ -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 ? diff --git a/test/fixtures/issue-543-min.css b/test/fixtures/issue-543-min.css index f4ae973d..21def463 100644 --- a/test/fixtures/issue-543-min.css +++ b/test/fixtures/issue-543-min.css @@ -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 diff --git a/test/text/comments-processor-test.js b/test/text/comments-processor-test.js index e301aaae..4e851f4f 100644 --- a/test/text/comments-processor-test.js +++ b/test/text/comments-processor-test.js @@ -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{}'