UNPKG

12.1 kBJavaScriptView Raw
1"use strict";
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5var parser = require('postcss-value-parser');
6
7var vendor = require('postcss').vendor;
8
9var list = require('postcss').list;
10
11var Transition =
12/*#__PURE__*/
13function () {
14 function Transition(prefixes) {
15 _defineProperty(this, "props", ['transition', 'transition-property']);
16
17 this.prefixes = prefixes;
18 }
19 /**
20 * Process transition and add prefixes for all necessary properties
21 */
22
23
24 var _proto = Transition.prototype;
25
26 _proto.add = function add(decl, result) {
27 var _this = this;
28
29 var prefix, prop;
30 var add = this.prefixes.add[decl.prop];
31 var declPrefixes = add && add.prefixes || [];
32 var params = this.parse(decl.value);
33 var names = params.map(function (i) {
34 return _this.findProp(i);
35 });
36 var added = [];
37
38 if (names.some(function (i) {
39 return i[0] === '-';
40 })) {
41 return;
42 }
43
44 for (var _iterator = params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
45 var _ref;
46
47 if (_isArray) {
48 if (_i >= _iterator.length) break;
49 _ref = _iterator[_i++];
50 } else {
51 _i = _iterator.next();
52 if (_i.done) break;
53 _ref = _i.value;
54 }
55
56 var param = _ref;
57 prop = this.findProp(param);
58 if (prop[0] === '-') continue;
59 var prefixer = this.prefixes.add[prop];
60 if (!prefixer || !prefixer.prefixes) continue;
61
62 for (var _iterator3 = prefixer.prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
63 if (_isArray3) {
64 if (_i3 >= _iterator3.length) break;
65 prefix = _iterator3[_i3++];
66 } else {
67 _i3 = _iterator3.next();
68 if (_i3.done) break;
69 prefix = _i3.value;
70 }
71
72 var prefixed = this.prefixes.prefixed(prop, prefix);
73
74 if (prefixed !== '-ms-transform' && !names.includes(prefixed)) {
75 if (!this.disabled(prop, prefix)) {
76 added.push(this.clone(prop, prefixed, param));
77 }
78 }
79 }
80 }
81
82 params = params.concat(added);
83 var value = this.stringify(params);
84 var webkitClean = this.stringify(this.cleanFromUnprefixed(params, '-webkit-'));
85
86 if (declPrefixes.includes('-webkit-')) {
87 this.cloneBefore(decl, "-webkit-" + decl.prop, webkitClean);
88 }
89
90 this.cloneBefore(decl, decl.prop, webkitClean);
91
92 if (declPrefixes.includes('-o-')) {
93 var operaClean = this.stringify(this.cleanFromUnprefixed(params, '-o-'));
94 this.cloneBefore(decl, "-o-" + decl.prop, operaClean);
95 }
96
97 for (var _iterator2 = declPrefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
98 if (_isArray2) {
99 if (_i2 >= _iterator2.length) break;
100 prefix = _iterator2[_i2++];
101 } else {
102 _i2 = _iterator2.next();
103 if (_i2.done) break;
104 prefix = _i2.value;
105 }
106
107 if (prefix !== '-webkit-' && prefix !== '-o-') {
108 var prefixValue = this.stringify(this.cleanOtherPrefixes(params, prefix));
109 this.cloneBefore(decl, prefix + decl.prop, prefixValue);
110 }
111 }
112
113 if (value !== decl.value && !this.already(decl, decl.prop, value)) {
114 this.checkForWarning(result, decl);
115 decl.cloneBefore();
116 decl.value = value;
117 }
118 }
119 /**
120 * Find property name
121 */
122 ;
123
124 _proto.findProp = function findProp(param) {
125 var prop = param[0].value;
126
127 if (/^\d/.test(prop)) {
128 for (var _iterator4 = param.entries(), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
129 var _ref2;
130
131 if (_isArray4) {
132 if (_i4 >= _iterator4.length) break;
133 _ref2 = _iterator4[_i4++];
134 } else {
135 _i4 = _iterator4.next();
136 if (_i4.done) break;
137 _ref2 = _i4.value;
138 }
139
140 var _ref3 = _ref2,
141 i = _ref3[0],
142 token = _ref3[1];
143
144 if (i !== 0 && token.type === 'word') {
145 return token.value;
146 }
147 }
148 }
149
150 return prop;
151 }
152 /**
153 * Does we already have this declaration
154 */
155 ;
156
157 _proto.already = function already(decl, prop, value) {
158 return decl.parent.some(function (i) {
159 return i.prop === prop && i.value === value;
160 });
161 }
162 /**
163 * Add declaration if it is not exist
164 */
165 ;
166
167 _proto.cloneBefore = function cloneBefore(decl, prop, value) {
168 if (!this.already(decl, prop, value)) {
169 decl.cloneBefore({
170 prop: prop,
171 value: value
172 });
173 }
174 }
175 /**
176 * Show transition-property warning
177 */
178 ;
179
180 _proto.checkForWarning = function checkForWarning(result, decl) {
181 if (decl.prop !== 'transition-property') {
182 return;
183 }
184
185 decl.parent.each(function (i) {
186 if (i.type !== 'decl') {
187 return undefined;
188 }
189
190 if (i.prop.indexOf('transition-') !== 0) {
191 return undefined;
192 }
193
194 if (i.prop === 'transition-property') {
195 return undefined;
196 }
197
198 if (list.comma(i.value).length > 1) {
199 decl.warn(result, 'Replace transition-property to transition, ' + 'because Autoprefixer could not support ' + 'any cases of transition-property ' + 'and other transition-*');
200 }
201
202 return false;
203 });
204 }
205 /**
206 * Process transition and remove all unnecessary properties
207 */
208 ;
209
210 _proto.remove = function remove(decl) {
211 var _this2 = this;
212
213 var params = this.parse(decl.value);
214 params = params.filter(function (i) {
215 var prop = _this2.prefixes.remove[_this2.findProp(i)];
216
217 return !prop || !prop.remove;
218 });
219 var value = this.stringify(params);
220
221 if (decl.value === value) {
222 return;
223 }
224
225 if (params.length === 0) {
226 decl.remove();
227 return;
228 }
229
230 var _double = decl.parent.some(function (i) {
231 return i.prop === decl.prop && i.value === value;
232 });
233
234 var smaller = decl.parent.some(function (i) {
235 return i !== decl && i.prop === decl.prop && i.value.length > value.length;
236 });
237
238 if (_double || smaller) {
239 decl.remove();
240 return;
241 }
242
243 decl.value = value;
244 }
245 /**
246 * Parse properties list to array
247 */
248 ;
249
250 _proto.parse = function parse(value) {
251 var ast = parser(value);
252 var result = [];
253 var param = [];
254
255 for (var _iterator5 = ast.nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
256 var _ref4;
257
258 if (_isArray5) {
259 if (_i5 >= _iterator5.length) break;
260 _ref4 = _iterator5[_i5++];
261 } else {
262 _i5 = _iterator5.next();
263 if (_i5.done) break;
264 _ref4 = _i5.value;
265 }
266
267 var node = _ref4;
268 param.push(node);
269
270 if (node.type === 'div' && node.value === ',') {
271 result.push(param);
272 param = [];
273 }
274 }
275
276 result.push(param);
277 return result.filter(function (i) {
278 return i.length > 0;
279 });
280 }
281 /**
282 * Return properties string from array
283 */
284 ;
285
286 _proto.stringify = function stringify(params) {
287 if (params.length === 0) {
288 return '';
289 }
290
291 var nodes = [];
292
293 for (var _iterator6 = params, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
294 var _ref5;
295
296 if (_isArray6) {
297 if (_i6 >= _iterator6.length) break;
298 _ref5 = _iterator6[_i6++];
299 } else {
300 _i6 = _iterator6.next();
301 if (_i6.done) break;
302 _ref5 = _i6.value;
303 }
304
305 var param = _ref5;
306
307 if (param[param.length - 1].type !== 'div') {
308 param.push(this.div(params));
309 }
310
311 nodes = nodes.concat(param);
312 }
313
314 if (nodes[0].type === 'div') {
315 nodes = nodes.slice(1);
316 }
317
318 if (nodes[nodes.length - 1].type === 'div') {
319 nodes = nodes.slice(0, +-2 + 1 || undefined);
320 }
321
322 return parser.stringify({
323 nodes: nodes
324 });
325 }
326 /**
327 * Return new param array with different name
328 */
329 ;
330
331 _proto.clone = function clone(origin, name, param) {
332 var result = [];
333 var changed = false;
334
335 for (var _iterator7 = param, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
336 var _ref6;
337
338 if (_isArray7) {
339 if (_i7 >= _iterator7.length) break;
340 _ref6 = _iterator7[_i7++];
341 } else {
342 _i7 = _iterator7.next();
343 if (_i7.done) break;
344 _ref6 = _i7.value;
345 }
346
347 var i = _ref6;
348
349 if (!changed && i.type === 'word' && i.value === origin) {
350 result.push({
351 type: 'word',
352 value: name
353 });
354 changed = true;
355 } else {
356 result.push(i);
357 }
358 }
359
360 return result;
361 }
362 /**
363 * Find or create separator
364 */
365 ;
366
367 _proto.div = function div(params) {
368 for (var _iterator8 = params, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
369 var _ref7;
370
371 if (_isArray8) {
372 if (_i8 >= _iterator8.length) break;
373 _ref7 = _iterator8[_i8++];
374 } else {
375 _i8 = _iterator8.next();
376 if (_i8.done) break;
377 _ref7 = _i8.value;
378 }
379
380 var param = _ref7;
381
382 for (var _iterator9 = param, _isArray9 = Array.isArray(_iterator9), _i9 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {
383 var _ref8;
384
385 if (_isArray9) {
386 if (_i9 >= _iterator9.length) break;
387 _ref8 = _iterator9[_i9++];
388 } else {
389 _i9 = _iterator9.next();
390 if (_i9.done) break;
391 _ref8 = _i9.value;
392 }
393
394 var node = _ref8;
395
396 if (node.type === 'div' && node.value === ',') {
397 return node;
398 }
399 }
400 }
401
402 return {
403 type: 'div',
404 value: ',',
405 after: ' '
406 };
407 };
408
409 _proto.cleanOtherPrefixes = function cleanOtherPrefixes(params, prefix) {
410 var _this3 = this;
411
412 return params.filter(function (param) {
413 var current = vendor.prefix(_this3.findProp(param));
414 return current === '' || current === prefix;
415 });
416 }
417 /**
418 * Remove all non-webkit prefixes and unprefixed params if we have prefixed
419 */
420 ;
421
422 _proto.cleanFromUnprefixed = function cleanFromUnprefixed(params, prefix) {
423 var _this4 = this;
424
425 var remove = params.map(function (i) {
426 return _this4.findProp(i);
427 }).filter(function (i) {
428 return i.slice(0, prefix.length) === prefix;
429 }).map(function (i) {
430 return _this4.prefixes.unprefixed(i);
431 });
432 var result = [];
433
434 for (var _iterator10 = params, _isArray10 = Array.isArray(_iterator10), _i10 = 0, _iterator10 = _isArray10 ? _iterator10 : _iterator10[Symbol.iterator]();;) {
435 var _ref9;
436
437 if (_isArray10) {
438 if (_i10 >= _iterator10.length) break;
439 _ref9 = _iterator10[_i10++];
440 } else {
441 _i10 = _iterator10.next();
442 if (_i10.done) break;
443 _ref9 = _i10.value;
444 }
445
446 var param = _ref9;
447 var prop = this.findProp(param);
448 var p = vendor.prefix(prop);
449
450 if (!remove.includes(prop) && (p === prefix || p === '')) {
451 result.push(param);
452 }
453 }
454
455 return result;
456 }
457 /**
458 * Check property for disabled by option
459 */
460 ;
461
462 _proto.disabled = function disabled(prop, prefix) {
463 var other = ['order', 'justify-content', 'align-self', 'align-content'];
464
465 if (prop.includes('flex') || other.includes(prop)) {
466 if (this.prefixes.options.flexbox === false) {
467 return true;
468 }
469
470 if (this.prefixes.options.flexbox === 'no-2009') {
471 return prefix.includes('2009');
472 }
473 }
474
475 return undefined;
476 };
477
478 return Transition;
479}();
480
481module.exports = Transition;
\No newline at end of file