1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = normalizeAnimation;
|
---|
7 |
|
---|
8 | var _postcssValueParser = require("postcss-value-parser");
|
---|
9 |
|
---|
10 | var _cssnanoUtils = require("cssnano-utils");
|
---|
11 |
|
---|
12 | var _addSpace = _interopRequireDefault(require("../lib/addSpace"));
|
---|
13 |
|
---|
14 | var _getValue = _interopRequireDefault(require("../lib/getValue"));
|
---|
15 |
|
---|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
17 |
|
---|
18 | // animation: [ none | <keyframes-name> ] || <time> || <single-timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state>
|
---|
19 | const isTimingFunction = (value, type) => {
|
---|
20 | const functions = ['steps', 'cubic-bezier', 'frames'];
|
---|
21 | const keywords = ['ease', 'ease-in', 'ease-in-out', 'ease-out', 'linear', 'step-end', 'step-start'];
|
---|
22 | return type === 'function' && functions.includes(value) || keywords.includes(value);
|
---|
23 | };
|
---|
24 |
|
---|
25 | const isDirection = value => {
|
---|
26 | return ['normal', 'reverse', 'alternate', 'alternate-reverse'].includes(value);
|
---|
27 | };
|
---|
28 |
|
---|
29 | const isFillMode = value => {
|
---|
30 | return ['none', 'forwards', 'backwards', 'both'].includes(value);
|
---|
31 | };
|
---|
32 |
|
---|
33 | const isPlayState = value => {
|
---|
34 | return ['running', 'paused'].includes(value);
|
---|
35 | };
|
---|
36 |
|
---|
37 | const isTime = value => {
|
---|
38 | const quantity = (0, _postcssValueParser.unit)(value);
|
---|
39 | return quantity && ['ms', 's'].includes(quantity.unit);
|
---|
40 | };
|
---|
41 |
|
---|
42 | const isIterationCount = value => {
|
---|
43 | const quantity = (0, _postcssValueParser.unit)(value);
|
---|
44 | return value === 'infinite' || quantity && !quantity.unit;
|
---|
45 | };
|
---|
46 |
|
---|
47 | function normalizeAnimation(parsed) {
|
---|
48 | const args = (0, _cssnanoUtils.getArguments)(parsed);
|
---|
49 | const values = args.reduce((list, arg) => {
|
---|
50 | const state = {
|
---|
51 | name: [],
|
---|
52 | duration: [],
|
---|
53 | timingFunction: [],
|
---|
54 | delay: [],
|
---|
55 | iterationCount: [],
|
---|
56 | direction: [],
|
---|
57 | fillMode: [],
|
---|
58 | playState: []
|
---|
59 | };
|
---|
60 | const stateConditions = [{
|
---|
61 | property: 'duration',
|
---|
62 | delegate: isTime
|
---|
63 | }, {
|
---|
64 | property: 'timingFunction',
|
---|
65 | delegate: isTimingFunction
|
---|
66 | }, {
|
---|
67 | property: 'delay',
|
---|
68 | delegate: isTime
|
---|
69 | }, {
|
---|
70 | property: 'iterationCount',
|
---|
71 | delegate: isIterationCount
|
---|
72 | }, {
|
---|
73 | property: 'direction',
|
---|
74 | delegate: isDirection
|
---|
75 | }, {
|
---|
76 | property: 'fillMode',
|
---|
77 | delegate: isFillMode
|
---|
78 | }, {
|
---|
79 | property: 'playState',
|
---|
80 | delegate: isPlayState
|
---|
81 | }];
|
---|
82 | arg.forEach(node => {
|
---|
83 | let {
|
---|
84 | type,
|
---|
85 | value
|
---|
86 | } = node;
|
---|
87 |
|
---|
88 | if (type === 'space') {
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | value = value.toLowerCase();
|
---|
93 | const hasMatch = stateConditions.some(({
|
---|
94 | property,
|
---|
95 | delegate
|
---|
96 | }) => {
|
---|
97 | if (delegate(value, type) && !state[property].length) {
|
---|
98 | state[property] = [node, (0, _addSpace.default)()];
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | });
|
---|
102 |
|
---|
103 | if (!hasMatch) {
|
---|
104 | state.name = [...state.name, node, (0, _addSpace.default)()];
|
---|
105 | }
|
---|
106 | });
|
---|
107 | return [...list, [...state.name, ...state.duration, ...state.timingFunction, ...state.delay, ...state.iterationCount, ...state.direction, ...state.fillMode, ...state.playState]];
|
---|
108 | }, []);
|
---|
109 | return (0, _getValue.default)(values);
|
---|
110 | }
|
---|
111 |
|
---|
112 | module.exports = exports.default; |
---|