declaration.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 value_1 = tslib_1.__importDefault(require("./value"));
  6. var keyword_1 = tslib_1.__importDefault(require("./keyword"));
  7. var anonymous_1 = tslib_1.__importDefault(require("./anonymous"));
  8. var Constants = tslib_1.__importStar(require("../constants"));
  9. var MATH = Constants.Math;
  10. function evalName(context, name) {
  11. var value = '';
  12. var i;
  13. var n = name.length;
  14. var output = { add: function (s) { value += s; } };
  15. for (i = 0; i < n; i++) {
  16. name[i].eval(context).genCSS(context, output);
  17. }
  18. return value;
  19. }
  20. var Declaration = function (name, value, important, merge, index, currentFileInfo, inline, variable) {
  21. this.name = name;
  22. this.value = (value instanceof node_1.default) ? value : new value_1.default([value ? new anonymous_1.default(value) : null]);
  23. this.important = important ? " " + important.trim() : '';
  24. this.merge = merge;
  25. this._index = index;
  26. this._fileInfo = currentFileInfo;
  27. this.inline = inline || false;
  28. this.variable = (variable !== undefined) ? variable
  29. : (name.charAt && (name.charAt(0) === '@'));
  30. this.allowRoot = true;
  31. this.setParent(this.value, this);
  32. };
  33. Declaration.prototype = Object.assign(new node_1.default(), {
  34. type: 'Declaration',
  35. genCSS: function (context, output) {
  36. output.add(this.name + (context.compress ? ':' : ': '), this.fileInfo(), this.getIndex());
  37. try {
  38. this.value.genCSS(context, output);
  39. }
  40. catch (e) {
  41. e.index = this._index;
  42. e.filename = this._fileInfo.filename;
  43. throw e;
  44. }
  45. output.add(this.important + ((this.inline || (context.lastRule && context.compress)) ? '' : ';'), this._fileInfo, this._index);
  46. },
  47. eval: function (context) {
  48. var mathBypass = false, prevMath, name = this.name, evaldValue, variable = this.variable;
  49. if (typeof name !== 'string') {
  50. // expand 'primitive' name directly to get
  51. // things faster (~10% for benchmark.less):
  52. name = (name.length === 1) && (name[0] instanceof keyword_1.default) ?
  53. name[0].value : evalName(context, name);
  54. variable = false; // never treat expanded interpolation as new variable name
  55. }
  56. // @todo remove when parens-division is default
  57. if (name === 'font' && context.math === MATH.ALWAYS) {
  58. mathBypass = true;
  59. prevMath = context.math;
  60. context.math = MATH.PARENS_DIVISION;
  61. }
  62. try {
  63. context.importantScope.push({});
  64. evaldValue = this.value.eval(context);
  65. if (!this.variable && evaldValue.type === 'DetachedRuleset') {
  66. throw { message: 'Rulesets cannot be evaluated on a property.',
  67. index: this.getIndex(), filename: this.fileInfo().filename };
  68. }
  69. var important = this.important;
  70. var importantResult = context.importantScope.pop();
  71. if (!important && importantResult.important) {
  72. important = importantResult.important;
  73. }
  74. return new Declaration(name, evaldValue, important, this.merge, this.getIndex(), this.fileInfo(), this.inline, variable);
  75. }
  76. catch (e) {
  77. if (typeof e.index !== 'number') {
  78. e.index = this.getIndex();
  79. e.filename = this.fileInfo().filename;
  80. }
  81. throw e;
  82. }
  83. finally {
  84. if (mathBypass) {
  85. context.math = prevMath;
  86. }
  87. }
  88. },
  89. makeImportant: function () {
  90. return new Declaration(this.name, this.value, '!important', this.merge, this.getIndex(), this.fileInfo(), this.inline);
  91. }
  92. });
  93. exports.default = Declaration;
  94. //# sourceMappingURL=declaration.js.map