Fixes #626 - edge case in import rebasing.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 20 Sep 2015 11:23:06 +0000 (12:23 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 21 Sep 2015 08:35:25 +0000 (09:35 +0100)
When an import statement appeared inside a comment we were
still rebasing such imports if a quote appeared in subsequent
block of text.

History.md
lib/urls/reduce.js
test/fixtures/partials/with-commented-import.css [new file with mode: 0644]
test/module-test.js

index 97a423c..85ba78b 100644 (file)
@@ -6,6 +6,7 @@
 [3.4.4 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.3...3.4)
 ==================
 
+* Fixed issue [#626](https://github.com/jakubpawlowicz/clean-css/issues/626) - edge case in import rebasing.
 * Fixed issue [#674](https://github.com/jakubpawlowicz/clean-css/issues/674) - adjacent merging order.
 
 [3.4.3 / 2015-09-15](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.2...v3.4.3)
index daf4ee4..cc4fb4a 100644 (file)
@@ -6,6 +6,8 @@ var DATA_URI_PREFIX = 'data:';
 var IMPORT_URL_PREFIX = '@import';
 var UPPERCASE_IMPORT_URL_PREFIX = '@IMPORT';
 
+var COMMENT_END_MARKER = /\*\//;
+
 function byUrl(data, context, callback) {
   var nextStart = 0;
   var nextStartUpperCase = 0;
@@ -125,7 +127,7 @@ function byImport(data, context, callback) {
     nextEnd = data.indexOf(withQuote, nextStart + 1);
 
     untilNextQuote = data.substring(nextImport, nextEnd);
-    if (nextEnd == -1 || /^@import\s+(url\(|__ESCAPED)/i.test(untilNextQuote)) {
+    if (nextEnd == -1 || /^@import\s+(url\(|__ESCAPED)/i.test(untilNextQuote) || COMMENT_END_MARKER.test(untilNextQuote)) {
       cursor = nextStart;
       break;
     }
diff --git a/test/fixtures/partials/with-commented-import.css b/test/fixtures/partials/with-commented-import.css
new file mode 100644 (file)
index 0000000..9cf2b60
--- /dev/null
@@ -0,0 +1,2 @@
+/* @import */
+@font-face{font-family:"Font";src:url("/path/to/font")}
index 5b42586..0cad3f6 100644 (file)
@@ -258,6 +258,16 @@ vows.describe('module tests').addBatch({
       });
     }
   },
+  'local imports': {
+    'inside a comment preceding a quote': {
+      'topic': function () {
+        new CleanCSS().minify('@import "test/fixtures/partials/with-commented-import.css";', this.callback);
+      },
+      'has right output': function (errors, minified) {
+        assert.equal(minified.styles, '@font-face{font-family:Font;src:url(/path/to/font)}');
+      }
+    }
+  },
   'external imports and no callback': {
     'without content': {
       'topic': function () {
@@ -603,7 +613,7 @@ vows.describe('module tests').addBatch({
           assert.equal(minified.styles, '.one{color:red}.three{color:#0f0}.four{color:#00f}.two{color:#fff}');
         }
       },
-      'with two import URLs when one is a string plus a callback 123': {
+      'with two import URLs when one is a string plus a callback': {
         'topic': function () {
           new CleanCSS().minify({
             'main.css': {