findAnimationName.cjs 1.9 KB

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