findAnimationName.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. const getDimension = require('./getDimension');
  3. const isStandardSyntaxValue = require('./isStandardSyntaxValue');
  4. const isVariable = require('./isVariable');
  5. const { animationShorthandKeywords, basicKeywords } = require('../reference/keywords');
  6. const postcssValueParser = require('postcss-value-parser');
  7. /** @typedef {import('postcss-value-parser').Node} Node */
  8. /**
  9. * Get the animation name within an `animation` shorthand property value.
  10. *
  11. * @param {string} value
  12. *
  13. * @returns {Node[]}
  14. */
  15. module.exports = function findAnimationName(value) {
  16. /** @type {Node[]} */
  17. const animationNames = [];
  18. const valueNodes = postcssValueParser(value);
  19. const { nodes } = valueNodes;
  20. // Handle `inherit`, `initial` and etc
  21. if (nodes.length === 1 && nodes[0] && basicKeywords.has(nodes[0].value.toLowerCase())) {
  22. return [nodes[0]];
  23. }
  24. let shouldBeIgnored = false;
  25. valueNodes.walk((valueNode) => {
  26. if (shouldBeIgnored) return;
  27. if (valueNode.type === 'function') {
  28. return false;
  29. }
  30. if (valueNode.type !== 'word') {
  31. return;
  32. }
  33. const valueLowerCase = valueNode.value.toLowerCase();
  34. // Ignore non-standard syntax
  35. if (!isStandardSyntaxValue(valueLowerCase)) {
  36. // Cannot find animation name if shorthand has non-standard syntax value (#5532)
  37. shouldBeIgnored = true;
  38. animationNames.length = 0; // clears animationNames
  39. return;
  40. }
  41. // Ignore variables
  42. if (isVariable(valueLowerCase)) {
  43. return;
  44. }
  45. // Ignore keywords for other animation parts
  46. if (animationShorthandKeywords.has(valueLowerCase)) {
  47. return;
  48. }
  49. // Ignore numbers with units
  50. const { unit } = getDimension(valueNode);
  51. if (unit || unit === '') {
  52. return;
  53. }
  54. animationNames.push(valueNode);
  55. });
  56. return animationNames;
  57. };