processor.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var parser_1 = __importDefault(require("./parser"));
  7. var Processor = /** @class */ (function () {
  8. function Processor(func, options) {
  9. this.func = func || function noop() { };
  10. this.funcRes = null;
  11. this.options = options;
  12. }
  13. Processor.prototype._shouldUpdateSelector = function (rule, options) {
  14. if (options === void 0) { options = {}; }
  15. var merged = Object.assign({}, this.options, options);
  16. if (merged.updateSelector === false) {
  17. return false;
  18. }
  19. else {
  20. return typeof rule !== "string";
  21. }
  22. };
  23. Processor.prototype._isLossy = function (options) {
  24. if (options === void 0) { options = {}; }
  25. var merged = Object.assign({}, this.options, options);
  26. if (merged.lossless === false) {
  27. return true;
  28. }
  29. else {
  30. return false;
  31. }
  32. };
  33. Processor.prototype._root = function (rule, options) {
  34. if (options === void 0) { options = {}; }
  35. var parser = new parser_1.default(rule, this._parseOptions(options));
  36. return parser.root;
  37. };
  38. Processor.prototype._parseOptions = function (options) {
  39. var merged = Object.assign({}, this.options, options);
  40. return {
  41. lossy: this._isLossy(merged),
  42. maxNestingDepth: merged.maxNestingDepth,
  43. };
  44. };
  45. Processor.prototype._stringifyOptions = function (options) {
  46. var merged = Object.assign({}, this.options, options);
  47. return {
  48. maxNestingDepth: merged.maxNestingDepth,
  49. };
  50. };
  51. Processor.prototype._run = function (rule, options) {
  52. var _this = this;
  53. if (options === void 0) { options = {}; }
  54. return new Promise(function (resolve, reject) {
  55. try {
  56. var root_1 = _this._root(rule, options);
  57. Promise.resolve(_this.func(root_1))
  58. .then(function (transform) {
  59. var string = undefined;
  60. if (_this._shouldUpdateSelector(rule, options)) {
  61. string = root_1.toString(_this._stringifyOptions(options));
  62. rule.selector = string;
  63. }
  64. return { transform: transform, root: root_1, string: string };
  65. })
  66. .then(resolve, reject);
  67. }
  68. catch (e) {
  69. reject(e);
  70. return;
  71. }
  72. });
  73. };
  74. Processor.prototype._runSync = function (rule, options) {
  75. if (options === void 0) { options = {}; }
  76. var root = this._root(rule, options);
  77. var transform = this.func(root);
  78. if (transform && typeof transform.then === "function") {
  79. throw new Error("Selector processor returned a promise to a synchronous call.");
  80. }
  81. var string = undefined;
  82. if (options.updateSelector && typeof rule !== "string") {
  83. string = root.toString(this._stringifyOptions(options));
  84. rule.selector = string;
  85. }
  86. return { transform: transform, root: root, string: string };
  87. };
  88. /**
  89. * Process rule into a selector AST.
  90. *
  91. * @param rule {postcss.Rule | string} The css selector to be processed
  92. * @param options The options for processing
  93. * @returns {Promise<parser.Root>} The AST of the selector after processing it.
  94. */
  95. Processor.prototype.ast = function (rule, options) {
  96. return this._run(rule, options).then(function (result) { return result.root; });
  97. };
  98. /**
  99. * Process rule into a selector AST synchronously.
  100. *
  101. * @param rule {postcss.Rule | string} The css selector to be processed
  102. * @param options The options for processing
  103. * @returns {parser.Root} The AST of the selector after processing it.
  104. */
  105. Processor.prototype.astSync = function (rule, options) {
  106. return this._runSync(rule, options).root;
  107. };
  108. /**
  109. * Process a selector into a transformed value asynchronously
  110. *
  111. * @param rule {postcss.Rule | string} The css selector to be processed
  112. * @param options The options for processing
  113. * @returns {Promise<any>} The value returned by the processor.
  114. */
  115. Processor.prototype.transform = function (rule, options) {
  116. return this._run(rule, options).then(function (result) { return result.transform; });
  117. };
  118. /**
  119. * Process a selector into a transformed value synchronously.
  120. *
  121. * @param rule {postcss.Rule | string} The css selector to be processed
  122. * @param options The options for processing
  123. * @returns {any} The value returned by the processor.
  124. */
  125. Processor.prototype.transformSync = function (rule, options) {
  126. return this._runSync(rule, options).transform;
  127. };
  128. /**
  129. * Process a selector into a new selector string asynchronously.
  130. *
  131. * @param rule {postcss.Rule | string} The css selector to be processed
  132. * @param options The options for processing
  133. * @returns {string} the selector after processing.
  134. */
  135. Processor.prototype.process = function (rule, options) {
  136. var _this = this;
  137. return this._run(rule, options).then(function (result) { return result.string || result.root.toString(_this._stringifyOptions(options)); });
  138. };
  139. /**
  140. * Process a selector into a new selector string synchronously.
  141. *
  142. * @param rule {postcss.Rule | string} The css selector to be processed
  143. * @param options The options for processing
  144. * @returns {string} the selector after processing.
  145. */
  146. Processor.prototype.processSync = function (rule, options) {
  147. var result = this._runSync(rule, options);
  148. return result.string || result.root.toString(this._stringifyOptions(options));
  149. };
  150. return Processor;
  151. }());
  152. exports.default = Processor;
  153. //# sourceMappingURL=processor.js.map