Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2440 #2520

Merged
merged 2 commits into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/less/tree/comment.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: 1 addition & 0 deletions test/css/import.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@charset "UTF-8";
/** comment at the top**/
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
@import url("//ha.com/file.css") (min-width: 100px);
Expand Down
1 change: 0 additions & 1 deletion test/css/media.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
}
@page :first {
size: 8.5in 11in;

@top-left {
margin: 1cm;
}
Expand Down
1 change: 1 addition & 0 deletions test/less/import.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** comment at the top**/
@import url(http://fonts.googleapis.com/css?family=Open+Sans);

@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
Expand Down