You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.1 KiB

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