Fixes #198 - correctly handle a mixup of comments and @import statements.
authorGoalSmashers <jakub@goalsmashers.com>
Fri, 27 Dec 2013 11:27:18 +0000 (12:27 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sat, 4 Jan 2014 16:03:24 +0000 (17:03 +0100)
This is a follow up to #192 which apparently fixed an edge case of processing
more than one @import inside a comment but introduced this issue.

History.md
lib/imports/inliner.js
test/data/issue-198-min.css [new file with mode: 0644]
test/data/issue-198.css [new file with mode: 0644]

index d46fa50..2e1249c 100644 (file)
@@ -12,6 +12,7 @@
 [2.0.6 / 2014-xx-xx (UNRELEASED)](https://github.com/GoalSmashers/clean-css/compare/v2.0.5...v2.0.6)
 ==================
 
+* Fixed issue [#198](https://github.com/GoalSmashers/clean-css/issues/198) - process comments and `@import`s correctly.
 * Fixed issue [#205](https://github.com/GoalSmashers/clean-css/issues/205) - freeze on broken @import declaration.
 
 [2.0.5 / 2014-01-03](https://github.com/GoalSmashers/clean-css/compare/v2.0.4...v2.0.5)
index f67ad59..95bd13f 100644 (file)
@@ -44,6 +44,7 @@ module.exports = function Inliner(context) {
 
   var commentScanner = function(data) {
     var commentRegex = /(\/\*(?!\*\/)[\s\S]*?\*\/)/;
+    var lastStartIndex = 0;
     var lastEndIndex = 0;
     var noComments = false;
 
@@ -60,7 +61,7 @@ module.exports = function Inliner(context) {
         return false;
 
       // idx can be still within last matched comment (many @import statements inside one comment)
-      if (idx < lastEndIndex)
+      if (idx > lastStartIndex && idx < lastEndIndex)
         return true;
 
       comment = data.match(commentRegex);
@@ -71,7 +72,7 @@ module.exports = function Inliner(context) {
       }
 
       // get the indexes relative to the current data chunk
-      localStartIndex = comment.index;
+      lastStartIndex = localStartIndex = comment.index;
       localEndIndex = localStartIndex + comment[0].length;
 
       // calculate the indexes relative to the full original data
diff --git a/test/data/issue-198-min.css b/test/data/issue-198-min.css
new file mode 100644 (file)
index 0000000..78b32f5
--- /dev/null
@@ -0,0 +1,3 @@
+.one{color:red}
+.three{background-image:url(partials/extra/down.gif)}
+.imports-with-comment{color:#999}
diff --git a/test/data/issue-198.css b/test/data/issue-198.css
new file mode 100644 (file)
index 0000000..1ef2995
--- /dev/null
@@ -0,0 +1,4 @@
+@import url('./partials/one.css');
+@import url('./partials/three.css');
+/*some comment, you guys!*/
+.imports-with-comment { color: #999; }