mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
Move more toCSS logic into the toCSS visitor
This commit is contained in:
@@ -21,6 +21,10 @@
|
||||
return [];
|
||||
},
|
||||
|
||||
visitExtend: function (extendNode, visitArgs) {
|
||||
return [];
|
||||
},
|
||||
|
||||
visitComment: function (commentNode, visitArgs) {
|
||||
if (commentNode.isSilent(this._env)) {
|
||||
return [];
|
||||
@@ -28,6 +32,16 @@
|
||||
return commentNode;
|
||||
},
|
||||
|
||||
visitMedia: function(mediaNode, visitArgs) {
|
||||
mediaNode.accept(this._visitor);
|
||||
visitArgs.visitDeeper = false;
|
||||
|
||||
if (!mediaNode.rules.length) {
|
||||
return [];
|
||||
}
|
||||
return mediaNode;
|
||||
},
|
||||
|
||||
visitDirective: function(directiveNode, visitArgs) {
|
||||
if (directiveNode.currentFileInfo.reference && !directiveNode.isReferenced) {
|
||||
return [];
|
||||
|
||||
@@ -32,7 +32,7 @@ tree.Media.prototype = {
|
||||
}
|
||||
env.tabLevel--;
|
||||
|
||||
if (css.match(/\S/)) {
|
||||
if (this.rules.length) { //css.match(/\S/)) {
|
||||
return '@media ' + features + (env.compress ? '{' : ' {\n') + css +
|
||||
'}';
|
||||
} else {
|
||||
|
||||
@@ -25,7 +25,7 @@ tree.Rule.prototype = {
|
||||
try {
|
||||
var css = this.name + (env.compress ? ':' : ': ') +
|
||||
this.value.toCSS(env) +
|
||||
this.important + (this.inline ? "" : ";");
|
||||
this.important + ((this.inline || (env.lastRule && env.compress)) ? "" : ";");
|
||||
return css;
|
||||
}
|
||||
catch(e) {
|
||||
|
||||
@@ -217,63 +217,53 @@ tree.Ruleset.prototype = {
|
||||
for (i = 0; i < ruleNodes.length; i++) {
|
||||
rule = ruleNodes[i];
|
||||
|
||||
if (i + 1 === ruleNodes.length) {
|
||||
env.lastRule = true;
|
||||
}
|
||||
|
||||
if (rule.toCSS) {
|
||||
rules.push(rule.toCSS(env));
|
||||
} else if (rule.value) {
|
||||
rules.push(rule.value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO - remove in toCSS visitor
|
||||
rules = rules.filter(function(rule) { return !!rule; });
|
||||
|
||||
//TODO move to env and rule.js
|
||||
// Remove last semicolon
|
||||
if (env.compress && rules.length) {
|
||||
rule = rules[rules.length - 1];
|
||||
if (rule.charAt(rule.length - 1) === ';') {
|
||||
rules[rules.length - 1] = rule.substring(0, rule.length - 1);
|
||||
}
|
||||
env.lastRule = false;
|
||||
}
|
||||
|
||||
// If this is the root node, we don't render
|
||||
// a selector, or {}.
|
||||
// Otherwise, only output if this ruleset has rules.
|
||||
if (this.root) {
|
||||
if (rules.length > 0) {
|
||||
css.push(rules.join(env.compress ? '' : ('\n' + tabRuleStr))/* + '\n'*/);
|
||||
}
|
||||
css.push(rules.join(env.compress ? '' : ('\n' + tabRuleStr)));
|
||||
} else {
|
||||
if (rules.length > 0) {
|
||||
debugInfo = tree.debugInfo(env, this, tabSetStr);
|
||||
selector = this.paths
|
||||
.map(function (p) {
|
||||
return p.map(function (s) {
|
||||
return s.toCSS(env);
|
||||
}).join('').trim();
|
||||
}).join(env.compress ? ',' : (',\n' + tabSetStr));
|
||||
debugInfo = tree.debugInfo(env, this, tabSetStr);
|
||||
selector = this.paths
|
||||
.map(function (p) {
|
||||
return p.map(function (s) {
|
||||
return s.toCSS(env);
|
||||
}).join('').trim();
|
||||
}).join(env.compress ? ',' : (',\n' + tabSetStr));
|
||||
|
||||
if (selector) {
|
||||
//TODO need to do this in the toCSS visitor
|
||||
//only bother doing if compression is on?
|
||||
// Remove duplicates
|
||||
for (var i = rules.length - 1; i >= 0; i--) {
|
||||
if (rules[i].slice(0, 2) === "/*" || _rules.indexOf(rules[i]) === -1) {
|
||||
_rules.unshift(rules[i]);
|
||||
}
|
||||
if (selector) {
|
||||
//TODO need to do this in the toCSS visitor
|
||||
//only bother doing if compression is on?
|
||||
// Remove duplicates
|
||||
for (var i = rules.length - 1; i >= 0; i--) {
|
||||
if (rules[i].slice(0, 2) === "/*" || _rules.indexOf(rules[i]) === -1) {
|
||||
_rules.unshift(rules[i]);
|
||||
}
|
||||
rules = _rules;
|
||||
|
||||
if (debugInfo) {
|
||||
css.push(debugInfo);
|
||||
css.push(tabSetStr);
|
||||
}
|
||||
|
||||
css.push(selector +
|
||||
(env.compress ? '{' : ' {\n') + tabRuleStr +
|
||||
rules.join(env.compress ? '' : ('\n' + tabRuleStr)) +
|
||||
(env.compress ? '}' : '\n' + tabSetStr + '}'));
|
||||
}
|
||||
rules = _rules;
|
||||
|
||||
if (debugInfo) {
|
||||
css.push(debugInfo);
|
||||
css.push(tabSetStr);
|
||||
}
|
||||
|
||||
css.push(selector +
|
||||
(env.compress ? '{' : ' {\n') + tabRuleStr +
|
||||
rules.join(env.compress ? '' : ('\n' + tabRuleStr)) +
|
||||
(env.compress ? '}' : '\n' + tabSetStr + '}'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,18 +273,14 @@ tree.Ruleset.prototype = {
|
||||
|
||||
var firstRuleset = true;
|
||||
for (i = 0; i < rulesetNodes.length; i++) {
|
||||
var rulesetCSS = rulesetNodes[i].toCSS(env);
|
||||
//TODO need to remove empty rulesets in the toCSS visitor
|
||||
if (rulesetCSS) {
|
||||
if (rules.length && firstRuleset) {
|
||||
rulesets.push("\n" + (this.root ? tabRuleStr : tabSetStr));
|
||||
}
|
||||
if (!firstRuleset) {
|
||||
rulesets.push('\n' + (this.root ? tabRuleStr : tabSetStr));
|
||||
}
|
||||
firstRuleset = false;
|
||||
rulesets.push(rulesetCSS);
|
||||
if (rules.length && firstRuleset) {
|
||||
rulesets.push("\n" + (this.root ? tabRuleStr : tabSetStr));
|
||||
}
|
||||
if (!firstRuleset) {
|
||||
rulesets.push('\n' + (this.root ? tabRuleStr : tabSetStr));
|
||||
}
|
||||
firstRuleset = false;
|
||||
rulesets.push(rulesetNodes[i].toCSS(env));
|
||||
}
|
||||
|
||||
css = css.concat(rulesets);
|
||||
@@ -302,9 +288,6 @@ tree.Ruleset.prototype = {
|
||||
return css.join('') + (!env.compress && this.firstRoot ? '\n' : '');
|
||||
},
|
||||
|
||||
toCSSRoot: function (env) {
|
||||
},
|
||||
|
||||
markReferenced: function () {
|
||||
for (var s = 0; s < this.selectors.length; s++) {
|
||||
this.selectors[s].markReferenced();
|
||||
|
||||
Reference in New Issue
Block a user