operation.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var node_1 = tslib_1.__importDefault(require("./node"));
  5. var color_1 = tslib_1.__importDefault(require("./color"));
  6. var dimension_1 = tslib_1.__importDefault(require("./dimension"));
  7. var Constants = tslib_1.__importStar(require("../constants"));
  8. var MATH = Constants.Math;
  9. var Operation = function (op, operands, isSpaced) {
  10. this.op = op.trim();
  11. this.operands = operands;
  12. this.isSpaced = isSpaced;
  13. };
  14. Operation.prototype = Object.assign(new node_1.default(), {
  15. type: 'Operation',
  16. accept: function (visitor) {
  17. this.operands = visitor.visitArray(this.operands);
  18. },
  19. eval: function (context) {
  20. var a = this.operands[0].eval(context), b = this.operands[1].eval(context), op;
  21. if (context.isMathOn(this.op)) {
  22. op = this.op === './' ? '/' : this.op;
  23. if (a instanceof dimension_1.default && b instanceof color_1.default) {
  24. a = a.toColor();
  25. }
  26. if (b instanceof dimension_1.default && a instanceof color_1.default) {
  27. b = b.toColor();
  28. }
  29. if (!a.operate || !b.operate) {
  30. if ((a instanceof Operation || b instanceof Operation)
  31. && a.op === '/' && context.math === MATH.PARENS_DIVISION) {
  32. return new Operation(this.op, [a, b], this.isSpaced);
  33. }
  34. throw { type: 'Operation',
  35. message: 'Operation on an invalid type' };
  36. }
  37. return a.operate(context, op, b);
  38. }
  39. else {
  40. return new Operation(this.op, [a, b], this.isSpaced);
  41. }
  42. },
  43. genCSS: function (context, output) {
  44. this.operands[0].genCSS(context, output);
  45. if (this.isSpaced) {
  46. output.add(' ');
  47. }
  48. output.add(this.op);
  49. if (this.isSpaced) {
  50. output.add(' ');
  51. }
  52. this.operands[1].genCSS(context, output);
  53. }
  54. });
  55. exports.default = Operation;
  56. //# sourceMappingURL=operation.js.map