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.

184 lines
5.9 KiB

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