move more files over to use genCSS

This commit is contained in:
Luke Page
2013-07-18 06:48:32 +01:00
parent aab9c1b24f
commit fb75c42e4b
7 changed files with 66 additions and 72 deletions

View File

@@ -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'));

View File

@@ -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("*");

View File

@@ -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) {

View File

@@ -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'));

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 = [];