From fb75c42e4bea62eb070b9a46c1a933b092bc1e34 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Thu, 18 Jul 2013 06:48:32 +0100 Subject: [PATCH] move more files over to use genCSS --- lib/less/tree.js | 14 ++++++++++++++ lib/less/tree/dimension.js | 25 +++++++++++-------------- lib/less/tree/directive.js | 20 ++++++-------------- lib/less/tree/element.js | 34 +++++++++++++++++++++------------- lib/less/tree/import.js | 16 ++++++++-------- lib/less/tree/keyword.js | 4 ++-- lib/less/tree/media.js | 25 ++++--------------------- 7 files changed, 66 insertions(+), 72 deletions(-) diff --git a/lib/less/tree.js b/lib/less/tree.js index b974b877..d44d2339 100644 --- a/lib/less/tree.js +++ b/lib/less/tree.js @@ -58,4 +58,18 @@ tree.toCSS = function (env) { return strs.join(''); }; +tree.outputRuleset = function (env, output, rules) { + output.add((env.compress ? '{' : ' {\n')); + env.tabLevel = (env.tabLevel || 0) + 1; + var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "), + tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" "); + for(var i = 0; i < rules.length; i++) { + output.add(tabRuleStr); + rules[i].genCSS(env, output); + output.add(env.compress ? '' : '\n'); + } + env.tabLevel--; + output.add(tabSetStr + "}"); +}; + })(require('./tree')); diff --git a/lib/less/tree/dimension.js b/lib/less/tree/dimension.js index c203fd0f..2097e128 100644 --- a/lib/less/tree/dimension.js +++ b/lib/less/tree/dimension.js @@ -21,9 +21,6 @@ tree.Dimension.prototype = { return new(tree.Color)([this.value, this.value, this.value]); }, genCSS: function (env, output) { - output.add(this.toCSS(env)); - }, - toCSS: function (env) { if ((env && env.strictUnits) && !this.unit.isSingular()) { throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString()); } @@ -39,7 +36,8 @@ tree.Dimension.prototype = { if (env && env.compress) { // Zero values doesn't need a unit if (value === 0 && !this.unit.isAngle()) { - return strValue; + output.add(strValue); + return; } // Float values doesn't need a leading zero @@ -48,8 +46,10 @@ tree.Dimension.prototype = { } } - return strValue + this.unit.toCSS(env); + output.add(strValue); + this.unit.genCSS(env, output); }, + toCSS: tree.toCSS, // In an operation between two Dimensions, // we default to the first Dimension's unit, @@ -188,20 +188,17 @@ tree.Unit.prototype = { return new tree.Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit); }, genCSS: function (env, output) { - output.add(this.toCSS(env)); - }, - toCSS: function (env) { if (this.numerator.length >= 1) { - return this.numerator[0]; - } + output.add(this.numerator[0]); + } else if (this.denominator.length >= 1) { - return this.denominator[0]; - } + output.add(this.denominator[0]); + } else if ((!env || !env.strictUnits) && this.backupUnit) { - return this.backupUnit; + output.add(this.backupUnit); } - return ""; }, + toCSS: tree.toCSS, toString: function () { var i, returnStr = this.numerator.join("*"); diff --git a/lib/less/tree/directive.js b/lib/less/tree/directive.js index e66a21ad..9f2a0f5e 100644 --- a/lib/less/tree/directive.js +++ b/lib/less/tree/directive.js @@ -18,24 +18,16 @@ tree.Directive.prototype = { this.value = visitor.visit(this.value); }, genCSS: function (env, output) { - output.add(this.toCSS(env)); - }, - toCSS: function (env) { + output.add(this.name); if (this.rules) { - //todo put in a common place, also done in media.js - env.tabLevel = (env.tabLevel || 0) + 1; - var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "), - tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" "); - var css = ""; - for(var i = 0; i < this.rules.length; i++) { - css += tabRuleStr + this.rules[i].toCSS(env) + (env.compress ? "" : "\n"); - } - env.tabLevel--; - return this.name + (env.compress ? '{' : ' {\n') + css + (env.compress ? '}': (tabSetStr + '}')); + tree.outputRuleset(env, output, this.rules); } else { - return this.name + ' ' + this.value.toCSS() + ';'; + output.add(' '); + this.value.genCSS(env, output); + output.add(';'); } }, + toCSS: tree.toCSS, eval: function (env) { var evaldDirective = this; if (this.rules) { diff --git a/lib/less/tree/element.js b/lib/less/tree/element.js index 6605b548..b827d84e 100644 --- a/lib/less/tree/element.js +++ b/lib/less/tree/element.js @@ -75,20 +75,28 @@ tree.Combinator = function (value) { }; tree.Combinator.prototype = { type: "Combinator", - genCSS: function (env, output) { - output.add(this.toCSS(env)); + _outputMap: { + '' : '', + ' ' : ' ', + ':' : ' :', + '+' : ' + ', + '~' : ' ~ ', + '>' : ' > ', + '|' : '|' }, - toCSS: function (env) { - return { - '' : '', - ' ' : ' ', - ':' : ' :', - '+' : env.compress ? '+' : ' + ', - '~' : env.compress ? '~' : ' ~ ', - '>' : env.compress ? '>' : ' > ', - '|' : '|' - }[this.value]; - } + _outputMapCompressed: { + '' : '', + ' ' : ' ', + ':' : ' :', + '+' : '+', + '~' : '~', + '>' : '>', + '|' : '|' + }, + genCSS: function (env, output) { + output.add((env.compress ? this._outputMapCompressed : this._outputMap)[this.value]); + }, + toCSS: tree.toCSS }; })(require('../tree')); diff --git a/lib/less/tree/import.js b/lib/less/tree/import.js index 7ad06ee8..b917d6c0 100644 --- a/lib/less/tree/import.js +++ b/lib/less/tree/import.js @@ -49,17 +49,17 @@ tree.Import.prototype = { } }, genCSS: function (env, output) { - output.add(this.toCSS(env)); - }, - toCSS: function (env) { - var features = this.features ? ' ' + this.features.toCSS(env) : ''; - if (this.css) { - return "@import " + this.path.toCSS() + features + ';'; - } else { - return ""; + output.add("@import "); + this.path.genCSS(env, output); + if (this.features) { + output.add(" "); + this.features.genCSS(env, output); + } + output.add(';'); } }, + toCSS: tree.toCSS, getPath: function () { if (this.path instanceof tree.Quoted) { var path = this.path.value; diff --git a/lib/less/tree/keyword.js b/lib/less/tree/keyword.js index 46090d7f..8cfbb09c 100644 --- a/lib/less/tree/keyword.js +++ b/lib/less/tree/keyword.js @@ -5,9 +5,9 @@ tree.Keyword.prototype = { type: "Keyword", eval: function () { return this; }, genCSS: function (env, output) { - output.add(this.toCSS(env)); + output.add(this.value); }, - toCSS: function () { return this.value; }, + toCSS: tree.toCSS, compare: function (other) { if (other instanceof tree.Keyword) { return other.value === this.value ? 0 : 1; diff --git a/lib/less/tree/media.js b/lib/less/tree/media.js index 59e6e4fd..08198404 100644 --- a/lib/less/tree/media.js +++ b/lib/less/tree/media.js @@ -17,28 +17,11 @@ tree.Media.prototype = { this.rules = visitor.visit(this.rules); }, genCSS: function (env, output) { - output.add(this.toCSS(env)); - }, - toCSS: function (env) { - var features = this.features.toCSS(env); - - //todo put in a common place, also done in directive.js - env.tabLevel = (env.tabLevel || 0) + 1; - var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "), - tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" "); - var css = ""; - for(var i = 0; i < this.rules.length; i++) { - css += tabRuleStr + this.rules[i].toCSS(env) + (env.compress ? '' : '\n'); - } - env.tabLevel--; - - if (this.rules.length) { //css.match(/\S/)) { - return '@media ' + features + (env.compress ? '{' : ' {\n') + css + - '}'; - } else { - return ""; - } + output.add('@media '); + this.features.genCSS(env, output); + tree.outputRuleset(env, output, this.rules); }, + toCSS: tree.toCSS, eval: function (env) { if (!env.mediaBlocks) { env.mediaBlocks = [];