mirror of
https://github.com/less/less.js.git
synced 2026-01-24 14:48:00 -05:00
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
module.exports = function (tree) {
|
|
|
|
var Operation = function (op, operands, isSpaced) {
|
|
this.op = op.trim();
|
|
this.operands = operands;
|
|
this.isSpaced = isSpaced;
|
|
};
|
|
Operation.prototype = {
|
|
type: "Operation",
|
|
accept: function (visitor) {
|
|
this.operands = visitor.visit(this.operands);
|
|
},
|
|
eval: function (env) {
|
|
var a = this.operands[0].eval(env),
|
|
b = this.operands[1].eval(env);
|
|
|
|
if (env.isMathOn()) {
|
|
if (a instanceof tree.Dimension && b instanceof tree.Color) {
|
|
a = a.toColor();
|
|
}
|
|
if (b instanceof tree.Dimension && a instanceof tree.Color) {
|
|
b = b.toColor();
|
|
}
|
|
if (!a.operate) {
|
|
throw { type: "Operation",
|
|
message: "Operation on an invalid type" };
|
|
}
|
|
|
|
return a.operate(env, this.op, b);
|
|
} else {
|
|
return new(Operation)(this.op, [a, b], this.isSpaced);
|
|
}
|
|
},
|
|
genCSS: function (env, output) {
|
|
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: tree.toCSS
|
|
};
|
|
|
|
// todo move!
|
|
tree.operate = function (env, op, a, b) {
|
|
switch (op) {
|
|
case '+': return a + b;
|
|
case '-': return a - b;
|
|
case '*': return a * b;
|
|
case '/': return a / b;
|
|
}
|
|
};
|
|
|
|
return Operation;
|
|
|
|
};
|