convert the rest of the nodes to use genCSS

This commit is contained in:
Luke Page
2013-07-18 12:47:11 +01:00
parent fb75c42e4b
commit 1cc63d11b4
10 changed files with 58 additions and 71 deletions

View File

@@ -109,7 +109,6 @@ tree.mixin.Definition.prototype = {
this.rules = visitor.visit(this.rules);
this.condition = visitor.visit(this.condition);
},
toCSS: function () { return ""; },
variable: function (name) { return this.parent.variable.call(this, name); },
variables: function () { return this.parent.variables.call(this); },
find: function () { return this.parent.find.apply(this, arguments); },

View File

@@ -9,11 +9,10 @@ tree.Negative.prototype = {
this.value = visitor.visit(this.value);
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function (env) {
return '-' + this.value.toCSS(env);
output.add('-');
this.value.genCSS(env, output)
},
toCSS: tree.toCSS,
eval: function (env) {
if (env.isMathOn()) {
return (new(tree.Operation)('*', [new(tree.Dimension)(-1), this.value])).eval(env);

View File

@@ -35,12 +35,17 @@ tree.Operation.prototype = {
}
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
this.operands[0].genCSS(env, output);
if (this.isSpaced) {
output.add(" ");
}
output.add(this.op);
if (this.isSpaced) {
output.add(" ");
}
this.operands[1].genCSS(env, output)
},
toCSS: function (env) {
var separator = this.isSpaced ? " " : "";
return this.operands[0].toCSS() + separator + this.op + separator + this.operands[1].toCSS();
}
toCSS: tree.toCSS
};
tree.operate = function (env, op, a, b) {

View File

@@ -10,11 +10,11 @@ tree.Paren.prototype = {
this.value = visitor.visit(this.value);
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function (env) {
return '(' + this.value.toCSS(env).trim() + ')';
output.add('(');
this.value.genCSS(env, output);
output.add(')');
},
toCSS: tree.toCSS,
eval: function (env) {
return new(tree.Paren)(this.value.eval(env));
}

View File

@@ -10,15 +10,15 @@ tree.Quoted = function (str, content, escaped, index, currentFileInfo) {
tree.Quoted.prototype = {
type: "Quoted",
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function () {
if (this.escaped) {
return this.value;
} else {
return this.quote + this.value + this.quote;
if (!this.escaped) {
output.add(this.quote);
}
output.add(this.value);
if (!this.escaped) {
output.add(this.quote);
}
},
toCSS: tree.toCSS,
eval: function (env) {
var that = this;
var value = this.value.replace(/`([^`]+)`/g, function (_, exp) {

View File

@@ -17,24 +17,18 @@ tree.Rule.prototype = {
this.value = visitor.visit(this.value);
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function (env) {
if (this.variable) { return ""; }
else {
try {
var css = this.name + (env.compress ? ':' : ': ') +
this.value.toCSS(env) +
this.important + ((this.inline || (env.lastRule && env.compress)) ? "" : ";");
return css;
}
catch(e) {
e.index = this.index;
e.filename = this.currentFileInfo.filename;
throw e;
}
output.add(this.name + (env.compress ? ':' : ': '));
try {
this.value.genCSS(env, output);
}
catch(e) {
e.index = this.index;
e.filename = this.currentFileInfo.filename;
throw e;
}
output.add(this.important + ((this.inline || (env.lastRule && env.compress)) ? "" : ";"));
},
toCSS: tree.toCSS,
eval: function (env) {
var strictMathBypass = false;
if (this.name === "font" && env.strictMath === false) {

View File

@@ -55,29 +55,19 @@ tree.Selector.prototype = {
}), evaldCondition);
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function (env) {
if (!this._css) {
//surprised this caching works since the first element combinator is changed from ' ' to ''
//in the toCSS visitor, when toCSS may have already been called?
//is this caching worth it?
this._css = this.elements.map(function (e) {
if (typeof(e) === 'string') {
return ' ' + e.trim();
} else {
return e.toCSS(env);
}
}).join('');
}
var i, element;
if ((!env || !env.firstSelector) && this.elements[0].combinator.value === "") {
return ' ' + this._css;
output.add(' ');
}
if (!this._css) {
//TODO caching? speed comparison?
for(i = 0; i < this.elements.length; i++) {
element = this.elements[i];
element.genCSS(env, output);
}
}
return this._css;
},
toCSS: tree.toCSS,
markReferenced: function () {
this.isReferenced = true;
},

View File

@@ -6,11 +6,9 @@ tree.UnicodeDescriptor = function (value) {
tree.UnicodeDescriptor.prototype = {
type: "UnicodeDescriptor",
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function (env) {
return this.value;
output.add(this.value);
},
toCSS: tree.toCSS,
eval: function () { return this; }
};

View File

@@ -10,11 +10,11 @@ tree.URL.prototype = {
this.value = visitor.visit(this.value);
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
},
toCSS: function () {
return "url(" + this.value.toCSS() + ")";
output.add("url(");
this.value.genCSS(env, output)
output.add(")");
},
toCSS: tree.toCSS,
eval: function (ctx) {
var val = this.value.eval(ctx), rootpath;

View File

@@ -18,13 +18,15 @@ tree.Value.prototype = {
}
},
genCSS: function (env, output) {
output.add(this.toCSS(env));
var i;
for(i = 0; i < this.value.length; i++) {
this.value[i].genCSS(env, output);
if (i+1 < this.value.length) {
output.add(env.compress ? ',' : ', ');
}
}
},
toCSS: function (env) {
return this.value.map(function (e) {
return e.toCSS(env);
}).join(env.compress ? ',' : ', ');
}
toCSS: tree.toCSS
};
})(require('../tree'));