web 3d图形渲染器
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.

207 lines
5.1 KiB

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