1"use strict"; 2 3exports.__esModule = true; 4exports["default"] = void 0; 5var _parser = _interopRequireDefault(require("./parser")); 6function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 7var Processor = /*#__PURE__*/function () { 8 function Processor(func, options) { 9 this.func = func || function noop() {}; 10 this.funcRes = null; 11 this.options = options; 12 } 13 var _proto = Processor.prototype; 14 _proto._shouldUpdateSelector = function _shouldUpdateSelector(rule, options) { 15 if (options === void 0) { 16 options = {}; 17 } 18 var merged = Object.assign({}, this.options, options); 19 if (merged.updateSelector === false) { 20 return false; 21 } else { 22 return typeof rule !== "string"; 23 } 24 }; 25 _proto._isLossy = function _isLossy(options) { 26 if (options === void 0) { 27 options = {}; 28 } 29 var merged = Object.assign({}, this.options, options); 30 if (merged.lossless === false) { 31 return true; 32 } else { 33 return false; 34 } 35 }; 36 _proto._root = function _root(rule, options) { 37 if (options === void 0) { 38 options = {}; 39 } 40 var parser = new _parser["default"](rule, this._parseOptions(options)); 41 return parser.root; 42 }; 43 _proto._parseOptions = function _parseOptions(options) { 44 return { 45 lossy: this._isLossy(options) 46 }; 47 }; 48 _proto._run = function _run(rule, options) { 49 var _this = this; 50 if (options === void 0) { 51 options = {}; 52 } 53 return new Promise(function (resolve, reject) { 54 try { 55 var root = _this._root(rule, options); 56 Promise.resolve(_this.func(root)).then(function (transform) { 57 var string = undefined; 58 if (_this._shouldUpdateSelector(rule, options)) { 59 string = root.toString(); 60 rule.selector = string; 61 } 62 return { 63 transform: transform, 64 root: root, 65 string: string 66 }; 67 }).then(resolve, reject); 68 } catch (e) { 69 reject(e); 70 return; 71 } 72 }); 73 }; 74 _proto._runSync = function _runSync(rule, options) { 75 if (options === void 0) { 76 options = {}; 77 } 78 var root = this._root(rule, options); 79 var transform = this.func(root); 80 if (transform && typeof transform.then === "function") { 81 throw new Error("Selector processor returned a promise to a synchronous call."); 82 } 83 var string = undefined; 84 if (options.updateSelector && typeof rule !== "string") { 85 string = root.toString(); 86 rule.selector = string; 87 } 88 return { 89 transform: transform, 90 root: root, 91 string: string 92 }; 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 _proto.ast = function ast(rule, options) { 103 return this._run(rule, options).then(function (result) { 104 return result.root; 105 }); 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 _proto.astSync = function astSync(rule, options) { 116 return this._runSync(rule, options).root; 117 } 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 _proto.transform = function transform(rule, options) { 127 return this._run(rule, options).then(function (result) { 128 return result.transform; 129 }); 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 _proto.transformSync = function transformSync(rule, options) { 140 return this._runSync(rule, options).transform; 141 } 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 _proto.process = function process(rule, options) { 151 return this._run(rule, options).then(function (result) { 152 return result.string || result.root.toString(); 153 }); 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 _proto.processSync = function processSync(rule, options) { 164 var result = this._runSync(rule, options); 165 return result.string || result.root.toString(); 166 }; 167 return Processor; 168}(); 169exports["default"] = Processor; 170module.exports = exports.default;