3 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
5 var Value = require('./value');
7 var OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i;
8 var OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i;
10 var 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'];
12 var Processor = function () {
13 function Processor(prefixes) {
14 _classCallCheck(this, Processor);
16 this.prefixes = prefixes;
20 * Add necessary prefixes
24 Processor.prototype.add = function add(css, result) {
28 var resolution = this.prefixes.add['@resolution'];
29 var keyframes = this.prefixes.add['@keyframes'];
30 var viewport = this.prefixes.add['@viewport'];
31 var supports = this.prefixes.add['@supports'];
33 css.walkAtRules(function (rule) {
34 if (rule.name === 'keyframes') {
35 if (!_this.disabled(rule, result)) {
36 return keyframes && keyframes.process(rule);
38 } else if (rule.name === 'viewport') {
39 if (!_this.disabled(rule, result)) {
40 return viewport && viewport.process(rule);
42 } else if (rule.name === 'supports') {
43 if (_this.prefixes.options.supports !== false && !_this.disabled(rule, result)) {
44 return supports.process(rule);
46 } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
47 if (!_this.disabled(rule, result)) {
48 return resolution && resolution.process(rule);
56 css.walkRules(function (rule) {
57 if (_this.disabled(rule, result)) return undefined;
59 return _this.prefixes.add.selectors.map(function (selector) {
60 return selector.process(rule, result);
64 css.walkDecls(function (decl) {
65 if (_this.disabledDecl(decl, result)) return undefined;
67 if (decl.prop === 'display' && decl.value === 'box') {
68 result.warn('You should write display: flex by final spec ' + 'instead of display: box', { node: decl });
71 if (decl.value.indexOf('linear-gradient') !== -1) {
72 if (OLD_LINEAR.test(decl.value)) {
73 result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `to left` instead of `right`.', { node: decl });
76 if (decl.value.indexOf('radial-gradient') !== -1) {
77 if (OLD_RADIAL.test(decl.value)) {
78 result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `closest-side at 0 0` ' + 'instead of `0 0, closest-side`.', { node: decl });
79 } else if (/[^-]cover/.test(decl.value)) {
80 result.warn('Gradient has outdated direction syntax. ' + 'Replace `cover` to `farthest-corner`.', { node: decl });
81 } else if (/[^-]contain/.test(decl.value)) {
82 result.warn('Gradient has outdated direction syntax. ' + 'Replace `contain` to `closest-side`.', { node: decl });
85 if (decl.prop === 'text-emphasis-position') {
86 if (decl.value === 'under' || decl.value === 'over') {
87 result.warn('You should use 2 values for text-emphasis-position ' + 'For example, `under left` instead of just `under`.', { node: decl });
91 if (SIZES.indexOf(decl.prop) !== -1) {
92 if (decl.value.indexOf('fill-available') !== -1) {
93 result.warn('Replace fill-available to stretch, ' + 'because spec had been changed', { node: decl });
94 } else if (decl.value.indexOf('fill') !== -1) {
95 result.warn('Replace fill to stretch, ' + 'because spec had been changed', { node: decl });
99 var prefixer = void 0;
101 if (decl.prop === 'transition' || decl.prop === 'transition-property') {
103 return _this.prefixes.transition.add(decl, result);
104 } else if (decl.prop === 'align-self') {
105 // align-self flexbox or grid
106 var display = _this.displayType(decl);
107 if (display !== 'grid' && _this.prefixes.options.flexbox !== false) {
108 prefixer = _this.prefixes.add['align-self'];
109 if (prefixer && prefixer.prefixes) {
110 prefixer.process(decl);
113 if (display !== 'flex' && _this.prefixes.options.grid !== false) {
114 prefixer = _this.prefixes.add['grid-row-align'];
115 if (prefixer && prefixer.prefixes) {
116 return prefixer.process(decl, result);
119 } else if (decl.prop === 'justify-self') {
120 // justify-self flexbox or grid
121 var _display = _this.displayType(decl);
122 if (_display !== 'flex' && _this.prefixes.options.grid !== false) {
123 prefixer = _this.prefixes.add['grid-column-align'];
124 if (prefixer && prefixer.prefixes) {
125 return prefixer.process(decl, result);
130 prefixer = _this.prefixes.add[decl.prop];
131 if (prefixer && prefixer.prefixes) {
132 return prefixer.process(decl, result);
140 return css.walkDecls(function (decl) {
141 if (_this.disabledValue(decl, result)) return;
143 var unprefixed = _this.prefixes.unprefixed(decl.prop);
144 for (var _iterator = _this.prefixes.values('add', unprefixed), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
148 if (_i >= _iterator.length) break;
149 _ref = _iterator[_i++];
151 _i = _iterator.next();
158 value.process(decl, result);
160 Value.save(_this.prefixes, decl);
165 * Remove unnecessary pefixes
169 Processor.prototype.remove = function remove(css, result) {
173 var resolution = this.prefixes.remove['@resolution'];
175 css.walkAtRules(function (rule, i) {
176 if (_this2.prefixes.remove['@' + rule.name]) {
177 if (!_this2.disabled(rule, result)) {
178 rule.parent.removeChild(i);
180 } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1 && resolution) {
181 resolution.clean(rule);
187 var _loop = function _loop(checker) {
188 css.walkRules(function (rule, i) {
189 if (checker.check(rule)) {
190 if (!_this2.disabled(rule, result)) {
191 rule.parent.removeChild(i);
197 for (var _iterator2 = this.prefixes.remove.selectors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
201 if (_i2 >= _iterator2.length) break;
202 _ref2 = _iterator2[_i2++];
204 _i2 = _iterator2.next();
214 return css.walkDecls(function (decl, i) {
215 if (_this2.disabled(decl, result)) return;
217 var rule = decl.parent;
218 var unprefixed = _this2.prefixes.unprefixed(decl.prop);
221 if (decl.prop === 'transition' || decl.prop === 'transition-property') {
222 _this2.prefixes.transition.remove(decl);
226 if (_this2.prefixes.remove[decl.prop] && _this2.prefixes.remove[decl.prop].remove) {
227 var notHack = _this2.prefixes.group(decl).down(function (other) {
228 return _this2.prefixes.normalize(other.prop) === unprefixed;
231 if (unprefixed === 'flex-flow') {
235 if (notHack && !_this2.withHackValue(decl)) {
236 if (decl.raw('before').indexOf('\n') > -1) {
237 _this2.reduceSpaces(decl);
245 for (var _iterator3 = _this2.prefixes.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
249 if (_i3 >= _iterator3.length) break;
250 _ref3 = _iterator3[_i3++];
252 _i3 = _iterator3.next();
259 if (!checker.check(decl.value)) {
263 unprefixed = checker.unprefixed;
264 var _notHack = _this2.prefixes.group(decl).down(function (other) {
265 return other.value.indexOf(unprefixed) !== -1;
277 * Some rare old values, which is not in standard
281 Processor.prototype.withHackValue = function withHackValue(decl) {
282 return decl.prop === '-webkit-background-clip' && decl.value === 'text';
286 * Check for grid/flexbox options.
290 Processor.prototype.disabledValue = function disabledValue(node, result) {
291 if (this.prefixes.options.grid === false && node.type === 'decl') {
292 if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
296 if (this.prefixes.options.flexbox === false && node.type === 'decl') {
297 if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
302 return this.disabled(node, result);
306 * Check for grid/flexbox options.
310 Processor.prototype.disabledDecl = function disabledDecl(node, result) {
311 if (this.prefixes.options.grid === false && node.type === 'decl') {
312 if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
316 if (this.prefixes.options.flexbox === false && node.type === 'decl') {
317 var other = ['order', 'justify-content', 'align-items', 'align-content'];
318 if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
323 return this.disabled(node, result);
327 * Check for control comment and global options
331 Processor.prototype.disabled = function disabled(node, result) {
332 if (!node) return false;
333 if (node._autoprefixerDisabled !== undefined) {
334 return node._autoprefixerDisabled;
338 var status = undefined;
339 node.each(function (i) {
340 if (i.type !== 'comment') return;
341 if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
342 if (typeof status !== 'undefined') {
343 result.warn('Second Autoprefixer control comment ' + 'was ignored. Autoprefixer applies control ' + 'comment to whole block, not to next rules.', { node: i });
345 status = /on/i.test(i.text);
351 if (status !== undefined) {
353 } else if (node.parent) {
354 value = this.disabled(node.parent, result);
357 node._autoprefixerDisabled = value;
358 return node._autoprefixerDisabled;
360 node._autoprefixerDisabled = this.disabled(node.parent, result);
361 return node._autoprefixerDisabled;
366 * Normalize spaces in cascade declaration group
370 Processor.prototype.reduceSpaces = function reduceSpaces(decl) {
372 this.prefixes.group(decl).up(function () {
380 var parts = decl.raw('before').split('\n');
381 var prevMin = parts[parts.length - 1].length;
384 this.prefixes.group(decl).down(function (other) {
385 parts = other.raw('before').split('\n');
386 var last = parts.length - 1;
388 if (parts[last].length > prevMin) {
389 if (diff === false) {
390 diff = parts[last].length - prevMin;
393 parts[last] = parts[last].slice(0, -diff);
394 other.raws.before = parts.join('\n');
400 * Is it flebox or grid rule
404 Processor.prototype.displayType = function displayType(decl) {
405 for (var _iterator4 = decl.parent.nodes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
409 if (_i4 >= _iterator4.length) break;
410 _ref4 = _iterator4[_i4++];
412 _i4 = _iterator4.next();
419 if (i.prop !== 'display') {
423 if (i.value.indexOf('flex') !== -1) {
427 if (i.value.indexOf('grid') !== -1) {
438 module.exports = Processor;