Refactored some of the evaluation mechanisms

- Don't call `eval` from `toCSS`
- Every node responds to `eval`
This commit is contained in:
cloudhead
2010-05-07 23:21:16 -04:00
parent 07d5c20862
commit f5f0919349
10 changed files with 24 additions and 28 deletions

View File

@@ -406,7 +406,7 @@ less.Parser = function Parser(env) {
value = $(this.entities.quoted) || $(/[-a-zA-Z0-9_%@$\/.&=:;#+?]+/g);
if (! $(')')) throw new(Error)("missing closing ) for url()");
return new(tree.URL)(value);
return new(tree.URL)(value.value ? value : new(tree.Anonymous)(value));
},
//
@@ -719,7 +719,7 @@ less.Parser = function Parser(env) {
if (name = $(this.property) || $(this.variable)) {
if ((name[0] != '@') && (match = peek(/([^@+\/*(;{}-]*);/g))) {
i += match[0].length - 1;
value = match[1];
value = new(tree.Anonymous)(match[1]);
} else if (name === "font") {
value = $(this.font);
} else {

View File

@@ -5,6 +5,7 @@ tree.Alpha = function Alpha(val) {
};
tree.Alpha.prototype = {
toCSS: function () {
return "alpha(opacity=" + (this.value.toCSS ? this.value.toCSS() : this.value) + ")";
}
return "alpha(opacity=" + this.value.toCSS() + ")";
},
eval: function () { return this }
};

View File

@@ -6,5 +6,5 @@ tree.Comment = function Comment(value) {
tree.Comment.prototype = {
toCSS: function () {
return this.value;
}
},
};

View File

@@ -18,3 +18,4 @@ tree.Directive.prototype.toCSS = function () {
return this.name + ' ' + this.value.toCSS() + ';\n';
}
};
tree.Directive.prototype.eval = function () { return this }

View File

@@ -11,14 +11,9 @@ tree.Expression.prototype = {
return this.value[0].eval(env);
}
},
toCSS: function (env) {
var evaled;
evaled = this.value.map(function (e) {
if (e.eval) {
e = e.eval(env);
}
return e.toCSS ? e.toCSS(env) : e;
});
return evaled.join(' ');
toCSS: function () {
return this.value.map(function (e) {
return e.toCSS();
}).join(' ');
}
};

View File

@@ -69,7 +69,7 @@ tree.mixin.Definition.prototype = {
for (var i = 0; i < Math.min(argsLength, this.arity); i++) {
if (!this.params[i].name) {
if (args[i].wildcard) { continue }
else if (args[i].toCSS(env) != this.params[i].value.toCSS(env)) {
else if (args[i].eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
return false;
}
}

View File

@@ -8,11 +8,10 @@ tree.Rule = function Rule(name, value) {
this.variable = true;
} else { this.variable = false }
};
tree.Rule.prototype.toCSS = function (env) {
tree.Rule.prototype.toCSS = function () {
if (this.variable) { return "" }
else {
return this.name + ": " +
(this.value.toCSS ? this.value.toCSS(env) : this.value) + ";";
return this.name + ": " + this.value.toCSS() + ";";
}
};
@@ -27,17 +26,16 @@ tree.Value = function Value(value) {
tree.Value.prototype = {
eval: function (env) {
if (this.value.length === 1) {
return this.value[0].eval ? this.value[0].eval(env)
: this.value[0];
return this.value[0].eval(env);
} else {
return new(tree.Value)(this.value.map(function (v) {
return v.eval(env);
}));
}
},
toCSS: function (env) {
toCSS: function () {
return this.value.map(function (e) {
return e.toCSS ? e.toCSS(env) : e;
return e.toCSS();
}).join(', ');
}
};
@@ -50,5 +48,6 @@ tree.Shorthand = function Shorthand(a, b) {
tree.Shorthand.prototype = {
toCSS: function (env) {
return this.a.toCSS(env) + "/" + this.b.toCSS(env);
}
},
eval: function () { return this }
};

View File

@@ -16,7 +16,7 @@ tree.Ruleset.prototype = {
} else if (rule instanceof tree.mixin.Call) {
Array.prototype.push.apply(rules, rule.eval(context));
} else {
rules.push(rule.eval ? rule.eval(context) : rule);
rules.push(rule.eval(context));
}
});
this.rules = rules;
@@ -126,7 +126,7 @@ tree.Ruleset.prototype = {
}
} else {
if (rule.toCSS && !rule.variable) {
rules.push(rule.toCSS(env));
rules.push(rule.eval(env).toCSS());
} else if (rule.value && !rule.variable) {
rules.push(rule.value.toString());
}

View File

@@ -5,6 +5,7 @@ tree.URL = function URL(val) {
};
tree.URL.prototype = {
toCSS: function () {
return "url(" + (this.value.toCSS ? this.value.toCSS() : this.value) + ")";
}
return "url(" + this.value.toCSS() + ")";
},
eval: function () { return this }
};

View File

@@ -2,7 +2,6 @@ if (typeof(require) !== 'undefined') { var tree = require('less/tree') }
tree.Variable = function Variable(name) { this.name = name };
tree.Variable.prototype = {
toCSS: function (env) { return this.eval(env).toCSS(env) },
eval: function (env) {
var variable, v, name = this.name;