.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / hacks / gradient.js
1 'use strict';
2
3 function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
4
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8
9 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
10
11 var OldValue = require('../old-value');
12 var Value = require('../value');
13 var utils = require('../utils');
14
15 var parser = require('postcss-value-parser');
16 var range = require('normalize-range');
17
18 var isDirection = /top|left|right|bottom/gi;
19
20 var Gradient = function (_Value) {
21     _inherits(Gradient, _Value);
22
23     function Gradient() {
24         var _temp, _this, _ret;
25
26         _classCallCheck(this, Gradient);
27
28         for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
29             args[_key] = arguments[_key];
30         }
31
32         return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Value.call.apply(_Value, [this].concat(args))), _this), Object.defineProperty(_this, 'directions', {
33             enumerable: true,
34             writable: true,
35             value: {
36                 top: 'bottom',
37                 left: 'right',
38                 bottom: 'top',
39                 right: 'left'
40             }
41         }), Object.defineProperty(_this, 'oldDirections', {
42             enumerable: true,
43             writable: true,
44             value: {
45                 'top': 'left bottom, left top',
46                 'left': 'right top, left top',
47                 'bottom': 'left top, left bottom',
48                 'right': 'left top, right top',
49
50                 'top right': 'left bottom, right top',
51                 'top left': 'right bottom, left top',
52                 'right top': 'left bottom, right top',
53                 'right bottom': 'left top, right bottom',
54                 'bottom right': 'left top, right bottom',
55                 'bottom left': 'right top, left bottom',
56                 'left top': 'right bottom, left top',
57                 'left bottom': 'right top, left bottom'
58             }
59         }), _temp), _possibleConstructorReturn(_this, _ret);
60     }
61
62     // Direction to replace
63
64
65     // Direction to replace
66
67
68     /**
69      * Change degrees for webkit prefix
70      */
71     Gradient.prototype.replace = function replace(string, prefix) {
72         var ast = parser(string);
73         for (var _iterator = ast.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
74             var _ref;
75
76             if (_isArray) {
77                 if (_i >= _iterator.length) break;
78                 _ref = _iterator[_i++];
79             } else {
80                 _i = _iterator.next();
81                 if (_i.done) break;
82                 _ref = _i.value;
83             }
84
85             var node = _ref;
86
87             if (node.type === 'function' && node.value === this.name) {
88                 node.nodes = this.newDirection(node.nodes);
89                 node.nodes = this.normalize(node.nodes);
90                 if (prefix === '-webkit- old') {
91                     var changes = this.oldWebkit(node);
92                     if (!changes) {
93                         return false;
94                     }
95                 } else {
96                     node.nodes = this.convertDirection(node.nodes);
97                     node.value = prefix + node.value;
98                 }
99             }
100         }
101         return ast.toString();
102     };
103
104     /**
105      * Replace first token
106      */
107
108
109     Gradient.prototype.replaceFirst = function replaceFirst(params) {
110         for (var _len2 = arguments.length, words = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
111             words[_key2 - 1] = arguments[_key2];
112         }
113
114         var prefix = words.map(function (i) {
115             if (i === ' ') {
116                 return { type: 'space', value: i };
117             } else {
118                 return { type: 'word', value: i };
119             }
120         });
121         return prefix.concat(params.slice(1));
122     };
123
124     /**
125      * Convert angle unit to deg
126      */
127
128
129     Gradient.prototype.normalizeUnit = function normalizeUnit(str, full) {
130         var num = parseFloat(str);
131         var deg = num / full * 360;
132         return deg + 'deg';
133     };
134
135     /**
136      * Normalize angle
137      */
138
139
140     Gradient.prototype.normalize = function normalize(nodes) {
141         if (!nodes[0]) return nodes;
142
143         if (/-?\d+(.\d+)?grad/.test(nodes[0].value)) {
144             nodes[0].value = this.normalizeUnit(nodes[0].value, 400);
145         } else if (/-?\d+(.\d+)?rad/.test(nodes[0].value)) {
146             nodes[0].value = this.normalizeUnit(nodes[0].value, 2 * Math.PI);
147         } else if (/-?\d+(.\d+)?turn/.test(nodes[0].value)) {
148             nodes[0].value = this.normalizeUnit(nodes[0].value, 1);
149         } else if (nodes[0].value.indexOf('deg') !== -1) {
150             var num = parseFloat(nodes[0].value);
151             num = range.wrap(0, 360, num);
152             nodes[0].value = num + 'deg';
153         }
154
155         if (nodes[0].value === '0deg') {
156             nodes = this.replaceFirst(nodes, 'to', ' ', 'top');
157         } else if (nodes[0].value === '90deg') {
158             nodes = this.replaceFirst(nodes, 'to', ' ', 'right');
159         } else if (nodes[0].value === '180deg') {
160             nodes = this.replaceFirst(nodes, 'to', ' ', 'bottom');
161         } else if (nodes[0].value === '270deg') {
162             nodes = this.replaceFirst(nodes, 'to', ' ', 'left');
163         }
164
165         return nodes;
166     };
167
168     /**
169      * Replace old direction to new
170      */
171
172
173     Gradient.prototype.newDirection = function newDirection(params) {
174         if (params[0].value === 'to') {
175             return params;
176         }
177         isDirection.lastIndex = 0; // reset search index of global regexp
178         if (!isDirection.test(params[0].value)) {
179             return params;
180         }
181
182         params.unshift({
183             type: 'word',
184             value: 'to'
185         }, {
186             type: 'space',
187             value: ' '
188         });
189
190         for (var i = 2; i < params.length; i++) {
191             if (params[i].type === 'div') {
192                 break;
193             }
194             if (params[i].type === 'word') {
195                 params[i].value = this.revertDirection(params[i].value);
196             }
197         }
198
199         return params;
200     };
201
202     /**
203      * Change new direction to old
204      */
205
206
207     Gradient.prototype.convertDirection = function convertDirection(params) {
208         if (params.length > 0) {
209             if (params[0].value === 'to') {
210                 this.fixDirection(params);
211             } else if (params[0].value.indexOf('deg') !== -1) {
212                 this.fixAngle(params);
213             } else if (params[2] && params[2].value === 'at') {
214                 this.fixRadial(params);
215             }
216         }
217         return params;
218     };
219
220     /**
221      * Replace `to top left` to `bottom right`
222      */
223
224
225     Gradient.prototype.fixDirection = function fixDirection(params) {
226         params.splice(0, 2);
227
228         for (var _iterator2 = params, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
229             var _ref2;
230
231             if (_isArray2) {
232                 if (_i2 >= _iterator2.length) break;
233                 _ref2 = _iterator2[_i2++];
234             } else {
235                 _i2 = _iterator2.next();
236                 if (_i2.done) break;
237                 _ref2 = _i2.value;
238             }
239
240             var param = _ref2;
241
242             if (param.type === 'div') {
243                 break;
244             }
245             if (param.type === 'word') {
246                 param.value = this.revertDirection(param.value);
247             }
248         }
249     };
250
251     /**
252      * Add 90 degrees
253      */
254
255
256     Gradient.prototype.fixAngle = function fixAngle(params) {
257         var first = params[0].value;
258         first = parseFloat(first);
259         first = Math.abs(450 - first) % 360;
260         first = this.roundFloat(first, 3);
261         params[0].value = first + 'deg';
262     };
263
264     /**
265      * Fix radial direction syntax
266      */
267
268
269     Gradient.prototype.fixRadial = function fixRadial(params) {
270         var first = params[0];
271         var second = [];
272         var i = void 0;
273
274         var div = void 0;
275         for (i = 4; i < params.length; i++) {
276             if (params[i].type === 'div') {
277                 div = params[i];
278                 break;
279             } else {
280                 second.push(params[i]);
281             }
282         }
283
284         params.splice.apply(params, [0, i].concat(second, [div, first]));
285     };
286
287     Gradient.prototype.revertDirection = function revertDirection(word) {
288         return this.directions[word.toLowerCase()] || word;
289     };
290
291     /**
292      * Round float and save digits under dot
293      */
294
295
296     Gradient.prototype.roundFloat = function roundFloat(float, digits) {
297         return parseFloat(float.toFixed(digits));
298     };
299
300     /**
301      * Convert to old webkit syntax
302      */
303
304
305     Gradient.prototype.oldWebkit = function oldWebkit(node) {
306         var nodes = node.nodes;
307
308         var string = parser.stringify(node.nodes);
309
310         if (this.name !== 'linear-gradient') {
311             return false;
312         }
313         if (nodes[0] && nodes[0].value.indexOf('deg') !== -1) {
314             return false;
315         }
316         if (string.indexOf('px') !== -1 || string.indexOf('-corner') !== -1 || string.indexOf('-side') !== -1) {
317             return false;
318         }
319
320         var params = [[]];
321         for (var _iterator3 = nodes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
322             var _ref3;
323
324             if (_isArray3) {
325                 if (_i3 >= _iterator3.length) break;
326                 _ref3 = _iterator3[_i3++];
327             } else {
328                 _i3 = _iterator3.next();
329                 if (_i3.done) break;
330                 _ref3 = _i3.value;
331             }
332
333             var i = _ref3;
334
335             params[params.length - 1].push(i);
336             if (i.type === 'div' && i.value === ',') {
337                 params.push([]);
338             }
339         }
340
341         this.oldDirection(params);
342         this.colorStops(params);
343
344         node.nodes = [];
345         for (var _iterator4 = params, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
346             var _ref4;
347
348             if (_isArray4) {
349                 if (_i4 >= _iterator4.length) break;
350                 _ref4 = _iterator4[_i4++];
351             } else {
352                 _i4 = _iterator4.next();
353                 if (_i4.done) break;
354                 _ref4 = _i4.value;
355             }
356
357             var param = _ref4;
358
359             node.nodes = node.nodes.concat(param);
360         }
361
362         node.nodes.unshift({ type: 'word', value: 'linear' }, this.cloneDiv(node.nodes));
363         node.value = '-webkit-gradient';
364
365         return true;
366     };
367
368     /**
369      * Change direction syntax to old webkit
370      */
371
372
373     Gradient.prototype.oldDirection = function oldDirection(params) {
374         var div = this.cloneDiv(params[0]);
375
376         if (params[0][0].value !== 'to') {
377             return params.unshift([{ type: 'word', value: this.oldDirections.bottom }, div]);
378         } else {
379             var _words = [];
380             for (var _iterator5 = params[0].slice(2), _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
381                 var _ref5;
382
383                 if (_isArray5) {
384                     if (_i5 >= _iterator5.length) break;
385                     _ref5 = _iterator5[_i5++];
386                 } else {
387                     _i5 = _iterator5.next();
388                     if (_i5.done) break;
389                     _ref5 = _i5.value;
390                 }
391
392                 var node = _ref5;
393
394                 if (node.type === 'word') {
395                     _words.push(node.value.toLowerCase());
396                 }
397             }
398
399             _words = _words.join(' ');
400             var old = this.oldDirections[_words] || _words;
401
402             params[0] = [{ type: 'word', value: old }, div];
403             return params[0];
404         }
405     };
406
407     /**
408      * Get div token from exists parameters
409      */
410
411
412     Gradient.prototype.cloneDiv = function cloneDiv(params) {
413         for (var _iterator6 = params, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
414             var _ref6;
415
416             if (_isArray6) {
417                 if (_i6 >= _iterator6.length) break;
418                 _ref6 = _iterator6[_i6++];
419             } else {
420                 _i6 = _iterator6.next();
421                 if (_i6.done) break;
422                 _ref6 = _i6.value;
423             }
424
425             var i = _ref6;
426
427             if (i.type === 'div' && i.value === ',') {
428                 return i;
429             }
430         }
431         return { type: 'div', value: ',', after: ' ' };
432     };
433
434     /**
435      * Change colors syntax to old webkit
436      */
437
438
439     Gradient.prototype.colorStops = function colorStops(params) {
440         var result = [];
441         for (var i = 0; i < params.length; i++) {
442             var pos = void 0;
443             var param = params[i];
444             var item = void 0;
445             if (i === 0) {
446                 continue;
447             }
448
449             var color = parser.stringify(param[0]);
450             if (param[1] && param[1].type === 'word') {
451                 pos = param[1].value;
452             } else if (param[2] && param[2].type === 'word') {
453                 pos = param[2].value;
454             }
455
456             var stop = void 0;
457             if (i === 1 && (!pos || pos === '0%')) {
458                 stop = 'from(' + color + ')';
459             } else if (i === params.length - 1 && (!pos || pos === '100%')) {
460                 stop = 'to(' + color + ')';
461             } else if (pos) {
462                 stop = 'color-stop(' + pos + ', ' + color + ')';
463             } else {
464                 stop = 'color-stop(' + color + ')';
465             }
466
467             var div = param[param.length - 1];
468             params[i] = [{ type: 'word', value: stop }];
469             if (div.type === 'div' && div.value === ',') {
470                 item = params[i].push(div);
471             }
472             result.push(item);
473         }
474         return result;
475     };
476
477     /**
478      * Remove old WebKit gradient too
479      */
480
481
482     Gradient.prototype.old = function old(prefix) {
483         if (prefix === '-webkit-') {
484             var type = this.name === 'linear-gradient' ? 'linear' : 'radial';
485             var string = '-gradient';
486             var regexp = utils.regexp('-webkit-(' + type + '-gradient|gradient\\(\\s*' + type + ')', false);
487
488             return new OldValue(this.name, prefix + this.name, string, regexp);
489         } else {
490             return _Value.prototype.old.call(this, prefix);
491         }
492     };
493
494     /**
495      * Do not add non-webkit prefixes for list-style and object
496      */
497
498
499     Gradient.prototype.add = function add(decl, prefix) {
500         var p = decl.prop;
501         if (p.indexOf('mask') !== -1) {
502             if (prefix === '-webkit-' || prefix === '-webkit- old') {
503                 return _Value.prototype.add.call(this, decl, prefix);
504             }
505         } else if (p === 'list-style' || p === 'list-style-image' || p === 'content') {
506             if (prefix === '-webkit-' || prefix === '-webkit- old') {
507                 return _Value.prototype.add.call(this, decl, prefix);
508             }
509         } else {
510             return _Value.prototype.add.call(this, decl, prefix);
511         }
512         return undefined;
513     };
514
515     return Gradient;
516 }(Value);
517
518 Object.defineProperty(Gradient, 'names', {
519     enumerable: true,
520     writable: true,
521     value: ['linear-gradient', 'repeating-linear-gradient', 'radial-gradient', 'repeating-radial-gradient']
522 });
523
524
525 module.exports = Gradient;