From 6f1a24d0f917e4ef9d8d03ee130fede7a9d08d89 Mon Sep 17 00:00:00 2001 From: Eduardo Abela Date: Tue, 3 Mar 2015 16:52:09 +0100 Subject: [PATCH] Fixes #476 - keep `@import`s on top when restructuring. It is the same story as with `@charset`s and important comments. --- History.md | 1 + lib/selectors/optimizers/advanced.js | 4 +++- test/selectors/optimizer-test.js | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index d6a5d066d..89c64c58d 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ [3.1.4 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.1.3...3.1) ================== +* Fixes issue [#477](https://github.com/jakubpawlowicz/clean-css/issues/477) - `@import`s order in restructuring. * Fixes issue [#478](https://github.com/jakubpawlowicz/clean-css/issues/478) - ultimate fix to brace whitespace. [3.1.3 / 2015-03-03](https://github.com/jakubpawlowicz/clean-css/compare/v3.1.2...v3.1.3) diff --git a/lib/selectors/optimizers/advanced.js b/lib/selectors/optimizers/advanced.js index 030ee9fc6..08bed221f 100644 --- a/lib/selectors/optimizers/advanced.js +++ b/lib/selectors/optimizers/advanced.js @@ -634,7 +634,9 @@ AdvancedOptimizer.prototype.restructure = function (tokens) { var position = tokens[0] && tokens[0].kind == 'at-rule' && tokens[0].value.indexOf('@charset') === 0 ? 1 : 0; for (; position < tokens.length - 1; position++) { - if (!tokens[position] || tokens[position].kind != 'text' || tokens[position].value.indexOf('__ESCAPED_COMMENT_SPECIAL') !== 0) + var isImportRule = tokens[position].kind === 'at-rule' && tokens[position].value.indexOf('@import') === 0; + var isEscapedCommentSpecial = tokens[position].kind === 'text' && tokens[position].value.indexOf('__ESCAPED_COMMENT_SPECIAL') === 0; + if (!(isImportRule || isEscapedCommentSpecial)) break; } diff --git a/test/selectors/optimizer-test.js b/test/selectors/optimizer-test.js index 443f7a821..d2853193b 100644 --- a/test/selectors/optimizer-test.js +++ b/test/selectors/optimizer-test.js @@ -199,6 +199,14 @@ vows.describe(SelectorsOptimizer) 'with important comment and charset': [ '@charset "utf-8";__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__a{width:100px}div{color:red}.one{display:block}.two{display:inline;color:red}', '@charset "utf-8";__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__.two,div{color:red}a{width:100px}.one{display:block}.two{display:inline}' + ], + 'with charset and import': [ + '@charset "UTF-8";@import url(http://fonts.googleapis.com/css?family=Lora:400,700);a{width:100px}div{color:red}.one{display:block}.two{display:inline;color:red}', + '@charset "UTF-8";@import url(http://fonts.googleapis.com/css?family=Lora:400,700);.two,div{color:red}a{width:100px}.one{display:block}.two{display:inline}' + ], + 'with charset and import and comments': [ + '@charset "UTF-8";@import url(http://fonts.googleapis.com/css?family=Lora:400,700);__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__a{width:100px}div{color:red}.one{display:block}.two{display:inline;color:red}', + '@charset "UTF-8";@import url(http://fonts.googleapis.com/css?family=Lora:400,700);__ESCAPED_COMMENT_SPECIAL_CLEAN_CSS0__.two,div{color:red}a{width:100px}.one{display:block}.two{display:inline}' ] }, { advanced: true }) )