transition.js
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const { unit } = require('postcss-value-parser');
const { getArguments } = require('cssnano-utils');
const addSpace = require('../lib/addSpace');
const getValue = require('../lib/getValue');
// transition: [ none | <single-transition-property> ] || <time> || <single-transition-timing-function> || <time>
const timingFunctions = new Set([
'ease',
'linear',
'ease-in',
'ease-out',
'ease-in-out',
'step-start',
'step-end',
]);
/**
* @param {import('postcss-value-parser').Node[][]} args
* @return {import('postcss-value-parser').Node[][]}
*/
function normalize(args) {
const list = [];
for (const arg of args) {
/** @type {Record<string, import('postcss-value-parser').Node[]>} */
let state = {
timingFunction: [],
property: [],
time1: [],
time2: [],
};
arg.forEach((node) => {
const { type, value } = node;
if (type === 'space') {
return;
}
if (
type === 'function' &&
new Set(['steps', 'cubic-bezier']).has(value.toLowerCase())
) {
state.timingFunction = [...state.timingFunction, node, addSpace()];
} else if (unit(value)) {
if (!state.time1.length) {
state.time1 = [...state.time1, node, addSpace()];
} else {
state.time2 = [...state.time2, node, addSpace()];
}
} else if (timingFunctions.has(value.toLowerCase())) {
state.timingFunction = [...state.timingFunction, node, addSpace()];
} else {
state.property = [...state.property, node, addSpace()];
}
});
list.push([
...state.property,
...state.time1,
...state.timingFunction,
...state.time2,
]);
}
return list;
}
/**
* @param {import('postcss-value-parser').ParsedValue} parsed
* @return {string}
*/
module.exports = function normalizeTransition(parsed) {
const values = normalize(getArguments(parsed));
return getValue(values);
};