4 const isNumbery = require("./isNumbery");
5 const isStandardSyntaxValue = require("./isStandardSyntaxValue");
6 const isValidFontSize = require("./isValidFontSize");
7 const isVariable = require("./isVariable");
8 const keywordSets = require("../reference/keywordSets");
9 const postcssValueParser = require("postcss-value-parser");
11 const nodeTypesToCheck = new Set(["word", "string", "space", "div"]);
13 function joinValueNodes(firstNode, secondNode, charactersBetween) {
14 firstNode.value = firstNode.value + charactersBetween + secondNode.value;
20 * Get the font-families within a `font` shorthand property value.
22 * @param {string} value
23 * @return {object} Collection font-family nodes
25 module.exports = function findFontFamily(
27 ) /*: Array<Object>*/ {
28 const fontFamilies = [];
30 const valueNodes = postcssValueParser(value);
32 // Handle `inherit`, `initial` and etc
34 valueNodes.nodes.length === 1 &&
35 keywordSets.basicKeywords.has(valueNodes.nodes[0].value.toLowerCase())
37 return [valueNodes.nodes[0]];
40 let needMergeNodesByValue = false;
41 let mergeCharacters = null;
43 valueNodes.walk((valueNode, index, nodes) => {
44 if (valueNode.type === "function") {
47 if (!nodeTypesToCheck.has(valueNode.type)) {
51 const valueLowerCase = valueNode.value.toLowerCase();
53 // Ignore non standard syntax
54 if (!isStandardSyntaxValue(valueLowerCase)) {
59 if (isVariable(valueLowerCase)) {
63 // Ignore keywords for other font parts
65 keywordSets.fontShorthandKeywords.has(valueLowerCase) &&
66 !keywordSets.fontFamilyKeywords.has(valueLowerCase)
72 if (isValidFontSize(valueNode.value)) {
76 // Ignore anything come after a <font-size>/, because it's a line-height
79 nodes[index - 1].value === "/" &&
81 isValidFontSize(nodes[index - 2].value)
86 // Ignore number values
87 if (isNumbery(valueLowerCase)) {
91 // Detect when a space or comma is dividing a list of font-families, and save the joining character.
93 (valueNode.type === "space" ||
94 (valueNode.type === "div" && valueNode.value !== ",")) &&
95 fontFamilies.length !== 0
97 needMergeNodesByValue = true;
98 mergeCharacters = valueNode.value;
100 } else if (valueNode.type === "space" || valueNode.type === "div") {
104 const fontFamily = valueNode;
106 if (needMergeNodesByValue) {
108 fontFamilies[fontFamilies.length - 1],
112 needMergeNodesByValue = false;
113 mergeCharacters = null;
115 fontFamilies.push(fontFamily);