mirror of
https://github.com/less/less.js.git
synced 2026-05-01 03:00:22 -04:00
preliminary support for evaluating JavaScript code inside LESS
This commit is contained in:
32
lib/less/tree/javascript.js
Normal file
32
lib/less/tree/javascript.js
Normal 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'));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user