mirror of
https://github.com/less/less.js.git
synced 2026-02-04 20:15:06 -05:00
74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
(function (tree) {
|
|
|
|
//
|
|
// A function call node.
|
|
//
|
|
tree.Call = function (name, args, index, currentFileInfo) {
|
|
this.name = name;
|
|
this.args = args;
|
|
this.index = index;
|
|
this.currentFileInfo = currentFileInfo;
|
|
};
|
|
tree.Call.prototype = {
|
|
type: "Call",
|
|
accept: function (visitor) {
|
|
var args;
|
|
if (args = this.args) {
|
|
this.args = visitor.visitArray(args);
|
|
}
|
|
},
|
|
//
|
|
// When evaluating a function call,
|
|
// we either find the function in `tree.functions` [1],
|
|
// in which case we call it, passing the evaluated arguments,
|
|
// if this returns null or we cannot find the function, we
|
|
// simply print it out as it appeared originally [2].
|
|
//
|
|
// The *functions.js* file contains the built-in functions.
|
|
//
|
|
// The reason why we evaluate the arguments, is in the case where
|
|
// we try to pass a variable to a function, like: `saturate(@color)`.
|
|
// The function should receive the value, not the variable.
|
|
//
|
|
eval: function (env) {
|
|
var args = this.args.map(function (a) { return a.eval(env); }),
|
|
nameLC = this.name.toLowerCase(),
|
|
result, func;
|
|
|
|
if (nameLC in tree.functions) { // 1.
|
|
try {
|
|
func = new tree.functionCall(env, this.currentFileInfo);
|
|
result = func[nameLC].apply(func, args);
|
|
/*jshint eqnull:true */
|
|
if (result != null) {
|
|
return result;
|
|
}
|
|
} catch (e) {
|
|
throw { type: e.type || "Runtime",
|
|
message: "error evaluating function `" + this.name + "`" +
|
|
(e.message ? ': ' + e.message : ''),
|
|
index: this.index, filename: this.currentFileInfo.filename };
|
|
}
|
|
}
|
|
|
|
return new tree.Call(this.name, args, this.index, this.currentFileInfo);
|
|
},
|
|
|
|
genCSS: function (env, output) {
|
|
output.add(this.name + "(", this.currentFileInfo, this.index);
|
|
|
|
for(var i = 0; i < this.args.length; i++) {
|
|
this.args[i].genCSS(env, output);
|
|
if (i + 1 < this.args.length) {
|
|
output.add(", ");
|
|
}
|
|
}
|
|
|
|
output.add(")");
|
|
},
|
|
|
|
toCSS: tree.toCSS
|
|
};
|
|
|
|
})(require('../tree'));
|