3 exports.__esModule = true;
5 var _parser = require("./parser");
7 var _parser2 = _interopRequireDefault(_parser);
9 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13 var Processor = function () {
14 function Processor(func, options) {
15 _classCallCheck(this, Processor);
17 this.func = func || function noop() {};
19 this.options = options;
22 Processor.prototype._shouldUpdateSelector = function _shouldUpdateSelector(rule) {
23 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25 var merged = Object.assign({}, this.options, options);
26 if (merged.updateSelector === false) {
29 return typeof rule !== "string";
33 Processor.prototype._isLossy = function _isLossy() {
34 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
36 var merged = Object.assign({}, this.options, options);
37 if (merged.lossless === false) {
44 Processor.prototype._root = function _root(rule) {
45 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
47 var parser = new _parser2.default(rule, this._parseOptions(options));
51 Processor.prototype._parseOptions = function _parseOptions(options) {
53 lossy: this._isLossy(options)
57 Processor.prototype._run = function _run(rule) {
60 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
62 return new Promise(function (resolve, reject) {
64 var root = _this._root(rule, options);
65 Promise.resolve(_this.func(root)).then(function (transform) {
66 var string = undefined;
67 if (_this._shouldUpdateSelector(rule, options)) {
68 string = root.toString();
69 rule.selector = string;
71 return { transform: transform, root: root, string: string };
72 }).then(resolve, reject);
80 Processor.prototype._runSync = function _runSync(rule) {
81 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83 var root = this._root(rule, options);
84 var transform = this.func(root);
85 if (transform && typeof transform.then === "function") {
86 throw new Error("Selector processor returned a promise to a synchronous call.");
88 var string = undefined;
89 if (options.updateSelector && typeof rule !== "string") {
90 string = root.toString();
91 rule.selector = string;
93 return { transform: transform, root: root, string: string };
97 * Process rule into a selector AST.
99 * @param rule {postcss.Rule | string} The css selector to be processed
100 * @param options The options for processing
101 * @returns {Promise<parser.Root>} The AST of the selector after processing it.
105 Processor.prototype.ast = function ast(rule, options) {
106 return this._run(rule, options).then(function (result) {
112 * Process rule into a selector AST synchronously.
114 * @param rule {postcss.Rule | string} The css selector to be processed
115 * @param options The options for processing
116 * @returns {parser.Root} The AST of the selector after processing it.
120 Processor.prototype.astSync = function astSync(rule, options) {
121 return this._runSync(rule, options).root;
125 * Process a selector into a transformed value asynchronously
127 * @param rule {postcss.Rule | string} The css selector to be processed
128 * @param options The options for processing
129 * @returns {Promise<any>} The value returned by the processor.
133 Processor.prototype.transform = function transform(rule, options) {
134 return this._run(rule, options).then(function (result) {
135 return result.transform;
140 * Process a selector into a transformed value synchronously.
142 * @param rule {postcss.Rule | string} The css selector to be processed
143 * @param options The options for processing
144 * @returns {any} The value returned by the processor.
148 Processor.prototype.transformSync = function transformSync(rule, options) {
149 return this._runSync(rule, options).transform;
153 * Process a selector into a new selector string asynchronously.
155 * @param rule {postcss.Rule | string} The css selector to be processed
156 * @param options The options for processing
157 * @returns {string} the selector after processing.
161 Processor.prototype.process = function process(rule, options) {
162 return this._run(rule, options).then(function (result) {
163 return result.string || result.root.toString();
168 * Process a selector into a new selector string synchronously.
170 * @param rule {postcss.Rule | string} The css selector to be processed
171 * @param options The options for processing
172 * @returns {string} the selector after processing.
176 Processor.prototype.processSync = function processSync(rule, options) {
177 var result = this._runSync(rule, options);
178 return result.string || result.root.toString();
184 exports.default = Processor;
185 module.exports = exports["default"];