diff --git a/lib/less/tree/comment.js b/lib/less/tree/comment.js index 25842a547..e0562b2ba 100644 --- a/lib/less/tree/comment.js +++ b/lib/less/tree/comment.js @@ -22,7 +22,4 @@ Comment.prototype.isSilent = function(context) { Comment.prototype.markReferenced = function () { this.isReferenced = true; }; -Comment.prototype.isRulesetLike = function(root) { - return Boolean(root); -}; module.exports = Comment; diff --git a/lib/less/tree/ruleset.js b/lib/less/tree/ruleset.js index ea2948f5b..2b2da9358 100644 --- a/lib/less/tree/ruleset.js +++ b/lib/less/tree/ruleset.js @@ -303,8 +303,6 @@ Ruleset.prototype.genCSS = function (context, output) { var i, j, charsetRuleNodes = [], ruleNodes = [], - rulesetNodes = [], - rulesetNodeCnt, debugInfo, // Line number debugging rule, path; @@ -319,31 +317,38 @@ Ruleset.prototype.genCSS = function (context, output) { tabSetStr = context.compress ? '' : Array(context.tabLevel).join(" "), sep; - function isRulesetLikeNode(rule, root) { + function isRulesetLikeNode(rule) { // if it has nested rules, then it should be treated like a ruleset // medias and comments do not have nested rules, but should be treated like rulesets anyway // some directives and anonymous nodes are ruleset like, others are not if (typeof rule.isRulesetLike === "boolean") { return rule.isRulesetLike; } else if (typeof rule.isRulesetLike === "function") { - return rule.isRulesetLike(root); + return rule.isRulesetLike(); } //anything else is assumed to be a rule return false; } + var charsetNodeIndex = 0; + var importNodeIndex = 0; for (i = 0; i < this.rules.length; i++) { rule = this.rules[i]; - if (isRulesetLikeNode(rule, this.root)) { - rulesetNodes.push(rule); - } else { - //charsets should float on top of everything - if (rule.isCharset && rule.isCharset()) { - charsetRuleNodes.push(rule); - } else { - ruleNodes.push(rule); + if (rule.type === "Comment") { + if (importNodeIndex === i) { + importNodeIndex++; } + ruleNodes.push(rule); + } else if (rule.isCharset && rule.isCharset()) { + ruleNodes.splice(charsetNodeIndex, 0, rule); + charsetNodeIndex++; + importNodeIndex++; + } else if (rule.type === "Import") { + ruleNodes.splice(importNodeIndex, 0, rule); + importNodeIndex++; + } else { + ruleNodes.push(rule); } } ruleNodes = charsetRuleNodes.concat(ruleNodes); @@ -384,18 +389,23 @@ Ruleset.prototype.genCSS = function (context, output) { for (i = 0; i < ruleNodes.length; i++) { rule = ruleNodes[i]; - // @page{ directive ends up with root elements inside it, a mix of rules and rulesets - // In this instance we do not know whether it is the last property - if (i + 1 === ruleNodes.length && (!this.root || rulesetNodes.length === 0 || this.firstRoot)) { + if (i + 1 === ruleNodes.length) { context.lastRule = true; } + var currentLastRule = context.lastRule; + if (isRulesetLikeNode(rule)) { + context.lastRule = false; + } + if (rule.genCSS) { rule.genCSS(context, output); } else if (rule.value) { output.add(rule.value.toString()); } + context.lastRule = currentLastRule; + if (!context.lastRule) { output.add(context.compress ? '' : ('\n' + tabRuleStr)); } else { @@ -408,17 +418,6 @@ Ruleset.prototype.genCSS = function (context, output) { context.tabLevel--; } - sep = (context.compress ? "" : "\n") + (this.root ? tabRuleStr : tabSetStr); - rulesetNodeCnt = rulesetNodes.length; - if (rulesetNodeCnt) { - if (ruleNodes.length && sep) { output.add(sep); } - rulesetNodes[0].genCSS(context, output); - for (i = 1; i < rulesetNodeCnt; i++) { - if (sep) { output.add(sep); } - rulesetNodes[i].genCSS(context, output); - } - } - if (!output.isEmpty() && !context.compress && this.firstRoot) { output.add('\n'); } diff --git a/test/css/media.css b/test/css/media.css index 607f0e44e..4e88ea698 100644 --- a/test/css/media.css +++ b/test/css/media.css @@ -119,7 +119,6 @@ } @page :first { size: 8.5in 11in; - @top-left { margin: 1cm; }