You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = normalizeAnimation;
  6. var _postcssValueParser = require('postcss-value-parser');
  7. var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
  8. var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
  9. var _addSpace = require('../lib/addSpace');
  10. var _addSpace2 = _interopRequireDefault(_addSpace);
  11. var _getValue = require('../lib/getValue');
  12. var _getValue2 = _interopRequireDefault(_getValue);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. // animation: [ none | <keyframes-name> ] || <time> || <single-timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state>
  15. const isTimingFunction = (value, type) => {
  16. const functions = ['steps', 'cubic-bezier', 'frames'];
  17. const keywords = ['ease', 'ease-in', 'ease-in-out', 'ease-out', 'linear', 'step-end', 'step-start'];
  18. return type === 'function' && functions.includes(value) || keywords.includes(value);
  19. };
  20. const isDirection = value => {
  21. return ['normal', 'reverse', 'alternate', 'alternate-reverse'].includes(value);
  22. };
  23. const isFillMode = value => {
  24. return ['none', 'forwards', 'backwards', 'both'].includes(value);
  25. };
  26. const isPlayState = value => {
  27. return ['running', 'paused'].includes(value);
  28. };
  29. const isTime = value => {
  30. const quantity = (0, _postcssValueParser.unit)(value);
  31. return quantity && ['ms', 's'].includes(quantity.unit);
  32. };
  33. const isIterationCount = value => {
  34. const quantity = (0, _postcssValueParser.unit)(value);
  35. return value === 'infinite' || quantity && !quantity.unit;
  36. };
  37. function normalizeAnimation(parsed) {
  38. const args = (0, _cssnanoUtilGetArguments2.default)(parsed);
  39. const values = args.reduce((list, arg) => {
  40. const state = {
  41. name: [],
  42. duration: [],
  43. timingFunction: [],
  44. delay: [],
  45. iterationCount: [],
  46. direction: [],
  47. fillMode: [],
  48. playState: []
  49. };
  50. const stateConditions = [{ property: 'duration', delegate: isTime }, { property: 'timingFunction', delegate: isTimingFunction }, { property: 'delay', delegate: isTime }, { property: 'iterationCount', delegate: isIterationCount }, { property: 'direction', delegate: isDirection }, { property: 'fillMode', delegate: isFillMode }, { property: 'playState', delegate: isPlayState }];
  51. arg.forEach(node => {
  52. let { type, value } = node;
  53. if (type === 'space') {
  54. return;
  55. }
  56. value = value.toLowerCase();
  57. const hasMatch = stateConditions.some(({ property, delegate }) => {
  58. if (delegate(value, type) && !state[property].length) {
  59. state[property] = [node, (0, _addSpace2.default)()];
  60. return true;
  61. }
  62. });
  63. if (!hasMatch) {
  64. state.name = [...state.name, node, (0, _addSpace2.default)()];
  65. }
  66. });
  67. return [...list, [...state.name, ...state.duration, ...state.timingFunction, ...state.delay, ...state.iterationCount, ...state.direction, ...state.fillMode, ...state.playState]];
  68. }, []);
  69. return (0, _getValue2.default)(values);
  70. };
  71. module.exports = exports['default'];