.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / processor.js
1 'use strict';
2
3 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5 var Value = require('./value');
6
7 var OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i;
8 var OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i;
9
10 var SIZES = ['width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'inline-size', 'min-inline-size', 'max-inline-size', 'block-size', 'min-block-size', 'max-block-size'];
11
12 var Processor = function () {
13     function Processor(prefixes) {
14         _classCallCheck(this, Processor);
15
16         this.prefixes = prefixes;
17     }
18
19     /**
20      * Add necessary prefixes
21      */
22
23
24     Processor.prototype.add = function add(css, result) {
25         var _this = this;
26
27         // At-rules
28         var resolution = this.prefixes.add['@resolution'];
29         var keyframes = this.prefixes.add['@keyframes'];
30         var viewport = this.prefixes.add['@viewport'];
31         var supports = this.prefixes.add['@supports'];
32
33         css.walkAtRules(function (rule) {
34             if (rule.name === 'keyframes') {
35                 if (!_this.disabled(rule, result)) {
36                     return keyframes && keyframes.process(rule);
37                 }
38             } else if (rule.name === 'viewport') {
39                 if (!_this.disabled(rule, result)) {
40                     return viewport && viewport.process(rule);
41                 }
42             } else if (rule.name === 'supports') {
43                 if (_this.prefixes.options.supports !== false && !_this.disabled(rule, result)) {
44                     return supports.process(rule);
45                 }
46             } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
47                 if (!_this.disabled(rule, result)) {
48                     return resolution && resolution.process(rule);
49                 }
50             }
51
52             return undefined;
53         });
54
55         // Selectors
56         css.walkRules(function (rule) {
57             if (_this.disabled(rule, result)) return undefined;
58
59             return _this.prefixes.add.selectors.map(function (selector) {
60                 return selector.process(rule, result);
61             });
62         });
63
64         css.walkDecls(function (decl) {
65             if (_this.disabledDecl(decl, result)) return undefined;
66
67             if (decl.prop === 'display' && decl.value === 'box') {
68                 result.warn('You should write display: flex by final spec ' + 'instead of display: box', { node: decl });
69                 return undefined;
70             }
71             if (decl.value.indexOf('linear-gradient') !== -1) {
72                 if (OLD_LINEAR.test(decl.value)) {
73                     result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `to left` instead of `right`.', { node: decl });
74                 }
75             }
76             if (decl.value.indexOf('radial-gradient') !== -1) {
77                 if (OLD_RADIAL.test(decl.value)) {
78                     result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `closest-side at 0 0` ' + 'instead of `0 0, closest-side`.', { node: decl });
79                 } else if (/[^-]cover/.test(decl.value)) {
80                     result.warn('Gradient has outdated direction syntax. ' + 'Replace `cover` to `farthest-corner`.', { node: decl });
81                 } else if (/[^-]contain/.test(decl.value)) {
82                     result.warn('Gradient has outdated direction syntax. ' + 'Replace `contain` to `closest-side`.', { node: decl });
83                 }
84             }
85             if (decl.prop === 'text-emphasis-position') {
86                 if (decl.value === 'under' || decl.value === 'over') {
87                     result.warn('You should use 2 values for text-emphasis-position ' + 'For example, `under left` instead of just `under`.', { node: decl });
88                 }
89             }
90
91             if (SIZES.indexOf(decl.prop) !== -1) {
92                 if (decl.value.indexOf('fill-available') !== -1) {
93                     result.warn('Replace fill-available to stretch, ' + 'because spec had been changed', { node: decl });
94                 } else if (decl.value.indexOf('fill') !== -1) {
95                     result.warn('Replace fill to stretch, ' + 'because spec had been changed', { node: decl });
96                 }
97             }
98
99             var prefixer = void 0;
100
101             if (decl.prop === 'transition' || decl.prop === 'transition-property') {
102                 // Transition
103                 return _this.prefixes.transition.add(decl, result);
104             } else if (decl.prop === 'align-self') {
105                 // align-self flexbox or grid
106                 var display = _this.displayType(decl);
107                 if (display !== 'grid' && _this.prefixes.options.flexbox !== false) {
108                     prefixer = _this.prefixes.add['align-self'];
109                     if (prefixer && prefixer.prefixes) {
110                         prefixer.process(decl);
111                     }
112                 }
113                 if (display !== 'flex' && _this.prefixes.options.grid !== false) {
114                     prefixer = _this.prefixes.add['grid-row-align'];
115                     if (prefixer && prefixer.prefixes) {
116                         return prefixer.process(decl, result);
117                     }
118                 }
119             } else if (decl.prop === 'justify-self') {
120                 // justify-self flexbox or grid
121                 var _display = _this.displayType(decl);
122                 if (_display !== 'flex' && _this.prefixes.options.grid !== false) {
123                     prefixer = _this.prefixes.add['grid-column-align'];
124                     if (prefixer && prefixer.prefixes) {
125                         return prefixer.process(decl, result);
126                     }
127                 }
128             } else {
129                 // Properties
130                 prefixer = _this.prefixes.add[decl.prop];
131                 if (prefixer && prefixer.prefixes) {
132                     return prefixer.process(decl, result);
133                 }
134             }
135
136             return undefined;
137         });
138
139         // Values
140         return css.walkDecls(function (decl) {
141             if (_this.disabledValue(decl, result)) return;
142
143             var unprefixed = _this.prefixes.unprefixed(decl.prop);
144             for (var _iterator = _this.prefixes.values('add', unprefixed), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
145                 var _ref;
146
147                 if (_isArray) {
148                     if (_i >= _iterator.length) break;
149                     _ref = _iterator[_i++];
150                 } else {
151                     _i = _iterator.next();
152                     if (_i.done) break;
153                     _ref = _i.value;
154                 }
155
156                 var value = _ref;
157
158                 value.process(decl, result);
159             }
160             Value.save(_this.prefixes, decl);
161         });
162     };
163
164     /**
165      * Remove unnecessary pefixes
166      */
167
168
169     Processor.prototype.remove = function remove(css, result) {
170         var _this2 = this;
171
172         // At-rules
173         var resolution = this.prefixes.remove['@resolution'];
174
175         css.walkAtRules(function (rule, i) {
176             if (_this2.prefixes.remove['@' + rule.name]) {
177                 if (!_this2.disabled(rule, result)) {
178                     rule.parent.removeChild(i);
179                 }
180             } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1 && resolution) {
181                 resolution.clean(rule);
182             }
183         });
184
185         // Selectors
186
187         var _loop = function _loop(checker) {
188             css.walkRules(function (rule, i) {
189                 if (checker.check(rule)) {
190                     if (!_this2.disabled(rule, result)) {
191                         rule.parent.removeChild(i);
192                     }
193                 }
194             });
195         };
196
197         for (var _iterator2 = this.prefixes.remove.selectors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
198             var _ref2;
199
200             if (_isArray2) {
201                 if (_i2 >= _iterator2.length) break;
202                 _ref2 = _iterator2[_i2++];
203             } else {
204                 _i2 = _iterator2.next();
205                 if (_i2.done) break;
206                 _ref2 = _i2.value;
207             }
208
209             var checker = _ref2;
210
211             _loop(checker);
212         }
213
214         return css.walkDecls(function (decl, i) {
215             if (_this2.disabled(decl, result)) return;
216
217             var rule = decl.parent;
218             var unprefixed = _this2.prefixes.unprefixed(decl.prop);
219
220             // Transition
221             if (decl.prop === 'transition' || decl.prop === 'transition-property') {
222                 _this2.prefixes.transition.remove(decl);
223             }
224
225             // Properties
226             if (_this2.prefixes.remove[decl.prop] && _this2.prefixes.remove[decl.prop].remove) {
227                 var notHack = _this2.prefixes.group(decl).down(function (other) {
228                     return _this2.prefixes.normalize(other.prop) === unprefixed;
229                 });
230
231                 if (unprefixed === 'flex-flow') {
232                     notHack = true;
233                 }
234
235                 if (notHack && !_this2.withHackValue(decl)) {
236                     if (decl.raw('before').indexOf('\n') > -1) {
237                         _this2.reduceSpaces(decl);
238                     }
239                     rule.removeChild(i);
240                     return;
241                 }
242             }
243
244             // Values
245             for (var _iterator3 = _this2.prefixes.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
246                 var _ref3;
247
248                 if (_isArray3) {
249                     if (_i3 >= _iterator3.length) break;
250                     _ref3 = _iterator3[_i3++];
251                 } else {
252                     _i3 = _iterator3.next();
253                     if (_i3.done) break;
254                     _ref3 = _i3.value;
255                 }
256
257                 var checker = _ref3;
258
259                 if (!checker.check(decl.value)) {
260                     continue;
261                 }
262
263                 unprefixed = checker.unprefixed;
264                 var _notHack = _this2.prefixes.group(decl).down(function (other) {
265                     return other.value.indexOf(unprefixed) !== -1;
266                 });
267
268                 if (_notHack) {
269                     rule.removeChild(i);
270                     return;
271                 }
272             }
273         });
274     };
275
276     /**
277      * Some rare old values, which is not in standard
278      */
279
280
281     Processor.prototype.withHackValue = function withHackValue(decl) {
282         return decl.prop === '-webkit-background-clip' && decl.value === 'text';
283     };
284
285     /**
286      * Check for grid/flexbox options.
287      */
288
289
290     Processor.prototype.disabledValue = function disabledValue(node, result) {
291         if (this.prefixes.options.grid === false && node.type === 'decl') {
292             if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
293                 return true;
294             }
295         }
296         if (this.prefixes.options.flexbox === false && node.type === 'decl') {
297             if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
298                 return true;
299             }
300         }
301
302         return this.disabled(node, result);
303     };
304
305     /**
306      * Check for grid/flexbox options.
307      */
308
309
310     Processor.prototype.disabledDecl = function disabledDecl(node, result) {
311         if (this.prefixes.options.grid === false && node.type === 'decl') {
312             if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
313                 return true;
314             }
315         }
316         if (this.prefixes.options.flexbox === false && node.type === 'decl') {
317             var other = ['order', 'justify-content', 'align-items', 'align-content'];
318             if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
319                 return true;
320             }
321         }
322
323         return this.disabled(node, result);
324     };
325
326     /**
327      * Check for control comment and global options
328      */
329
330
331     Processor.prototype.disabled = function disabled(node, result) {
332         if (!node) return false;
333         if (node._autoprefixerDisabled !== undefined) {
334             return node._autoprefixerDisabled;
335         }
336
337         if (node.nodes) {
338             var status = undefined;
339             node.each(function (i) {
340                 if (i.type !== 'comment') return;
341                 if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
342                     if (typeof status !== 'undefined') {
343                         result.warn('Second Autoprefixer control comment ' + 'was ignored. Autoprefixer applies control ' + 'comment to whole block, not to next rules.', { node: i });
344                     } else {
345                         status = /on/i.test(i.text);
346                     }
347                 }
348             });
349
350             var value = false;
351             if (status !== undefined) {
352                 value = !status;
353             } else if (node.parent) {
354                 value = this.disabled(node.parent, result);
355             }
356
357             node._autoprefixerDisabled = value;
358             return node._autoprefixerDisabled;
359         } else {
360             node._autoprefixerDisabled = this.disabled(node.parent, result);
361             return node._autoprefixerDisabled;
362         }
363     };
364
365     /**
366      * Normalize spaces in cascade declaration group
367      */
368
369
370     Processor.prototype.reduceSpaces = function reduceSpaces(decl) {
371         var stop = false;
372         this.prefixes.group(decl).up(function () {
373             stop = true;
374             return true;
375         });
376         if (stop) {
377             return;
378         }
379
380         var parts = decl.raw('before').split('\n');
381         var prevMin = parts[parts.length - 1].length;
382         var diff = false;
383
384         this.prefixes.group(decl).down(function (other) {
385             parts = other.raw('before').split('\n');
386             var last = parts.length - 1;
387
388             if (parts[last].length > prevMin) {
389                 if (diff === false) {
390                     diff = parts[last].length - prevMin;
391                 }
392
393                 parts[last] = parts[last].slice(0, -diff);
394                 other.raws.before = parts.join('\n');
395             }
396         });
397     };
398
399     /**
400      * Is it flebox or grid rule
401      */
402
403
404     Processor.prototype.displayType = function displayType(decl) {
405         for (var _iterator4 = decl.parent.nodes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
406             var _ref4;
407
408             if (_isArray4) {
409                 if (_i4 >= _iterator4.length) break;
410                 _ref4 = _iterator4[_i4++];
411             } else {
412                 _i4 = _iterator4.next();
413                 if (_i4.done) break;
414                 _ref4 = _i4.value;
415             }
416
417             var i = _ref4;
418
419             if (i.prop !== 'display') {
420                 continue;
421             }
422
423             if (i.value.indexOf('flex') !== -1) {
424                 return 'flex';
425             }
426
427             if (i.value.indexOf('grid') !== -1) {
428                 return 'grid';
429             }
430         }
431
432         return false;
433     };
434
435     return Processor;
436 }();
437
438 module.exports = Processor;