expression.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 paren_1 = tslib_1.__importDefault(require("./paren"));
  6. var comment_1 = tslib_1.__importDefault(require("./comment"));
  7. var dimension_1 = tslib_1.__importDefault(require("./dimension"));
  8. var Expression = function (value, noSpacing) {
  9. this.value = value;
  10. this.noSpacing = noSpacing;
  11. if (!value) {
  12. throw new Error('Expression requires an array parameter');
  13. }
  14. };
  15. Expression.prototype = Object.assign(new node_1.default(), {
  16. type: 'Expression',
  17. accept: function (visitor) {
  18. this.value = visitor.visitArray(this.value);
  19. },
  20. eval: function (context) {
  21. var returnValue;
  22. var mathOn = context.isMathOn();
  23. var inParenthesis = this.parens;
  24. var doubleParen = false;
  25. if (inParenthesis) {
  26. context.inParenthesis();
  27. }
  28. if (this.value.length > 1) {
  29. returnValue = new Expression(this.value.map(function (e) {
  30. if (!e.eval) {
  31. return e;
  32. }
  33. return e.eval(context);
  34. }), this.noSpacing);
  35. }
  36. else if (this.value.length === 1) {
  37. if (this.value[0].parens && !this.value[0].parensInOp && !context.inCalc) {
  38. doubleParen = true;
  39. }
  40. returnValue = this.value[0].eval(context);
  41. }
  42. else {
  43. returnValue = this;
  44. }
  45. if (inParenthesis) {
  46. context.outOfParenthesis();
  47. }
  48. if (this.parens && this.parensInOp && !mathOn && !doubleParen
  49. && (!(returnValue instanceof dimension_1.default))) {
  50. returnValue = new paren_1.default(returnValue);
  51. }
  52. return returnValue;
  53. },
  54. genCSS: function (context, output) {
  55. for (var i = 0; i < this.value.length; i++) {
  56. this.value[i].genCSS(context, output);
  57. if (!this.noSpacing && i + 1 < this.value.length) {
  58. output.add(' ');
  59. }
  60. }
  61. },
  62. throwAwayComments: function () {
  63. this.value = this.value.filter(function (v) {
  64. return !(v instanceof comment_1.default);
  65. });
  66. }
  67. });
  68. exports.default = Expression;
  69. //# sourceMappingURL=expression.js.map