UNPKG

1.02 kBJavaScriptView Raw
1import { length, percentage } from './dataTypes'
2import { splitAtTopLevelOnly } from './splitAtTopLevelOnly'
3
4/**
5 *
6 * https://developer.mozilla.org/en-US/docs/Web/CSS/background-size#formal_syntax
7 *
8 * background-size =
9 * <bg-size>#
10 *
11 * <bg-size> =
12 * [ <length-percentage [0,∞]> | auto ]{1,2} |
13 * cover |
14 * contain
15 *
16 * <length-percentage> =
17 * <length> |
18 * <percentage>
19 *
20 * @param {string} value
21 */
22export function backgroundSize(value) {
23 let keywordValues = ['cover', 'contain']
24 // the <length-percentage> type will probably be a css function
25 // so we have to use `splitAtTopLevelOnly`
26 return splitAtTopLevelOnly(value, ',').every((part) => {
27 let sizes = splitAtTopLevelOnly(part, '_').filter(Boolean)
28 if (sizes.length === 1 && keywordValues.includes(sizes[0])) return true
29
30 if (sizes.length !== 1 && sizes.length !== 2) return false
31
32 return sizes.every((size) => length(size) || percentage(size) || size === 'auto')
33 })
34}