UNPKG

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