UNPKG

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