UNPKG

23 kBJavaScriptView Raw
1"use strict";
2
3var parser = require('postcss-value-parser');
4
5var Value = require('./value');
6
7var insertAreas = require('./hacks/grid-utils').insertAreas;
8
9var OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i;
10var OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i;
11var IGNORE_NEXT = /(!\s*)?autoprefixer:\s*ignore\s+next/i;
12var GRID_REGEX = /(!\s*)?autoprefixer\s*grid:\s*(on|off|(no-)?autoplace)/i;
13var 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'];
14
15function hasGridTemplate(decl) {
16 return decl.parent.some(function (i) {
17 return i.prop === 'grid-template' || i.prop === 'grid-template-areas';
18 });
19}
20
21function hasRowsAndColumns(decl) {
22 var hasRows = decl.parent.some(function (i) {
23 return i.prop === 'grid-template-rows';
24 });
25 var hasColumns = decl.parent.some(function (i) {
26 return i.prop === 'grid-template-columns';
27 });
28 return hasRows && hasColumns;
29}
30
31var Processor =
32/*#__PURE__*/
33function () {
34 function Processor(prefixes) {
35 this.prefixes = prefixes;
36 }
37 /**
38 * Add necessary prefixes
39 */
40
41
42 var _proto = Processor.prototype;
43
44 _proto.add = function add(css, result) {
45 var _this = this;
46
47 // At-rules
48 var resolution = this.prefixes.add['@resolution'];
49 var keyframes = this.prefixes.add['@keyframes'];
50 var viewport = this.prefixes.add['@viewport'];
51 var supports = this.prefixes.add['@supports'];
52 css.walkAtRules(function (rule) {
53 if (rule.name === 'keyframes') {
54 if (!_this.disabled(rule, result)) {
55 return keyframes && keyframes.process(rule);
56 }
57 } else if (rule.name === 'viewport') {
58 if (!_this.disabled(rule, result)) {
59 return viewport && viewport.process(rule);
60 }
61 } else if (rule.name === 'supports') {
62 if (_this.prefixes.options.supports !== false && !_this.disabled(rule, result)) {
63 return supports.process(rule);
64 }
65 } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
66 if (!_this.disabled(rule, result)) {
67 return resolution && resolution.process(rule);
68 }
69 }
70
71 return undefined;
72 }); // Selectors
73
74 css.walkRules(function (rule) {
75 if (_this.disabled(rule, result)) return undefined;
76 return _this.prefixes.add.selectors.map(function (selector) {
77 return selector.process(rule, result);
78 });
79 });
80
81 function insideGrid(decl) {
82 return decl.parent.some(function (subDecl) {
83 var displayGrid = subDecl.prop === 'display' && /(inline-)?grid/.test(subDecl.value);
84 var gridTemplate = /^grid-template/.test(subDecl.prop);
85 var gridGap = /^grid-([A-z]+-)?gap/.test(subDecl.prop);
86 return displayGrid || gridTemplate || gridGap;
87 });
88 }
89
90 function insideFlex(decl) {
91 return decl.parent.some(function (subDecl) {
92 return subDecl.prop === 'display' && /(inline-)?flex/.test(subDecl.value);
93 });
94 }
95
96 var gridPrefixes = this.gridStatus(css, result) && this.prefixes.add['grid-area'] && this.prefixes.add['grid-area'].prefixes;
97 css.walkDecls(function (decl) {
98 if (_this.disabledDecl(decl, result)) return undefined;
99 var parent = decl.parent;
100 var prop = decl.prop;
101 var value = decl.value;
102
103 if (prop === 'grid-row-span') {
104 result.warn('grid-row-span is not part of final Grid Layout. Use grid-row.', {
105 node: decl
106 });
107 return undefined;
108 } else if (prop === 'grid-column-span') {
109 result.warn('grid-column-span is not part of final Grid Layout. Use grid-column.', {
110 node: decl
111 });
112 return undefined;
113 } else if (prop === 'display' && value === 'box') {
114 result.warn('You should write display: flex by final spec ' + 'instead of display: box', {
115 node: decl
116 });
117 return undefined;
118 } else if (prop === 'text-emphasis-position') {
119 if (value === 'under' || value === 'over') {
120 result.warn('You should use 2 values for text-emphasis-position ' + 'For example, `under left` instead of just `under`.', {
121 node: decl
122 });
123 }
124 } else if (/^(align|justify|place)-(items|content)$/.test(prop) && insideFlex(decl)) {
125 if (value === 'start' || value === 'end') {
126 result.warn(value + " value has mixed support, consider using " + ("flex-" + value + " instead"), {
127 node: decl
128 });
129 }
130 } else if (prop === 'text-decoration-skip' && value === 'ink') {
131 result.warn('Replace text-decoration-skip: ink to ' + 'text-decoration-skip-ink: auto, because spec had been changed', {
132 node: decl
133 });
134 } else {
135 if (gridPrefixes) {
136 if (/^(align|justify|place)-items$/.test(prop) && insideGrid(decl)) {
137 var fixed = prop.replace('-items', '-self');
138 result.warn("IE does not support " + prop + " on grid containers. " + ("Try using " + fixed + " on child elements instead: ") + (decl.parent.selector + " > * { " + fixed + ": " + decl.value + " }"), {
139 node: decl
140 });
141 } else if (/^(align|justify|place)-content$/.test(prop) && insideGrid(decl)) {
142 result.warn("IE does not support " + decl.prop + " on grid containers", {
143 node: decl
144 });
145 } else if (prop === 'display' && decl.value === 'contents') {
146 result.warn('Please do not use display: contents; ' + 'if you have grid setting enabled', {
147 node: decl
148 });
149 return undefined;
150 } else if (decl.prop === 'grid-gap') {
151 var status = _this.gridStatus(decl, result);
152
153 if (status === 'autoplace' && !hasRowsAndColumns(decl) && !hasGridTemplate(decl)) {
154 result.warn('grid-gap only works if grid-template(-areas) is being ' + 'used or both rows and columns have been declared ' + 'and cells have not been manually ' + 'placed inside the explicit grid', {
155 node: decl
156 });
157 } else if ((status === true || status === 'no-autoplace') && !hasGridTemplate(decl)) {
158 result.warn("grid-gap only works if grid-template(-areas) is being used", {
159 node: decl
160 });
161 }
162 } else if (prop === 'grid-auto-columns') {
163 result.warn('grid-auto-columns is not supported by IE', {
164 node: decl
165 });
166 return undefined;
167 } else if (prop === 'grid-auto-rows') {
168 result.warn('grid-auto-rows is not supported by IE', {
169 node: decl
170 });
171 return undefined;
172 } else if (prop === 'grid-auto-flow') {
173 var hasRows = parent.some(function (i) {
174 return i.prop === 'grid-template-rows';
175 });
176 var hasCols = parent.some(function (i) {
177 return i.prop === 'grid-template-columns';
178 });
179
180 if (hasGridTemplate(decl)) {
181 result.warn('grid-auto-flow is not supported by IE', {
182 node: decl
183 });
184 } else if (value.includes('dense')) {
185 result.warn('grid-auto-flow: dense is not supported by IE', {
186 node: decl
187 });
188 } else if (!hasRows && !hasCols) {
189 result.warn('grid-auto-flow works only if grid-template-rows and ' + 'grid-template-columns are present in the same rule', {
190 node: decl
191 });
192 }
193
194 return undefined;
195 } else if (value.indexOf('auto-fit') !== -1) {
196 result.warn('auto-fit value is not supported by IE', {
197 node: decl,
198 word: 'auto-fit'
199 });
200 return undefined;
201 } else if (value.indexOf('auto-fill') !== -1) {
202 result.warn('auto-fill value is not supported by IE', {
203 node: decl,
204 word: 'auto-fill'
205 });
206 return undefined;
207 }
208 }
209
210 if (value.indexOf('radial-gradient') !== -1) {
211 if (OLD_RADIAL.test(decl.value)) {
212 result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `closest-side at 0 0` ' + 'instead of `0 0, closest-side`.', {
213 node: decl
214 });
215 } else {
216 var ast = parser(value);
217
218 for (var _iterator = ast.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
219 var _ref;
220
221 if (_isArray) {
222 if (_i >= _iterator.length) break;
223 _ref = _iterator[_i++];
224 } else {
225 _i = _iterator.next();
226 if (_i.done) break;
227 _ref = _i.value;
228 }
229
230 var i = _ref;
231
232 if (i.type === 'function' && i.value === 'radial-gradient') {
233 for (var _iterator2 = i.nodes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
234 var _ref2;
235
236 if (_isArray2) {
237 if (_i2 >= _iterator2.length) break;
238 _ref2 = _iterator2[_i2++];
239 } else {
240 _i2 = _iterator2.next();
241 if (_i2.done) break;
242 _ref2 = _i2.value;
243 }
244
245 var word = _ref2;
246
247 if (word.type === 'word') {
248 if (word.value === 'cover') {
249 result.warn('Gradient has outdated direction syntax. ' + 'Replace `cover` to `farthest-corner`.', {
250 node: decl
251 });
252 } else if (word.value === 'contain') {
253 result.warn('Gradient has outdated direction syntax. ' + 'Replace `contain` to `closest-side`.', {
254 node: decl
255 });
256 }
257 }
258 }
259 }
260 }
261 }
262 }
263
264 if (value.indexOf('linear-gradient') !== -1) {
265 if (OLD_LINEAR.test(value)) {
266 result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `to left` instead of `right`.', {
267 node: decl
268 });
269 }
270 }
271 }
272
273 if (SIZES.indexOf(decl.prop) !== -1) {
274 if (decl.value.indexOf('-fill-available') === -1) {
275 if (decl.value.indexOf('fill-available') !== -1) {
276 result.warn('Replace fill-available to stretch, ' + 'because spec had been changed', {
277 node: decl
278 });
279 } else if (decl.value.indexOf('fill') !== -1) {
280 result.warn('Replace fill to stretch, because spec had been changed', {
281 node: decl
282 });
283 }
284 }
285 }
286
287 var prefixer;
288
289 if (decl.prop === 'transition' || decl.prop === 'transition-property') {
290 // Transition
291 return _this.prefixes.transition.add(decl, result);
292 } else if (decl.prop === 'align-self') {
293 // align-self flexbox or grid
294 var display = _this.displayType(decl);
295
296 if (display !== 'grid' && _this.prefixes.options.flexbox !== false) {
297 prefixer = _this.prefixes.add['align-self'];
298
299 if (prefixer && prefixer.prefixes) {
300 prefixer.process(decl);
301 }
302 }
303
304 if (display !== 'flex' && _this.gridStatus(decl, result) !== false) {
305 prefixer = _this.prefixes.add['grid-row-align'];
306
307 if (prefixer && prefixer.prefixes) {
308 return prefixer.process(decl, result);
309 }
310 }
311 } else if (decl.prop === 'justify-self') {
312 // justify-self flexbox or grid
313 var _display = _this.displayType(decl);
314
315 if (_display !== 'flex' && _this.gridStatus(decl, result) !== false) {
316 prefixer = _this.prefixes.add['grid-column-align'];
317
318 if (prefixer && prefixer.prefixes) {
319 return prefixer.process(decl, result);
320 }
321 }
322 } else if (decl.prop === 'place-self') {
323 prefixer = _this.prefixes.add['place-self'];
324
325 if (prefixer && prefixer.prefixes && _this.gridStatus(decl, result) !== false) {
326 return prefixer.process(decl, result);
327 }
328 } else {
329 // Properties
330 prefixer = _this.prefixes.add[decl.prop];
331
332 if (prefixer && prefixer.prefixes) {
333 return prefixer.process(decl, result);
334 }
335 }
336
337 return undefined;
338 }); // Insert grid-area prefixes. We need to be able to store the different
339 // rules as a data and hack API is not enough for this
340
341 if (this.gridStatus(css, result)) {
342 insertAreas(css, this.disabled);
343 } // Values
344
345
346 return css.walkDecls(function (decl) {
347 if (_this.disabledValue(decl, result)) return;
348
349 var unprefixed = _this.prefixes.unprefixed(decl.prop);
350
351 for (var _iterator3 = _this.prefixes.values('add', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
352 var _ref3;
353
354 if (_isArray3) {
355 if (_i3 >= _iterator3.length) break;
356 _ref3 = _iterator3[_i3++];
357 } else {
358 _i3 = _iterator3.next();
359 if (_i3.done) break;
360 _ref3 = _i3.value;
361 }
362
363 var value = _ref3;
364 value.process(decl, result);
365 }
366
367 Value.save(_this.prefixes, decl);
368 });
369 }
370 /**
371 * Remove unnecessary pefixes
372 */
373 ;
374
375 _proto.remove = function remove(css, result) {
376 var _this2 = this;
377
378 // At-rules
379 var resolution = this.prefixes.remove['@resolution'];
380 css.walkAtRules(function (rule, i) {
381 if (_this2.prefixes.remove["@" + rule.name]) {
382 if (!_this2.disabled(rule, result)) {
383 rule.parent.removeChild(i);
384 }
385 } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1 && resolution) {
386 resolution.clean(rule);
387 }
388 }); // Selectors
389
390 var _loop = function _loop() {
391 if (_isArray4) {
392 if (_i4 >= _iterator4.length) return "break";
393 _ref4 = _iterator4[_i4++];
394 } else {
395 _i4 = _iterator4.next();
396 if (_i4.done) return "break";
397 _ref4 = _i4.value;
398 }
399
400 var checker = _ref4;
401 css.walkRules(function (rule, i) {
402 if (checker.check(rule)) {
403 if (!_this2.disabled(rule, result)) {
404 rule.parent.removeChild(i);
405 }
406 }
407 });
408 };
409
410 for (var _iterator4 = this.prefixes.remove.selectors, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
411 var _ref4;
412
413 var _ret = _loop();
414
415 if (_ret === "break") break;
416 }
417
418 return css.walkDecls(function (decl, i) {
419 if (_this2.disabled(decl, result)) return;
420 var rule = decl.parent;
421
422 var unprefixed = _this2.prefixes.unprefixed(decl.prop); // Transition
423
424
425 if (decl.prop === 'transition' || decl.prop === 'transition-property') {
426 _this2.prefixes.transition.remove(decl);
427 } // Properties
428
429
430 if (_this2.prefixes.remove[decl.prop] && _this2.prefixes.remove[decl.prop].remove) {
431 var notHack = _this2.prefixes.group(decl).down(function (other) {
432 return _this2.prefixes.normalize(other.prop) === unprefixed;
433 });
434
435 if (unprefixed === 'flex-flow') {
436 notHack = true;
437 }
438
439 if (notHack && !_this2.withHackValue(decl)) {
440 if (decl.raw('before').indexOf('\n') > -1) {
441 _this2.reduceSpaces(decl);
442 }
443
444 rule.removeChild(i);
445 return;
446 }
447 } // Values
448
449
450 for (var _iterator5 = _this2.prefixes.values('remove', unprefixed), _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
451 var _ref5;
452
453 if (_isArray5) {
454 if (_i5 >= _iterator5.length) break;
455 _ref5 = _iterator5[_i5++];
456 } else {
457 _i5 = _iterator5.next();
458 if (_i5.done) break;
459 _ref5 = _i5.value;
460 }
461
462 var checker = _ref5;
463
464 if (!checker.check(decl.value)) {
465 continue;
466 }
467
468 unprefixed = checker.unprefixed;
469
470 var _notHack = _this2.prefixes.group(decl).down(function (other) {
471 return other.value.indexOf(unprefixed) !== -1;
472 });
473
474 if (_notHack) {
475 rule.removeChild(i);
476 return;
477 }
478 }
479 });
480 }
481 /**
482 * Some rare old values, which is not in standard
483 */
484 ;
485
486 _proto.withHackValue = function withHackValue(decl) {
487 return decl.prop === '-webkit-background-clip' && decl.value === 'text';
488 }
489 /**
490 * Check for grid/flexbox options.
491 */
492 ;
493
494 _proto.disabledValue = function disabledValue(node, result) {
495 if (this.gridStatus(node, result) === false && node.type === 'decl') {
496 if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
497 return true;
498 }
499 }
500
501 if (this.prefixes.options.flexbox === false && node.type === 'decl') {
502 if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
503 return true;
504 }
505 }
506
507 return this.disabled(node, result);
508 }
509 /**
510 * Check for grid/flexbox options.
511 */
512 ;
513
514 _proto.disabledDecl = function disabledDecl(node, result) {
515 if (this.gridStatus(node, result) === false && node.type === 'decl') {
516 if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
517 return true;
518 }
519 }
520
521 if (this.prefixes.options.flexbox === false && node.type === 'decl') {
522 var other = ['order', 'justify-content', 'align-items', 'align-content'];
523
524 if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
525 return true;
526 }
527 }
528
529 return this.disabled(node, result);
530 }
531 /**
532 * Check for control comment and global options
533 */
534 ;
535
536 _proto.disabled = function disabled(node, result) {
537 if (!node) return false;
538
539 if (node._autoprefixerDisabled !== undefined) {
540 return node._autoprefixerDisabled;
541 }
542
543 if (node.parent) {
544 var p = node.prev();
545
546 if (p && p.type === 'comment' && IGNORE_NEXT.test(p.text)) {
547 node._autoprefixerDisabled = true;
548 node._autoprefixerSelfDisabled = true;
549 return true;
550 }
551 }
552
553 var value = null;
554
555 if (node.nodes) {
556 var status;
557 node.each(function (i) {
558 if (i.type !== 'comment') return;
559
560 if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
561 if (typeof status !== 'undefined') {
562 result.warn('Second Autoprefixer control comment ' + 'was ignored. Autoprefixer applies control ' + 'comment to whole block, not to next rules.', {
563 node: i
564 });
565 } else {
566 status = /on/i.test(i.text);
567 }
568 }
569 });
570
571 if (status !== undefined) {
572 value = !status;
573 }
574 }
575
576 if (!node.nodes || value === null) {
577 if (node.parent) {
578 var isParentDisabled = this.disabled(node.parent, result);
579
580 if (node.parent._autoprefixerSelfDisabled === true) {
581 value = false;
582 } else {
583 value = isParentDisabled;
584 }
585 } else {
586 value = false;
587 }
588 }
589
590 node._autoprefixerDisabled = value;
591 return value;
592 }
593 /**
594 * Normalize spaces in cascade declaration group
595 */
596 ;
597
598 _proto.reduceSpaces = function reduceSpaces(decl) {
599 var stop = false;
600 this.prefixes.group(decl).up(function () {
601 stop = true;
602 return true;
603 });
604
605 if (stop) {
606 return;
607 }
608
609 var parts = decl.raw('before').split('\n');
610 var prevMin = parts[parts.length - 1].length;
611 var diff = false;
612 this.prefixes.group(decl).down(function (other) {
613 parts = other.raw('before').split('\n');
614 var last = parts.length - 1;
615
616 if (parts[last].length > prevMin) {
617 if (diff === false) {
618 diff = parts[last].length - prevMin;
619 }
620
621 parts[last] = parts[last].slice(0, -diff);
622 other.raws.before = parts.join('\n');
623 }
624 });
625 }
626 /**
627 * Is it flebox or grid rule
628 */
629 ;
630
631 _proto.displayType = function displayType(decl) {
632 for (var _iterator6 = decl.parent.nodes, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
633 var _ref6;
634
635 if (_isArray6) {
636 if (_i6 >= _iterator6.length) break;
637 _ref6 = _iterator6[_i6++];
638 } else {
639 _i6 = _iterator6.next();
640 if (_i6.done) break;
641 _ref6 = _i6.value;
642 }
643
644 var i = _ref6;
645
646 if (i.prop !== 'display') {
647 continue;
648 }
649
650 if (i.value.indexOf('flex') !== -1) {
651 return 'flex';
652 }
653
654 if (i.value.indexOf('grid') !== -1) {
655 return 'grid';
656 }
657 }
658
659 return false;
660 }
661 /**
662 * Set grid option via control comment
663 */
664 ;
665
666 _proto.gridStatus = function gridStatus(node, result) {
667 if (!node) return false;
668
669 if (node._autoprefixerGridStatus !== undefined) {
670 return node._autoprefixerGridStatus;
671 }
672
673 var value = null;
674
675 if (node.nodes) {
676 var status;
677 node.each(function (i) {
678 if (i.type !== 'comment') return;
679
680 if (GRID_REGEX.test(i.text)) {
681 var hasAutoplace = /:\s*autoplace/i.test(i.text);
682 var noAutoplace = /no-autoplace/i.test(i.text);
683
684 if (typeof status !== 'undefined') {
685 result.warn("Second Autoprefixer grid control comment was " + 'ignored. Autoprefixer applies control comments to the whole ' + 'block, not to the next rules.', {
686 node: i
687 });
688 } else if (hasAutoplace) {
689 status = 'autoplace';
690 } else if (noAutoplace) {
691 status = true;
692 } else {
693 status = /on/i.test(i.text);
694 }
695 }
696 });
697
698 if (status !== undefined) {
699 value = status;
700 }
701 }
702
703 if (node.type === 'atrule' && node.name === 'supports') {
704 var params = node.params;
705
706 if (params.indexOf('grid') !== -1 && params.indexOf('auto') !== -1) {
707 value = false;
708 }
709 }
710
711 if (!node.nodes || value === null) {
712 if (node.parent) {
713 var isParentGrid = this.gridStatus(node.parent, result);
714
715 if (node.parent._autoprefixerSelfDisabled === true) {
716 value = false;
717 } else {
718 value = isParentGrid;
719 }
720 } else {
721 value = this.prefixes.options.grid;
722 }
723 }
724
725 node._autoprefixerGridStatus = value;
726 return value;
727 };
728
729 return Processor;
730}();
731
732module.exports = Processor;
\No newline at end of file