From e7c81125b776cb3cd220bbde8b877a46f65f5002 Mon Sep 17 00:00:00 2001 From: jurcovicovam Date: Sun, 3 Aug 2014 10:37:10 +0200 Subject: [PATCH] Charsets should float on top #2126 --- lib/less/tree/directive.js | 5 ++++- lib/less/tree/ruleset.js | 9 ++++++++- test/less/import.less | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/less/tree/directive.js b/lib/less/tree/directive.js index 3d598f43..9b0eb0f4 100644 --- a/lib/less/tree/directive.js +++ b/lib/less/tree/directive.js @@ -24,7 +24,10 @@ tree.Directive.prototype = { } }, isRulesetLike: function() { - return "@charset" !== this.name; + return !this.isCharset(); + }, + isCharset: function() { + return "@charset" === this.name; }, genCSS: function (env, output) { var value = this.value, rules = this.rules; diff --git a/lib/less/tree/ruleset.js b/lib/less/tree/ruleset.js index 510c1f18..17c0ad1a 100644 --- a/lib/less/tree/ruleset.js +++ b/lib/less/tree/ruleset.js @@ -266,6 +266,7 @@ tree.Ruleset.prototype = { }, genCSS: function (env, output) { var i, j, + charsetRuleNodes = [], ruleNodes = [], rulesetNodes = [], rulesetNodeCnt, @@ -306,9 +307,15 @@ tree.Ruleset.prototype = { if (isRulesetLikeNode(rule, this.root)) { rulesetNodes.push(rule); } else { - ruleNodes.push(rule); + //charsets should float on top of everything + if (rule.isCharset && rule.isCharset()) { + charsetRuleNodes.push(rule); + } else { + ruleNodes.push(rule); + } } } + ruleNodes = charsetRuleNodes.concat(ruleNodes); // If this is the root node, we don't render // a selector, or {}. diff --git a/test/less/import.less b/test/less/import.less index 98573cf3..9f8b2914 100644 --- a/test/less/import.less +++ b/test/less/import.less @@ -1,4 +1,3 @@ -@charset "UTF-8"; // stay on top #2013 @import url(http://fonts.googleapis.com/css?family=Open+Sans); @import url(/absolute/something.css) screen and (color) and (max-width: 600px); @@ -23,4 +22,7 @@ @media print { @import (multiple) "import/import-test-e"; -} \ No newline at end of file +} + +@charset "UTF-8"; // climb on top #2126 +