UNPKG

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