Skip to content

Commit

Permalink
Bubble import only above other non comment, non charset rules. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Mar 21, 2015
1 parent 125a7f0 commit e0dff53
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
3 changes: 0 additions & 3 deletions lib/less/tree/comment.js
Expand Up @@ -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;
51 changes: 25 additions & 26 deletions lib/less/tree/ruleset.js
Expand Up @@ -303,8 +303,6 @@ Ruleset.prototype.genCSS = function (context, output) {
var i, j,
charsetRuleNodes = [],
ruleNodes = [],
rulesetNodes = [],
rulesetNodeCnt,
debugInfo, // Line number debugging
rule,
path;
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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');
}
Expand Down
1 change: 0 additions & 1 deletion test/css/media.css
Expand Up @@ -119,7 +119,6 @@
}
@page :first {
size: 8.5in 11in;

@top-left {
margin: 1cm;
}
Expand Down

0 comments on commit e0dff53

Please sign in to comment.