(cssPropertyValue: string, cssProperty?: string)
| 129 | * @return {object} parsed css property |
| 130 | */ |
| 131 | export function parseCSS(cssPropertyValue: string, cssProperty?: string) { |
| 132 | const parsedValue: ParsedCSSValue = { |
| 133 | property: cssProperty, |
| 134 | value: cssPropertyValue.toLowerCase().trim(), |
| 135 | parsed: {} |
| 136 | } |
| 137 | |
| 138 | if (parsedValue.value?.indexOf('rgb') === 0) { |
| 139 | /** |
| 140 | * remove whitespace in rgb values |
| 141 | */ |
| 142 | parsedValue.value = parsedValue.value.replace(/\s/g, '') |
| 143 | |
| 144 | /** |
| 145 | * parse color values |
| 146 | */ |
| 147 | const color = parsedValue.value |
| 148 | parsedValue.parsed = rgb2hex(parsedValue.value) |
| 149 | parsedValue.parsed.type = 'color' |
| 150 | |
| 151 | const colorType = /[rgba]+/g.exec(color) || [] |
| 152 | parsedValue.parsed[colorType[0] as 'rgb' | 'rgba'] = color |
| 153 | } else if (parsedValue.property === 'font-family') { |
| 154 | const font = cssValue(cssPropertyValue) |
| 155 | const string = parsedValue.value |
| 156 | const value = cssPropertyValue.split(/,/).map(sanitizeCSS) |
| 157 | |
| 158 | parsedValue.value = sanitizeCSS(font[0].value as string || font[0].string) |
| 159 | parsedValue.parsed = { value, type: 'font', string } |
| 160 | } else { |
| 161 | /** |
| 162 | * parse other css properties |
| 163 | */ |
| 164 | try { |
| 165 | const value = cssValue(cssPropertyValue) |
| 166 | |
| 167 | if (value.length === 1) { |
| 168 | parsedValue.parsed = value[0] |
| 169 | } |
| 170 | |
| 171 | if (parsedValue.parsed.type && parsedValue.parsed.type === 'number' && parsedValue.parsed.unit === '') { |
| 172 | parsedValue.value = parsedValue.parsed.value |
| 173 | } |
| 174 | } catch { |
| 175 | // TODO improve css-parse lib to handle properties like |
| 176 | // `-webkit-animation-timing-function : cubic-bezier(0.25, 0.1, 0.25, 1) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return parsedValue |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * check for unicode character or split string into literals |
no test coverage detected
searching dependent graphs…