Files
less.js/lib/less/tree/operation.js
2014-10-19 16:57:22 +01:00

49 lines
1.3 KiB
JavaScript

var Node = require("./node"),
Color = require("./color"),
Dimension = require("./dimension");
var Operation = function (op, operands, isSpaced) {
this.op = op.trim();
this.operands = operands;
this.isSpaced = isSpaced;
};
Operation.prototype = new Node();
Operation.prototype.type = "Operation";
Operation.prototype.accept = function (visitor) {
this.operands = visitor.visit(this.operands);
};
Operation.prototype.eval = function (context) {
var a = this.operands[0].eval(context),
b = this.operands[1].eval(context);
if (context.isMathOn()) {
if (a instanceof Dimension && b instanceof Color) {
a = a.toColor();
}
if (b instanceof Dimension && a instanceof Color) {
b = b.toColor();
}
if (!a.operate) {
throw { type: "Operation",
message: "Operation on an invalid type" };
}
return a.operate(context, this.op, b);
} else {
return new Operation(this.op, [a, b], this.isSpaced);
}
};
Operation.prototype.genCSS = function (context, output) {
this.operands[0].genCSS(context, output);
if (this.isSpaced) {
output.add(" ");
}
output.add(this.op);
if (this.isSpaced) {
output.add(" ");
}
this.operands[1].genCSS(context, output);
};
module.exports = Operation;