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