preliminary support for evaluating JavaScript code inside LESS

This commit is contained in:
cloudhead
2010-07-08 19:04:36 +02:00
parent 66fa17b274
commit e36080a3ff
7 changed files with 81 additions and 11 deletions

View File

@@ -0,0 +1,32 @@
(function (tree) {
tree.JavaScript = function (string, index) {
this.expression = string;
this.index = index;
};
tree.JavaScript.prototype = {
toCSS: function () {
return this.evaluated;
},
eval: function (env) {
var result,
expression = new(Function)('return (' + this.expression + ')'),
context = {};
for (var k in env.frames[0].variables()) {
context[k.slice(1)] = env.frames[0].variables()[k].value.eval(env).toCSS();
}
try {
result = expression.call(context);
this.evaluated = JSON.stringify(result);
} catch (e) {
throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
index: this.index };
}
return this;
}
};
})(require('less/tree'));

View File

@@ -55,10 +55,11 @@ tree.mixin.Definition = function (name, params, rules) {
this.frames = [];
};
tree.mixin.Definition.prototype = {
toCSS: function () { return "" },
variable: function (name) { return this.parent.variable.call(this, name) },
find: function () { return this.parent.find.apply(this, arguments) },
rulesets: function () { return this.parent.rulesets.apply(this) },
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) },
rulesets: function () { return this.parent.rulesets.apply(this) },
eval: function (env, args) {
var frame = new(tree.Ruleset)(null, []), context;

View File

@@ -56,17 +56,20 @@ tree.Ruleset.prototype = {
match: function (args) {
return !args || args.length === 0;
},
variable: function (name) {
if (this._variables) { return this._variables[name] }
variables: function () {
if (this._variables) { return this._variables }
else {
return (this._variables = this.rules.reduce(function (hash, r) {
return this._variables = this.rules.reduce(function (hash, r) {
if (r instanceof tree.Rule && r.variable === true) {
hash[r.name] = r;
}
return hash;
}, {}))[name];
}, {});
}
},
variable: function (name) {
return this.variables()[name];
},
rulesets: function () {
if (this._rulesets) { return this._rulesets }
else {