UNPKG

15 kBJavaScriptView Raw
1"use strict";
2
3var Declaration = require('./declaration');
4
5var Resolution = require('./resolution');
6
7var Transition = require('./transition');
8
9var Processor = require('./processor');
10
11var Supports = require('./supports');
12
13var Browsers = require('./browsers');
14
15var Selector = require('./selector');
16
17var AtRule = require('./at-rule');
18
19var Value = require('./value');
20
21var utils = require('./utils');
22
23var vendor = require('postcss').vendor;
24
25Selector.hack(require('./hacks/fullscreen'));
26Selector.hack(require('./hacks/placeholder'));
27Declaration.hack(require('./hacks/flex'));
28Declaration.hack(require('./hacks/order'));
29Declaration.hack(require('./hacks/filter'));
30Declaration.hack(require('./hacks/grid-end'));
31Declaration.hack(require('./hacks/animation'));
32Declaration.hack(require('./hacks/flex-flow'));
33Declaration.hack(require('./hacks/flex-grow'));
34Declaration.hack(require('./hacks/flex-wrap'));
35Declaration.hack(require('./hacks/grid-area'));
36Declaration.hack(require('./hacks/place-self'));
37Declaration.hack(require('./hacks/grid-start'));
38Declaration.hack(require('./hacks/align-self'));
39Declaration.hack(require('./hacks/appearance'));
40Declaration.hack(require('./hacks/flex-basis'));
41Declaration.hack(require('./hacks/mask-border'));
42Declaration.hack(require('./hacks/align-items'));
43Declaration.hack(require('./hacks/flex-shrink'));
44Declaration.hack(require('./hacks/break-props'));
45Declaration.hack(require('./hacks/color-adjust'));
46Declaration.hack(require('./hacks/writing-mode'));
47Declaration.hack(require('./hacks/border-image'));
48Declaration.hack(require('./hacks/align-content'));
49Declaration.hack(require('./hacks/border-radius'));
50Declaration.hack(require('./hacks/block-logical'));
51Declaration.hack(require('./hacks/grid-template'));
52Declaration.hack(require('./hacks/inline-logical'));
53Declaration.hack(require('./hacks/grid-row-align'));
54Declaration.hack(require('./hacks/transform-decl'));
55Declaration.hack(require('./hacks/flex-direction'));
56Declaration.hack(require('./hacks/image-rendering'));
57Declaration.hack(require('./hacks/background-clip'));
58Declaration.hack(require('./hacks/text-decoration'));
59Declaration.hack(require('./hacks/justify-content'));
60Declaration.hack(require('./hacks/background-size'));
61Declaration.hack(require('./hacks/grid-row-column'));
62Declaration.hack(require('./hacks/grid-rows-columns'));
63Declaration.hack(require('./hacks/grid-column-align'));
64Declaration.hack(require('./hacks/overscroll-behavior'));
65Declaration.hack(require('./hacks/grid-template-areas'));
66Declaration.hack(require('./hacks/text-emphasis-position'));
67Declaration.hack(require('./hacks/text-decoration-skip-ink'));
68Value.hack(require('./hacks/gradient'));
69Value.hack(require('./hacks/intrinsic'));
70Value.hack(require('./hacks/pixelated'));
71Value.hack(require('./hacks/image-set'));
72Value.hack(require('./hacks/cross-fade'));
73Value.hack(require('./hacks/display-flex'));
74Value.hack(require('./hacks/display-grid'));
75Value.hack(require('./hacks/filter-value'));
76var declsCache = {};
77
78var Prefixes =
79/*#__PURE__*/
80function () {
81 function Prefixes(data, browsers, options) {
82 if (options === void 0) {
83 options = {};
84 }
85
86 this.data = data;
87 this.browsers = browsers;
88 this.options = options;
89
90 var _this$preprocess = this.preprocess(this.select(this.data));
91
92 this.add = _this$preprocess[0];
93 this.remove = _this$preprocess[1];
94 this.transition = new Transition(this);
95 this.processor = new Processor(this);
96 }
97 /**
98 * Return clone instance to remove all prefixes
99 */
100
101
102 var _proto = Prefixes.prototype;
103
104 _proto.cleaner = function cleaner() {
105 if (this.cleanerCache) {
106 return this.cleanerCache;
107 }
108
109 if (this.browsers.selected.length) {
110 var empty = new Browsers(this.browsers.data, []);
111 this.cleanerCache = new Prefixes(this.data, empty, this.options);
112 } else {
113 return this;
114 }
115
116 return this.cleanerCache;
117 }
118 /**
119 * Select prefixes from data, which is necessary for selected browsers
120 */
121 ;
122
123 _proto.select = function select(list) {
124 var _this = this;
125
126 var selected = {
127 add: {},
128 remove: {}
129 };
130
131 var _loop = function _loop(name) {
132 var data = list[name];
133 var add = data.browsers.map(function (i) {
134 var params = i.split(' ');
135 return {
136 browser: params[0] + " " + params[1],
137 note: params[2]
138 };
139 });
140 var notes = add.filter(function (i) {
141 return i.note;
142 }).map(function (i) {
143 return _this.browsers.prefix(i.browser) + " " + i.note;
144 });
145 notes = utils.uniq(notes);
146 add = add.filter(function (i) {
147 return _this.browsers.isSelected(i.browser);
148 }).map(function (i) {
149 var prefix = _this.browsers.prefix(i.browser);
150
151 if (i.note) {
152 return prefix + " " + i.note;
153 } else {
154 return prefix;
155 }
156 });
157 add = _this.sort(utils.uniq(add));
158
159 if (_this.options.flexbox === 'no-2009') {
160 add = add.filter(function (i) {
161 return i.indexOf('2009') === -1;
162 });
163 }
164
165 var all = data.browsers.map(function (i) {
166 return _this.browsers.prefix(i);
167 });
168
169 if (data.mistakes) {
170 all = all.concat(data.mistakes);
171 }
172
173 all = all.concat(notes);
174 all = utils.uniq(all);
175
176 if (add.length) {
177 selected.add[name] = add;
178
179 if (add.length < all.length) {
180 selected.remove[name] = all.filter(function (i) {
181 return add.indexOf(i) === -1;
182 });
183 }
184 } else {
185 selected.remove[name] = all;
186 }
187 };
188
189 for (var name in list) {
190 _loop(name);
191 }
192
193 return selected;
194 }
195 /**
196 * Sort vendor prefixes
197 */
198 ;
199
200 _proto.sort = function sort(prefixes) {
201 return prefixes.sort(function (a, b) {
202 var aLength = utils.removeNote(a).length;
203 var bLength = utils.removeNote(b).length;
204
205 if (aLength === bLength) {
206 return b.length - a.length;
207 } else {
208 return bLength - aLength;
209 }
210 });
211 }
212 /**
213 * Cache prefixes data to fast CSS processing
214 */
215 ;
216
217 _proto.preprocess = function preprocess(selected) {
218 var add = {
219 'selectors': [],
220 '@supports': new Supports(Prefixes, this)
221 };
222
223 for (var name in selected.add) {
224 var prefixes = selected.add[name];
225
226 if (name === '@keyframes' || name === '@viewport') {
227 add[name] = new AtRule(name, prefixes, this);
228 } else if (name === '@resolution') {
229 add[name] = new Resolution(name, prefixes, this);
230 } else if (this.data[name].selector) {
231 add.selectors.push(Selector.load(name, prefixes, this));
232 } else {
233 var props = this.data[name].props;
234
235 if (props) {
236 var value = Value.load(name, prefixes, this);
237
238 for (var _iterator = props, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
239 var _ref;
240
241 if (_isArray) {
242 if (_i >= _iterator.length) break;
243 _ref = _iterator[_i++];
244 } else {
245 _i = _iterator.next();
246 if (_i.done) break;
247 _ref = _i.value;
248 }
249
250 var prop = _ref;
251
252 if (!add[prop]) {
253 add[prop] = {
254 values: []
255 };
256 }
257
258 add[prop].values.push(value);
259 }
260 } else {
261 var values = add[name] && add[name].values || [];
262 add[name] = Declaration.load(name, prefixes, this);
263 add[name].values = values;
264 }
265 }
266 }
267
268 var remove = {
269 selectors: []
270 };
271
272 for (var _name in selected.remove) {
273 var _prefixes = selected.remove[_name];
274
275 if (this.data[_name].selector) {
276 var selector = Selector.load(_name, _prefixes);
277
278 for (var _iterator2 = _prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
279 var _ref2;
280
281 if (_isArray2) {
282 if (_i2 >= _iterator2.length) break;
283 _ref2 = _iterator2[_i2++];
284 } else {
285 _i2 = _iterator2.next();
286 if (_i2.done) break;
287 _ref2 = _i2.value;
288 }
289
290 var prefix = _ref2;
291 remove.selectors.push(selector.old(prefix));
292 }
293 } else if (_name === '@keyframes' || _name === '@viewport') {
294 for (var _iterator3 = _prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
295 var _ref3;
296
297 if (_isArray3) {
298 if (_i3 >= _iterator3.length) break;
299 _ref3 = _iterator3[_i3++];
300 } else {
301 _i3 = _iterator3.next();
302 if (_i3.done) break;
303 _ref3 = _i3.value;
304 }
305
306 var _prefix = _ref3;
307
308 var prefixed = "@" + _prefix + _name.slice(1);
309
310 remove[prefixed] = {
311 remove: true
312 };
313 }
314 } else if (_name === '@resolution') {
315 remove[_name] = new Resolution(_name, _prefixes, this);
316 } else {
317 var _props = this.data[_name].props;
318
319 if (_props) {
320 var _value = Value.load(_name, [], this);
321
322 for (var _iterator4 = _prefixes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
323 var _ref4;
324
325 if (_isArray4) {
326 if (_i4 >= _iterator4.length) break;
327 _ref4 = _iterator4[_i4++];
328 } else {
329 _i4 = _iterator4.next();
330 if (_i4.done) break;
331 _ref4 = _i4.value;
332 }
333
334 var _prefix2 = _ref4;
335
336 var old = _value.old(_prefix2);
337
338 if (old) {
339 for (var _iterator5 = _props, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
340 var _ref5;
341
342 if (_isArray5) {
343 if (_i5 >= _iterator5.length) break;
344 _ref5 = _iterator5[_i5++];
345 } else {
346 _i5 = _iterator5.next();
347 if (_i5.done) break;
348 _ref5 = _i5.value;
349 }
350
351 var _prop = _ref5;
352
353 if (!remove[_prop]) {
354 remove[_prop] = {};
355 }
356
357 if (!remove[_prop].values) {
358 remove[_prop].values = [];
359 }
360
361 remove[_prop].values.push(old);
362 }
363 }
364 }
365 } else {
366 for (var _iterator6 = _prefixes, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
367 var _ref6;
368
369 if (_isArray6) {
370 if (_i6 >= _iterator6.length) break;
371 _ref6 = _iterator6[_i6++];
372 } else {
373 _i6 = _iterator6.next();
374 if (_i6.done) break;
375 _ref6 = _i6.value;
376 }
377
378 var _prefix3 = _ref6;
379 var olds = this.decl(_name).old(_name, _prefix3);
380
381 if (_name === 'align-self') {
382 var a = add[_name] && add[_name].prefixes;
383
384 if (a) {
385 if (_prefix3 === '-webkit- 2009' && a.indexOf('-webkit-') !== -1) {
386 continue;
387 } else if (_prefix3 === '-webkit-' && a.indexOf('-webkit- 2009') !== -1) {
388 continue;
389 }
390 }
391 }
392
393 for (var _iterator7 = olds, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
394 var _ref7;
395
396 if (_isArray7) {
397 if (_i7 >= _iterator7.length) break;
398 _ref7 = _iterator7[_i7++];
399 } else {
400 _i7 = _iterator7.next();
401 if (_i7.done) break;
402 _ref7 = _i7.value;
403 }
404
405 var _prefixed = _ref7;
406
407 if (!remove[_prefixed]) {
408 remove[_prefixed] = {};
409 }
410
411 remove[_prefixed].remove = true;
412 }
413 }
414 }
415 }
416 }
417
418 return [add, remove];
419 }
420 /**
421 * Declaration loader with caching
422 */
423 ;
424
425 _proto.decl = function decl(prop) {
426 var decl = declsCache[prop];
427
428 if (decl) {
429 return decl;
430 } else {
431 declsCache[prop] = Declaration.load(prop);
432 return declsCache[prop];
433 }
434 }
435 /**
436 * Return unprefixed version of property
437 */
438 ;
439
440 _proto.unprefixed = function unprefixed(prop) {
441 var value = this.normalize(vendor.unprefixed(prop));
442
443 if (value === 'flex-direction') {
444 value = 'flex-flow';
445 }
446
447 return value;
448 }
449 /**
450 * Normalize prefix for remover
451 */
452 ;
453
454 _proto.normalize = function normalize(prop) {
455 return this.decl(prop).normalize(prop);
456 }
457 /**
458 * Return prefixed version of property
459 */
460 ;
461
462 _proto.prefixed = function prefixed(prop, prefix) {
463 prop = vendor.unprefixed(prop);
464 return this.decl(prop).prefixed(prop, prefix);
465 }
466 /**
467 * Return values, which must be prefixed in selected property
468 */
469 ;
470
471 _proto.values = function values(type, prop) {
472 var data = this[type];
473 var global = data['*'] && data['*'].values;
474 var values = data[prop] && data[prop].values;
475
476 if (global && values) {
477 return utils.uniq(global.concat(values));
478 } else {
479 return global || values || [];
480 }
481 }
482 /**
483 * Group declaration by unprefixed property to check them
484 */
485 ;
486
487 _proto.group = function group(decl) {
488 var _this2 = this;
489
490 var rule = decl.parent;
491 var index = rule.index(decl);
492 var length = rule.nodes.length;
493 var unprefixed = this.unprefixed(decl.prop);
494
495 var checker = function checker(step, callback) {
496 index += step;
497
498 while (index >= 0 && index < length) {
499 var other = rule.nodes[index];
500
501 if (other.type === 'decl') {
502 if (step === -1 && other.prop === unprefixed) {
503 if (!Browsers.withPrefix(other.value)) {
504 break;
505 }
506 }
507
508 if (_this2.unprefixed(other.prop) !== unprefixed) {
509 break;
510 } else if (callback(other) === true) {
511 return true;
512 }
513
514 if (step === +1 && other.prop === unprefixed) {
515 if (!Browsers.withPrefix(other.value)) {
516 break;
517 }
518 }
519 }
520
521 index += step;
522 }
523
524 return false;
525 };
526
527 return {
528 up: function up(callback) {
529 return checker(-1, callback);
530 },
531 down: function down(callback) {
532 return checker(+1, callback);
533 }
534 };
535 };
536
537 return Prefixes;
538}();
539
540module.exports = Prefixes;
\No newline at end of file