From 02fd73e617adf45332b6e3a5410b4285e6c99830 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 20 Sep 2015 12:23:06 +0100 Subject: [PATCH] Fixes #626 - edge case in import rebasing. 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 | 1 + lib/urls/reduce.js | 4 +++- test/fixtures/partials/with-commented-import.css | 2 ++ test/module-test.js | 12 +++++++++++- 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/partials/with-commented-import.css diff --git a/History.md b/History.md index 97a423c0..85ba78b4 100644 --- a/History.md +++ b/History.md @@ -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) diff --git a/lib/urls/reduce.js b/lib/urls/reduce.js index daf4ee48..cc4fb4a3 100644 --- a/lib/urls/reduce.js +++ b/lib/urls/reduce.js @@ -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 index 00000000..9cf2b60e --- /dev/null +++ b/test/fixtures/partials/with-commented-import.css @@ -0,0 +1,2 @@ +/* @import */ +@font-face{font-family:"Font";src:url("/path/to/font")} diff --git a/test/module-test.js b/test/module-test.js index 5b425863..0cad3f6b 100644 --- a/test/module-test.js +++ b/test/module-test.js @@ -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': { -- 2.34.1