|
| 1 | +import {METHOD} from './constant'; |
| 2 | + |
| 3 | +import parseParenthesisContent from './parse-parenthesis-content'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {string} mapString SASS map string without opening parenthesis |
| 7 | + * @param {string} keyParameter the key to extract from the map |
| 8 | + * @returns {?string} retrieved `keyParameter` value from the map |
| 9 | + */ |
| 10 | +function getKeyFromMapString(mapString, keyParameter) { |
| 11 | + // remove all whitespace character from the key |
| 12 | + const keyValue = keyParameter.replace(/\s/g, ''); |
| 13 | + // remove open and close parenthesis from the map string |
| 14 | + mapString = mapString.slice(1, -1); |
| 15 | + |
| 16 | + const map = {}; |
| 17 | + |
| 18 | + let isParsingKey = true; |
| 19 | + |
| 20 | + let key = ''; |
| 21 | + let value = ''; |
| 22 | + |
| 23 | + for (let position = 0; position < mapString.length; position++) { |
| 24 | + const currentCharacter = mapString[position]; |
| 25 | + |
| 26 | + if (isParsingKey) { // process the key (add all characters until find a :) |
| 27 | + if (currentCharacter === ':') { |
| 28 | + isParsingKey = false; |
| 29 | + } else { |
| 30 | + key += currentCharacter; |
| 31 | + } |
| 32 | + } else if (currentCharacter === '(') { |
| 33 | + // if value contains a `(` that means that is map so parse the string until the `(` is closed |
| 34 | + const output = parseParenthesisContent(mapString, position); |
| 35 | + value += output.content; |
| 36 | + position = output.position; |
| 37 | + |
| 38 | + map[key] = value.trim(); |
| 39 | + |
| 40 | + // value declaration is complete return to check key and reset both variables |
| 41 | + isParsingKey = true; |
| 42 | + key = ''; |
| 43 | + value = ''; |
| 44 | + } else { |
| 45 | + // simple map with property / value pairs |
| 46 | + const isLastCharacter = position === mapString.length - 1; |
| 47 | + if (currentCharacter === ',' || isLastCharacter) { // map with only one property or end of the string |
| 48 | + if (isLastCharacter) { |
| 49 | + value += currentCharacter; |
| 50 | + } |
| 51 | + |
| 52 | + map[key] = value.trim(); |
| 53 | + |
| 54 | + isParsingKey = true; |
| 55 | + key = ''; |
| 56 | + value = ''; |
| 57 | + } else { |
| 58 | + value += currentCharacter; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return map[keyValue]; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @param {string} value CSS property value including map-get invocation |
| 68 | + * @returns {string} value of css property resolved by map-get |
| 69 | + */ |
| 70 | +export default function (value) { |
| 71 | + let resolvedValue = value; |
| 72 | + // start to resolve map-get more nested |
| 73 | + let indexOfMethod = resolvedValue.indexOf(METHOD); |
| 74 | + while (indexOfMethod > -1) { |
| 75 | + const startPosition = indexOfMethod; |
| 76 | + let position = (startPosition + METHOD.length) - 1; |
| 77 | + |
| 78 | + let mapString = ''; |
| 79 | + |
| 80 | + // resolve map content |
| 81 | + const output = parseParenthesisContent(resolvedValue, position); |
| 82 | + mapString += output.content; |
| 83 | + position = output.position; |
| 84 | + |
| 85 | + // resolve the desidered requested key |
| 86 | + let keyString = ''; |
| 87 | + let hasFoundComa = false; |
| 88 | + |
| 89 | + for (; position < resolvedValue.length; position++) { |
| 90 | + const currentCharacter = resolvedValue[position]; |
| 91 | + |
| 92 | + if (currentCharacter === ',') { |
| 93 | + hasFoundComa = true; |
| 94 | + } else if (currentCharacter === ')') { |
| 95 | + break; |
| 96 | + } else if (hasFoundComa) { |
| 97 | + keyString += currentCharacter; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // get the original invocation string |
| 102 | + position++; // Include last closing parenthesis |
| 103 | + const currentDeclaration = resolvedValue.slice(startPosition, position); |
| 104 | + |
| 105 | + const mapResolvedValue = getKeyFromMapString(mapString, keyString); |
| 106 | + |
| 107 | + // replace the value string with the resolved value |
| 108 | + resolvedValue = resolvedValue.replace(currentDeclaration, mapResolvedValue); |
| 109 | + |
| 110 | + // check if property value contains another map-get invocation |
| 111 | + indexOfMethod = resolvedValue.indexOf(METHOD); |
| 112 | + } |
| 113 | + |
| 114 | + return resolvedValue; |
| 115 | +} |
0 commit comments