Security update for Core, with self-updated composer
[yaffs-website] / node_modules / uncss / node_modules / postcss / lib / lazy-result.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
8
9 var _mapGenerator = require('./map-generator');
10
11 var _mapGenerator2 = _interopRequireDefault(_mapGenerator);
12
13 var _stringify2 = require('./stringify');
14
15 var _stringify3 = _interopRequireDefault(_stringify2);
16
17 var _warnOnce = require('./warn-once');
18
19 var _warnOnce2 = _interopRequireDefault(_warnOnce);
20
21 var _result = require('./result');
22
23 var _result2 = _interopRequireDefault(_result);
24
25 var _parse = require('./parse');
26
27 var _parse2 = _interopRequireDefault(_parse);
28
29 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33 function isPromise(obj) {
34     return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && typeof obj.then === 'function';
35 }
36
37 var LazyResult = function () {
38     function LazyResult(processor, css, opts) {
39         _classCallCheck(this, LazyResult);
40
41         this.stringified = false;
42         this.processed = false;
43
44         var root = void 0;
45         if ((typeof css === 'undefined' ? 'undefined' : _typeof(css)) === 'object' && css.type === 'root') {
46             root = css;
47         } else if (css instanceof LazyResult || css instanceof _result2.default) {
48             root = css.root;
49             if (css.map) {
50                 if (typeof opts.map === 'undefined') opts.map = {};
51                 if (!opts.map.inline) opts.map.inline = false;
52                 opts.map.prev = css.map;
53             }
54         } else {
55             var parser = _parse2.default;
56             if (opts.syntax) parser = opts.syntax.parse;
57             if (opts.parser) parser = opts.parser;
58             if (parser.parse) parser = parser.parse;
59
60             try {
61                 root = parser(css, opts);
62             } catch (error) {
63                 this.error = error;
64             }
65         }
66
67         this.result = new _result2.default(processor, root, opts);
68     }
69
70     LazyResult.prototype.warnings = function warnings() {
71         return this.sync().warnings();
72     };
73
74     LazyResult.prototype.toString = function toString() {
75         return this.css;
76     };
77
78     LazyResult.prototype.then = function then(onFulfilled, onRejected) {
79         return this.async().then(onFulfilled, onRejected);
80     };
81
82     LazyResult.prototype.catch = function _catch(onRejected) {
83         return this.async().catch(onRejected);
84     };
85
86     LazyResult.prototype.handleError = function handleError(error, plugin) {
87         try {
88             this.error = error;
89             if (error.name === 'CssSyntaxError' && !error.plugin) {
90                 error.plugin = plugin.postcssPlugin;
91                 error.setMessage();
92             } else if (plugin.postcssVersion) {
93                 var pluginName = plugin.postcssPlugin;
94                 var pluginVer = plugin.postcssVersion;
95                 var runtimeVer = this.result.processor.version;
96                 var a = pluginVer.split('.');
97                 var b = runtimeVer.split('.');
98
99                 if (a[0] !== b[0] || parseInt(a[1]) > parseInt(b[1])) {
100                     (0, _warnOnce2.default)('Your current PostCSS version ' + 'is ' + runtimeVer + ', but ' + pluginName + ' ' + 'uses ' + pluginVer + '. Perhaps this is ' + 'the source of the error below.');
101                 }
102             }
103         } catch (err) {
104             /* istanbul ignore next */
105             if (console && console.error) console.error(err);
106         }
107     };
108
109     LazyResult.prototype.asyncTick = function asyncTick(resolve, reject) {
110         var _this = this;
111
112         if (this.plugin >= this.processor.plugins.length) {
113             this.processed = true;
114             return resolve();
115         }
116
117         try {
118             (function () {
119                 var plugin = _this.processor.plugins[_this.plugin];
120                 var promise = _this.run(plugin);
121                 _this.plugin += 1;
122
123                 if (isPromise(promise)) {
124                     promise.then(function () {
125                         _this.asyncTick(resolve, reject);
126                     }).catch(function (error) {
127                         _this.handleError(error, plugin);
128                         _this.processed = true;
129                         reject(error);
130                     });
131                 } else {
132                     _this.asyncTick(resolve, reject);
133                 }
134             })();
135         } catch (error) {
136             this.processed = true;
137             reject(error);
138         }
139     };
140
141     LazyResult.prototype.async = function async() {
142         var _this2 = this;
143
144         if (this.processed) {
145             return new Promise(function (resolve, reject) {
146                 if (_this2.error) {
147                     reject(_this2.error);
148                 } else {
149                     resolve(_this2.stringify());
150                 }
151             });
152         }
153         if (this.processing) {
154             return this.processing;
155         }
156
157         this.processing = new Promise(function (resolve, reject) {
158             if (_this2.error) return reject(_this2.error);
159             _this2.plugin = 0;
160             _this2.asyncTick(resolve, reject);
161         }).then(function () {
162             _this2.processed = true;
163             return _this2.stringify();
164         });
165
166         return this.processing;
167     };
168
169     LazyResult.prototype.sync = function sync() {
170         if (this.processed) return this.result;
171         this.processed = true;
172
173         if (this.processing) {
174             throw new Error('Use process(css).then(cb) to work with async plugins');
175         }
176
177         if (this.error) throw this.error;
178
179         for (var _iterator = this.result.processor.plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
180             var _ref;
181
182             if (_isArray) {
183                 if (_i >= _iterator.length) break;
184                 _ref = _iterator[_i++];
185             } else {
186                 _i = _iterator.next();
187                 if (_i.done) break;
188                 _ref = _i.value;
189             }
190
191             var plugin = _ref;
192
193             var promise = this.run(plugin);
194             if (isPromise(promise)) {
195                 throw new Error('Use process(css).then(cb) to work with async plugins');
196             }
197         }
198
199         return this.result;
200     };
201
202     LazyResult.prototype.run = function run(plugin) {
203         this.result.lastPlugin = plugin;
204
205         try {
206             return plugin(this.result.root, this.result);
207         } catch (error) {
208             this.handleError(error, plugin);
209             throw error;
210         }
211     };
212
213     LazyResult.prototype.stringify = function stringify() {
214         if (this.stringified) return this.result;
215         this.stringified = true;
216
217         this.sync();
218
219         var opts = this.result.opts;
220         var str = _stringify3.default;
221         if (opts.syntax) str = opts.syntax.stringify;
222         if (opts.stringifier) str = opts.stringifier;
223         if (str.stringify) str = str.stringify;
224
225         var map = new _mapGenerator2.default(str, this.result.root, this.result.opts);
226         var data = map.generate();
227         this.result.css = data[0];
228         this.result.map = data[1];
229
230         return this.result;
231     };
232
233     _createClass(LazyResult, [{
234         key: 'processor',
235         get: function get() {
236             return this.result.processor;
237         }
238     }, {
239         key: 'opts',
240         get: function get() {
241             return this.result.opts;
242         }
243     }, {
244         key: 'css',
245         get: function get() {
246             return this.stringify().css;
247         }
248     }, {
249         key: 'content',
250         get: function get() {
251             return this.stringify().content;
252         }
253     }, {
254         key: 'map',
255         get: function get() {
256             return this.stringify().map;
257         }
258     }, {
259         key: 'root',
260         get: function get() {
261             return this.sync().root;
262         }
263     }, {
264         key: 'messages',
265         get: function get() {
266             return this.sync().messages;
267         }
268     }]);
269
270     return LazyResult;
271 }();
272
273 exports.default = LazyResult;
274 module.exports = exports['default'];