declaration.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  16. if (k2 === undefined) k2 = k;
  17. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  18. }) : (function(o, m, k, k2) {
  19. if (k2 === undefined) k2 = k;
  20. o[k2] = m[k];
  21. }));
  22. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  23. Object.defineProperty(o, "default", { enumerable: true, value: v });
  24. }) : function(o, v) {
  25. o["default"] = v;
  26. });
  27. var __importStar = (this && this.__importStar) || function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. var __importDefault = (this && this.__importDefault) || function (mod) {
  35. return (mod && mod.__esModule) ? mod : { "default": mod };
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var node_1 = __importDefault(require("./node"));
  39. var value_1 = __importDefault(require("./value"));
  40. var keyword_1 = __importDefault(require("./keyword"));
  41. var anonymous_1 = __importDefault(require("./anonymous"));
  42. var Constants = __importStar(require("../constants"));
  43. var MATH = Constants.Math;
  44. var Declaration = /** @class */ (function (_super) {
  45. __extends(Declaration, _super);
  46. function Declaration(name, value, important, merge, index, currentFileInfo, inline, variable) {
  47. var _this = _super.call(this) || this;
  48. _this.name = name;
  49. _this.value = (value instanceof node_1.default) ? value : new value_1.default([value ? new anonymous_1.default(value) : null]);
  50. _this.important = important ? " " + important.trim() : '';
  51. _this.merge = merge;
  52. _this._index = index;
  53. _this._fileInfo = currentFileInfo;
  54. _this.inline = inline || false;
  55. _this.variable = (variable !== undefined) ? variable
  56. : (name.charAt && (name.charAt(0) === '@'));
  57. _this.allowRoot = true;
  58. _this.setParent(_this.value, _this);
  59. return _this;
  60. }
  61. Declaration.prototype.genCSS = function (context, output) {
  62. output.add(this.name + (context.compress ? ':' : ': '), this.fileInfo(), this.getIndex());
  63. try {
  64. this.value.genCSS(context, output);
  65. }
  66. catch (e) {
  67. e.index = this._index;
  68. e.filename = this._fileInfo.filename;
  69. throw e;
  70. }
  71. output.add(this.important + ((this.inline || (context.lastRule && context.compress)) ? '' : ';'), this._fileInfo, this._index);
  72. };
  73. Declaration.prototype.eval = function (context) {
  74. var mathBypass = false;
  75. var prevMath;
  76. var name = this.name;
  77. var evaldValue;
  78. var variable = this.variable;
  79. if (typeof name !== 'string') {
  80. // expand 'primitive' name directly to get
  81. // things faster (~10% for benchmark.less):
  82. name = (name.length === 1) && (name[0] instanceof keyword_1.default) ?
  83. name[0].value : evalName(context, name);
  84. variable = false; // never treat expanded interpolation as new variable name
  85. }
  86. // @todo remove when parens-division is default
  87. if (name === 'font' && context.math === MATH.ALWAYS) {
  88. mathBypass = true;
  89. prevMath = context.math;
  90. context.math = MATH.PARENS_DIVISION;
  91. }
  92. try {
  93. context.importantScope.push({});
  94. evaldValue = this.value.eval(context);
  95. if (!this.variable && evaldValue.type === 'DetachedRuleset') {
  96. throw { message: 'Rulesets cannot be evaluated on a property.',
  97. index: this.getIndex(), filename: this.fileInfo().filename };
  98. }
  99. var important = this.important;
  100. var importantResult = context.importantScope.pop();
  101. if (!important && importantResult.important) {
  102. important = importantResult.important;
  103. }
  104. return new Declaration(name, evaldValue, important, this.merge, this.getIndex(), this.fileInfo(), this.inline, variable);
  105. }
  106. catch (e) {
  107. if (typeof e.index !== 'number') {
  108. e.index = this.getIndex();
  109. e.filename = this.fileInfo().filename;
  110. }
  111. throw e;
  112. }
  113. finally {
  114. if (mathBypass) {
  115. context.math = prevMath;
  116. }
  117. }
  118. };
  119. Declaration.prototype.makeImportant = function () {
  120. return new Declaration(this.name, this.value, '!important', this.merge, this.getIndex(), this.fileInfo(), this.inline);
  121. };
  122. return Declaration;
  123. }(node_1.default));
  124. function evalName(context, name) {
  125. var value = '';
  126. var i;
  127. var n = name.length;
  128. var output = { add: function (s) { value += s; } };
  129. for (i = 0; i < n; i++) {
  130. name[i].eval(context).genCSS(context, output);
  131. }
  132. return value;
  133. }
  134. Declaration.prototype.type = 'Declaration';
  135. exports.default = Declaration;
  136. //# sourceMappingURL=declaration.js.map