|
| 1 | +/** |
| 2 | + * @see {@link https://drafts.css-houdini.org/css-typed-om/#cssnumericvalue-match} |
| 3 | + */ |
| 4 | +const matchesSingleType = ( |
| 5 | + value: CSSNumericValue, |
| 6 | + productionKey: keyof CSSNumericType, |
| 7 | + expected: number = 1, |
| 8 | +): boolean => { |
| 9 | + const type = value.type() |
| 10 | + for (const key in type) { |
| 11 | + const typedKey = key as keyof CSSNumericType |
| 12 | + if (typedKey === productionKey) { |
| 13 | + if (type[typedKey] !== expected) return false |
| 14 | + } else { |
| 15 | + if (type[typedKey] !== 0) return false |
| 16 | + } |
| 17 | + } |
| 18 | + return true |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A CSSNumericValue matches for `<number>` |
| 23 | + * when all entries of `CSSNumericType` are non-zero |
| 24 | + */ |
| 25 | +export const matchesNumber = (value: CSSNumericValue): boolean => { |
| 26 | + const type = value.type() |
| 27 | + return type.length !== 0 && |
| 28 | + type.angle !== 0 && |
| 29 | + type.time !== 0 && |
| 30 | + type.frequency !== 0 && |
| 31 | + type.resolution !== 0 && |
| 32 | + type.flex !== 0 && |
| 33 | + type.percent !== 0 |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A CSSNumericValue matches for `<length>` |
| 38 | + * when the only non-zero entry of `CSSNumericType` is length of 1 |
| 39 | + * |
| 40 | + * @see {@link https://drafts.csswg.org/css-values-4/#length-value} |
| 41 | + */ |
| 42 | +export const matchesLength = (value: CSSNumericValue): boolean => |
| 43 | + matchesSingleType(value, 'length', 1) |
| 44 | + |
| 45 | +/** |
| 46 | + * A CSSNumericValue matches for `<angle>` |
| 47 | + * when the only non-zero entry of `CSSNumericType` is angle of 1 |
| 48 | + * |
| 49 | + * @see {@link https://drafts.csswg.org/css-values-4/#angle-value} |
| 50 | + */ |
| 51 | +export const matchesAngle = (value: CSSNumericValue): boolean => |
| 52 | + matchesSingleType(value, 'angle', 1) |
| 53 | + |
| 54 | +/** |
| 55 | + * A CSSNumericValue matches for `<time>` |
| 56 | + * when the only non-zero entry of `CSSNumericType` is time of 1 |
| 57 | + * |
| 58 | + * @see {@link https://drafts.csswg.org/css-values-4/#time-value} |
| 59 | + */ |
| 60 | +export const matchesTime = (value: CSSNumericValue): boolean => |
| 61 | + matchesSingleType(value, 'time', 1) |
| 62 | + |
| 63 | +/** |
| 64 | + * A CSSNumericValue matches for `<frequency>` |
| 65 | + * when the only non-zero entry of `CSSNumericType` is frequency of 1 |
| 66 | + * |
| 67 | + * @see {@link https://drafts.csswg.org/css-values-4/#frequency-value} |
| 68 | + */ |
| 69 | +export const matchesFrequency = (value: CSSNumericValue): boolean => |
| 70 | + matchesSingleType(value, 'frequency', 1) |
| 71 | + |
| 72 | +/** |
| 73 | + * A CSSNumericValue matches for `<resolution>` |
| 74 | + * when the only non-zero entry of `CSSNumericType` is resolution of 1 |
| 75 | + * |
| 76 | + * @see {@link https://drafts.csswg.org/css-values-4/#resolution-value} |
| 77 | + */ |
| 78 | +export const matchesResolution = (value: CSSNumericValue): boolean => |
| 79 | + matchesSingleType(value, 'resolution', 1) |
| 80 | + |
| 81 | +/** |
| 82 | + * A CSSNumericValue matches for `<flex>` |
| 83 | + * when the only non-zero entry of `CSSNumericType` is flex of 1 |
| 84 | + * |
| 85 | + * @see {@link https://drafts.csswg.org/css-values-4/#flex-value} |
| 86 | + */ |
| 87 | +export const matchesFlex = (value: CSSNumericValue): boolean => |
| 88 | + matchesSingleType(value, 'flex', 1) |
| 89 | + |
| 90 | +/** |
| 91 | + * A CSSNumericValue matches for `<percent>` |
| 92 | + * when the only non-zero entry of `CSSNumericType` is percent of 1 |
| 93 | + * |
| 94 | + * @see {@link https://drafts.csswg.org/css-values-4/#percent-value} |
| 95 | + */ |
| 96 | +export const matchesPercent = (value: CSSNumericValue): boolean => |
| 97 | + matchesSingleType(value, 'percent', 1) |
| 98 | + |
| 99 | +/** |
| 100 | + * A CSSNumericValue matches for `<length-percentage>` |
| 101 | + * |
| 102 | + * @see {@link https://drafts.csswg.org/css-values-4/#typedef-length-percentage} |
| 103 | + */ |
| 104 | +export const matchesLengthPercentage = (value: CSSNumericValue): boolean => |
| 105 | + matchesLength(value) || matchesPercent(value) |
| 106 | + |
| 107 | +/** |
| 108 | + * A CSSNumericValue matches for `<frequency-percentage>` |
| 109 | + * |
| 110 | + * @see {@link https://drafts.csswg.org/css-values-4/#typedef-frequency-percentage} |
| 111 | + */ |
| 112 | +export const matchesFrequencyPercentage = (value: CSSNumericValue): boolean => |
| 113 | + matchesFrequency(value) || matchesPercent(value) |
| 114 | + |
| 115 | +/** |
| 116 | + * A CSSNumericValue matches for `<angle-percentage>` |
| 117 | + * |
| 118 | + * @see {@link https://drafts.csswg.org/css-values-4/#typedef-angle-percentage} |
| 119 | + */ |
| 120 | +export const matchesAnglePercentage = (value: CSSNumericValue): boolean => |
| 121 | + matchesAngle(value) || matchesPercent(value) |
| 122 | + |
| 123 | +/** |
| 124 | + * A CSSNumericValue matches for `<time-percentage>` |
| 125 | + * |
| 126 | + * @see {@link https://drafts.csswg.org/css-values-4/#typedef-time-percentage} |
| 127 | + */ |
| 128 | +export const matchesTimePercentage = (value: CSSNumericValue): boolean => |
| 129 | + matchesTime(value) || matchesPercent(value) |
0 commit comments