.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / supports.js
1 'use strict';
2
3 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7 var Browsers = require('./browsers');
8 var brackets = require('./brackets');
9 var Value = require('./value');
10 var utils = require('./utils');
11
12 var postcss = require('postcss');
13
14 var supported = [];
15 var data = require('caniuse-lite').feature(require('caniuse-lite/data/features/css-featurequeries.js'));
16 for (var browser in data.stats) {
17     var versions = data.stats[browser];
18     for (var version in versions) {
19         var support = versions[version];
20         if (/y/.test(support)) {
21             supported.push(browser + ' ' + version);
22         }
23     }
24 }
25
26 var Supports = function () {
27     function Supports(Prefixes, all) {
28         _classCallCheck(this, Supports);
29
30         this.Prefixes = Prefixes;
31         this.all = all;
32     }
33
34     /**
35      * Return prefixer only with @supports supported browsers
36      */
37
38
39     Supports.prototype.prefixer = function prefixer() {
40         if (this.prefixerCache) {
41             return this.prefixerCache;
42         }
43
44         var filtered = this.all.browsers.selected.filter(function (i) {
45             return supported.indexOf(i) !== -1;
46         });
47
48         var browsers = new Browsers(this.all.browsers.data, filtered, this.all.options);
49         this.prefixerCache = new this.Prefixes(this.all.data, browsers, this.all.options);
50         return this.prefixerCache;
51     };
52
53     /**
54      * Parse string into declaration property and value
55      */
56
57
58     Supports.prototype.parse = function parse(str) {
59         var _str$split = str.split(':'),
60             prop = _str$split[0],
61             value = _str$split[1];
62
63         if (!value) value = '';
64         return [prop.trim(), value.trim()];
65     };
66
67     /**
68      * Create virtual rule to process it by prefixer
69      */
70
71
72     Supports.prototype.virtual = function virtual(str) {
73         var _parse = this.parse(str),
74             prop = _parse[0],
75             value = _parse[1];
76
77         var rule = postcss.parse('a{}').first;
78         rule.append({ prop: prop, value: value, raws: { before: '' } });
79         return rule;
80     };
81
82     /**
83      * Return array of Declaration with all necessary prefixes
84      */
85
86
87     Supports.prototype.prefixed = function prefixed(str) {
88         var rule = this.virtual(str);
89         if (this.disabled(rule.first)) {
90             return rule.nodes;
91         }
92
93         var prefixer = this.prefixer().add[rule.first.prop];
94         prefixer && prefixer.process && prefixer.process(rule.first);
95
96         for (var _iterator = rule.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
97             var _ref;
98
99             if (_isArray) {
100                 if (_i >= _iterator.length) break;
101                 _ref = _iterator[_i++];
102             } else {
103                 _i = _iterator.next();
104                 if (_i.done) break;
105                 _ref = _i.value;
106             }
107
108             var decl = _ref;
109
110             for (var _iterator2 = this.prefixer().values('add', rule.first.prop), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
111                 var _ref2;
112
113                 if (_isArray2) {
114                     if (_i2 >= _iterator2.length) break;
115                     _ref2 = _iterator2[_i2++];
116                 } else {
117                     _i2 = _iterator2.next();
118                     if (_i2.done) break;
119                     _ref2 = _i2.value;
120                 }
121
122                 var value = _ref2;
123
124                 value.process(decl);
125             }
126             Value.save(this.all, decl);
127         }
128
129         return rule.nodes;
130     };
131
132     /**
133      * Return true if brackets node is "not" word
134      */
135
136
137     Supports.prototype.isNot = function isNot(node) {
138         return typeof node === 'string' && /not\s*/i.test(node);
139     };
140
141     /**
142      * Return true if brackets node is "or" word
143      */
144
145
146     Supports.prototype.isOr = function isOr(node) {
147         return typeof node === 'string' && /\s*or\s*/i.test(node);
148     };
149
150     /**
151      * Return true if brackets node is (prop: value)
152      */
153
154
155     Supports.prototype.isProp = function isProp(node) {
156         return (typeof node === 'undefined' ? 'undefined' : _typeof(node)) === 'object' && node.length === 1 && typeof node[0] === 'string';
157     };
158
159     /**
160      * Return true if prefixed property has no unprefixed
161      */
162
163
164     Supports.prototype.isHack = function isHack(all, unprefixed) {
165         var check = new RegExp('(\\(|\\s)' + utils.escapeRegexp(unprefixed) + ':');
166         return !check.test(all);
167     };
168
169     /**
170      * Return true if we need to remove node
171      */
172
173
174     Supports.prototype.toRemove = function toRemove(str, all) {
175         var _parse2 = this.parse(str),
176             prop = _parse2[0],
177             value = _parse2[1];
178
179         var unprefixed = this.all.unprefixed(prop);
180
181         var cleaner = this.all.cleaner();
182
183         if (cleaner.remove[prop] && cleaner.remove[prop].remove && !this.isHack(all, unprefixed)) {
184             return true;
185         }
186
187         for (var _iterator3 = cleaner.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
188             var _ref3;
189
190             if (_isArray3) {
191                 if (_i3 >= _iterator3.length) break;
192                 _ref3 = _iterator3[_i3++];
193             } else {
194                 _i3 = _iterator3.next();
195                 if (_i3.done) break;
196                 _ref3 = _i3.value;
197             }
198
199             var checker = _ref3;
200
201             if (checker.check(value)) {
202                 return true;
203             }
204         }
205
206         return false;
207     };
208
209     /**
210      * Remove all unnecessary prefixes
211      */
212
213
214     Supports.prototype.remove = function remove(nodes, all) {
215         var i = 0;
216         while (i < nodes.length) {
217             if (!this.isNot(nodes[i - 1]) && this.isProp(nodes[i]) && this.isOr(nodes[i + 1])) {
218                 if (this.toRemove(nodes[i][0], all)) {
219                     nodes.splice(i, 2);
220                     continue;
221                 }
222
223                 i += 2;
224                 continue;
225             }
226
227             if (_typeof(nodes[i]) === 'object') {
228                 nodes[i] = this.remove(nodes[i], all);
229             }
230
231             i += 1;
232         }
233         return nodes;
234     };
235
236     /**
237      * Clean brackets with one child
238      */
239
240
241     Supports.prototype.cleanBrackets = function cleanBrackets(nodes) {
242         var _this = this;
243
244         return nodes.map(function (i) {
245             if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) !== 'object') {
246                 return i;
247             }
248
249             if (i.length === 1 && _typeof(i[0]) === 'object') {
250                 return _this.cleanBrackets(i[0]);
251             }
252
253             return _this.cleanBrackets(i);
254         });
255     };
256
257     /**
258      * Add " or " between properties and convert it to brackets format
259      */
260
261
262     Supports.prototype.convert = function convert(progress) {
263         var result = [''];
264         for (var _iterator4 = progress, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
265             var _ref4;
266
267             if (_isArray4) {
268                 if (_i4 >= _iterator4.length) break;
269                 _ref4 = _iterator4[_i4++];
270             } else {
271                 _i4 = _iterator4.next();
272                 if (_i4.done) break;
273                 _ref4 = _i4.value;
274             }
275
276             var i = _ref4;
277
278             result.push([i.prop + ': ' + i.value]);
279             result.push(' or ');
280         }
281         result[result.length - 1] = '';
282         return result;
283     };
284
285     /**
286      * Compress value functions into a string nodes
287      */
288
289
290     Supports.prototype.normalize = function normalize(nodes) {
291         var _this2 = this;
292
293         if ((typeof nodes === 'undefined' ? 'undefined' : _typeof(nodes)) !== 'object') {
294             return nodes;
295         }
296
297         nodes = nodes.filter(function (i) {
298             return i !== '';
299         });
300         if (typeof nodes[0] === 'string' && nodes[0].indexOf(':') !== -1) {
301             return [brackets.stringify(nodes)];
302         }
303
304         return nodes.map(function (i) {
305             return _this2.normalize(i);
306         });
307     };
308
309     /**
310      * Add prefixes
311      */
312
313
314     Supports.prototype.add = function add(nodes, all) {
315         var _this3 = this;
316
317         return nodes.map(function (i) {
318             if (_this3.isProp(i)) {
319                 var prefixed = _this3.prefixed(i[0]);
320                 if (prefixed.length > 1) {
321                     return _this3.convert(prefixed);
322                 }
323
324                 return i;
325             }
326
327             if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) === 'object') {
328                 return _this3.add(i, all);
329             }
330
331             return i;
332         });
333     };
334
335     /**
336      * Add prefixed declaration
337      */
338
339
340     Supports.prototype.process = function process(rule) {
341         var ast = brackets.parse(rule.params);
342         ast = this.normalize(ast);
343         ast = this.remove(ast, rule.params);
344         ast = this.add(ast, rule.params);
345         ast = this.cleanBrackets(ast);
346         rule.params = brackets.stringify(ast);
347     };
348
349     /**
350      * Check global options
351      */
352
353
354     Supports.prototype.disabled = function disabled(node) {
355         if (!this.all.options.grid) {
356             if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
357                 return true;
358             }
359             if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
360                 return true;
361             }
362         }
363
364         if (this.all.options.flexbox === false) {
365             if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
366                 return true;
367             }
368             var other = ['order', 'justify-content', 'align-items', 'align-content'];
369             if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
370                 return true;
371             }
372         }
373
374         return false;
375     };
376
377     return Supports;
378 }();
379
380 module.exports = Supports;