.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / declaration.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 Prefixer = require('./prefixer');
12 var Browsers = require('./browsers');
13 var utils = require('./utils');
14
15 var Declaration = function (_Prefixer) {
16     _inherits(Declaration, _Prefixer);
17
18     function Declaration() {
19         _classCallCheck(this, Declaration);
20
21         return _possibleConstructorReturn(this, _Prefixer.apply(this, arguments));
22     }
23
24     /**
25      * Always true, because we already get prefixer by property name
26      */
27     Declaration.prototype.check = function check() /* decl */{
28         return true;
29     };
30
31     /**
32      * Return prefixed version of property
33      */
34
35
36     Declaration.prototype.prefixed = function prefixed(prop, prefix) {
37         return prefix + prop;
38     };
39
40     /**
41      * Return unprefixed version of property
42      */
43
44
45     Declaration.prototype.normalize = function normalize(prop) {
46         return prop;
47     };
48
49     /**
50      * Check `value`, that it contain other prefixes, rather than `prefix`
51      */
52
53
54     Declaration.prototype.otherPrefixes = function otherPrefixes(value, prefix) {
55         for (var _iterator = Browsers.prefixes(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
56             var _ref;
57
58             if (_isArray) {
59                 if (_i >= _iterator.length) break;
60                 _ref = _iterator[_i++];
61             } else {
62                 _i = _iterator.next();
63                 if (_i.done) break;
64                 _ref = _i.value;
65             }
66
67             var other = _ref;
68
69             if (other === prefix) {
70                 continue;
71             }
72             if (value.indexOf(other) !== -1) {
73                 return true;
74             }
75         }
76         return false;
77     };
78
79     /**
80      * Set prefix to declaration
81      */
82
83
84     Declaration.prototype.set = function set(decl, prefix) {
85         decl.prop = this.prefixed(decl.prop, prefix);
86         return decl;
87     };
88
89     /**
90      * Should we use visual cascade for prefixes
91      */
92
93
94     Declaration.prototype.needCascade = function needCascade(decl) {
95         if (!decl._autoprefixerCascade) {
96             decl._autoprefixerCascade = this.all.options.cascade !== false && decl.raw('before').indexOf('\n') !== -1;
97         }
98         return decl._autoprefixerCascade;
99     };
100
101     /**
102      * Return maximum length of possible prefixed property
103      */
104
105
106     Declaration.prototype.maxPrefixed = function maxPrefixed(prefixes, decl) {
107         if (decl._autoprefixerMax) {
108             return decl._autoprefixerMax;
109         }
110
111         var max = 0;
112         for (var _iterator2 = prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
113             var _ref2;
114
115             if (_isArray2) {
116                 if (_i2 >= _iterator2.length) break;
117                 _ref2 = _iterator2[_i2++];
118             } else {
119                 _i2 = _iterator2.next();
120                 if (_i2.done) break;
121                 _ref2 = _i2.value;
122             }
123
124             var prefix = _ref2;
125
126             prefix = utils.removeNote(prefix);
127             if (prefix.length > max) {
128                 max = prefix.length;
129             }
130         }
131         decl._autoprefixerMax = max;
132
133         return decl._autoprefixerMax;
134     };
135
136     /**
137      * Calculate indentation to create visual cascade
138      */
139
140
141     Declaration.prototype.calcBefore = function calcBefore(prefixes, decl) {
142         var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
143
144         var max = this.maxPrefixed(prefixes, decl);
145         var diff = max - utils.removeNote(prefix).length;
146
147         var before = decl.raw('before');
148         if (diff > 0) {
149             before += Array(diff).fill(' ').join('');
150         }
151
152         return before;
153     };
154
155     /**
156      * Remove visual cascade
157      */
158
159
160     Declaration.prototype.restoreBefore = function restoreBefore(decl) {
161         var lines = decl.raw('before').split('\n');
162         var min = lines[lines.length - 1];
163
164         this.all.group(decl).up(function (prefixed) {
165             var array = prefixed.raw('before').split('\n');
166             var last = array[array.length - 1];
167             if (last.length < min.length) {
168                 min = last;
169             }
170         });
171
172         lines[lines.length - 1] = min;
173         decl.raws.before = lines.join('\n');
174     };
175
176     /**
177      * Clone and insert new declaration
178      */
179
180
181     Declaration.prototype.insert = function insert(decl, prefix, prefixes) {
182         var cloned = this.set(this.clone(decl), prefix);
183         if (!cloned) return undefined;
184
185         var already = decl.parent.some(function (i) {
186             return i.prop === cloned.prop && i.value === cloned.value;
187         });
188         if (already) {
189             return undefined;
190         }
191
192         if (this.needCascade(decl)) {
193             cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
194         }
195         return decl.parent.insertBefore(decl, cloned);
196     };
197
198     /**
199      * Did this declaration has this prefix above
200      */
201
202
203     Declaration.prototype.isAlready = function isAlready(decl, prefixed) {
204         var already = this.all.group(decl).up(function (i) {
205             return i.prop === prefixed;
206         });
207         if (!already) {
208             already = this.all.group(decl).down(function (i) {
209                 return i.prop === prefixed;
210             });
211         }
212         return already;
213     };
214
215     /**
216      * Clone and add prefixes for declaration
217      */
218
219
220     Declaration.prototype.add = function add(decl, prefix, prefixes, result) {
221         var prefixed = this.prefixed(decl.prop, prefix);
222         if (this.isAlready(decl, prefixed) || this.otherPrefixes(decl.value, prefix)) {
223             return undefined;
224         }
225         return this.insert(decl, prefix, prefixes, result);
226     };
227
228     /**
229      * Add spaces for visual cascade
230      */
231
232
233     Declaration.prototype.process = function process(decl, result) {
234         if (!this.needCascade(decl)) {
235             _Prefixer.prototype.process.call(this, decl, result);
236             return;
237         }
238
239         var prefixes = _Prefixer.prototype.process.call(this, decl, result);
240
241         if (!prefixes || !prefixes.length) {
242             return;
243         }
244
245         this.restoreBefore(decl);
246         decl.raws.before = this.calcBefore(prefixes, decl);
247     };
248
249     /**
250      * Return list of prefixed properties to clean old prefixes
251      */
252
253
254     Declaration.prototype.old = function old(prop, prefix) {
255         return [this.prefixed(prop, prefix)];
256     };
257
258     return Declaration;
259 }(Prefixer);
260
261 module.exports = Declaration;