-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
presedence.ts
161 lines (133 loc) · 4.66 KB
/
presedence.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* eslint-disable no-return-assign, no-cond-assign, no-implicit-coercion */
// Based on https://github.com/kripod/otion
// License MIT
import type { ThemeResolver } from '@tw-in-js/types'
import { tail, includes } from './util'
// Shared variables
let _: string | RegExpExecArray | null | number
// 0=none, 1=sm, 2=md, 3=lg, 4=xl, 5=2xl, 6=??, 7=??
// 0 - 31: 5 bits
// 576px -> 3
// 1536px -> 9
// 36rem -> 3
// 96rem -> 9
export const responsivePrecedence = (css: string): number =>
(((_ = /(?:^|min-width:\s*)(\d+(?:.\d+)?)(p)?/.exec(css)) ? +_[1] / (_[2] ? 15 : 1) / 10 : 0) &
31) <<
23
// Colon and dash count of string (ascending): 0 -> 7 => 3 bits
export const seperatorPrecedence = (string: string): number => {
_ = 0
for (let index = string.length; index--; ) {
if (includes('-:,', string[index])) {
++_
}
}
return _
}
export const atRulePresedence = (css: string): number => (seperatorPrecedence(css) & 15) << 18
// Pesudo variant presedence
// Chars 3 - 8: Uniquely identifies a pseudo selector
// represented as a bit set for each relevant value
// 17 bits: one for each variant plus one for unknown variants
//
// ':group-*' variants are normalized to their native pseudo class (':group-hover' -> ':hover')
// as they already have a higher selector presedence due to the add '.group' ('.group:hover .group-hover:...')
// Sources:
// - https://bitsofco.de/when-do-the-hover-focus-and-active-pseudo-classes-apply/#orderofstyleshoverthenfocusthenactive
// - https://developer.mozilla.org/docs/Web/CSS/:active#Active_links
// - https://github.com/tailwindlabs/tailwindcss/blob/master/stubs/defaultConfig.stub.js#L718
/* eslint-disable capitalized-comments */
const PRECEDENCES_BY_PSEUDO_CLASS = [
/* fi */ 'rst' /* : 0 */,
/* la */ 'st' /* : 1 */,
/* ev */ 'en' /* : 2 */,
/* od */ 'd' /* : 3 */,
/* li */ 'nk' /* : 4 */,
/* vi */ 'sited' /* : 5 */,
/* em */ 'pty' /* : 6 */,
/* ch */ 'ecked' /* : 7 */,
/* fo */ 'cus-w' /* ithin : 8 */,
/* ho */ 'ver' /* : 9 */,
/* fo */ 'cus' /* : 10 */,
/* fo */ 'cus-v' /* isible : 11 */,
/* ac */ 'tive' /* : 12 */,
/* di */ 'sable' /* d : 13 */,
/* re */ 'ad-on' /* ly: 14 */,
/* op */ 'tiona' /* l: 15 */,
/* re */ 'quire' /* d: 16 */,
]
/* eslint-enable capitalized-comments */
const pseudoPrecedence = (pseudoClass: string): number =>
1 <<
(~(_ = PRECEDENCES_BY_PSEUDO_CLASS.indexOf(pseudoClass.replace(/^:group-/, ':').slice(3, 8)))
? _
: 17)
// Variants: 28 bits
export const makeVariantPresedenceCalculator = (
theme: ThemeResolver,
variants: Record<string, string | undefined>,
) => (presedence: number, variant: string): number =>
presedence |
// 5: responsive
((_ = theme('screens', tail(variant), ''))
? // 0=none, 1=sm, 2=md, 3=lg, 4=xl, 5=2xl, 6=??, 7=??
// 0 - 31: 5 bits
// 576px -> 3
// 1536px -> 9
// 36rem -> 3
// 96rem -> 9
responsivePrecedence(_)
: // 1: dark mode flag
variant === ':dark'
? 1 << 22
: // 4: precedence of other at-rules
(_ = variants[variant] || variant)[0] === '@'
? atRulePresedence(_)
: // 17: pseudo and group variants
pseudoPrecedence(variant))
// https://github.com/kripod/otion/blob/main/packages/otion/src/propertyMatchers.ts
// "+1": [
// /* ^border-.*(w|c|sty) */
// "border-.*(width,color,style)",
// /* ^[tlbr].{2,4}m?$ */
// "top",
// "left",
// "bottom",
// "right",
// /* ^c.{7}$ */
// "continue",
// ],
// "-1": [
// /* ^[fl].{5}l */
// "flex-flow",
// "line-clamp",
// /* ^g.{8}$ */
// "grid-area",
// /* ^pl */
// "place-content",
// "place-items",
// "place-self",
// ],
// group: 1 => +1
// group: 2 => -1
const PROPERTY_PRECEDENCE_CORRECTION_GROUPS = /^(?:(border-(?:[tlbr].{2,4}-)?(?:w|c|sty)|[tlbr].{2,4}m?$|c.{7}$)|([fl].{5}l|g.{8}$|pl))/
// 0 - 15 => 4 bits
const propertyPrecedence = (property: string): number => {
// The property's baseline precedence is based on "-" counting
const unprefixedProperty =
property[0] === '-' ? tail(property, property.indexOf('-', 1) + 1) : property
_ = PROPERTY_PRECEDENCE_CORRECTION_GROUPS.exec(unprefixedProperty)
return (
seperatorPrecedence(unprefixedProperty) + (_ ? +!!_[1] /* +1 */ || -!!_[2] /* -1 */ : 0) + 1
)
}
// 0 - 15 => 4 bits
// Ignore vendor prefixed and custom properties
export const declarationPropertyPrecedence = (property: string): number =>
property[0] === '-' ? 0 : propertyPrecedence(property)
export const descending = (value: number): number => Math.max(0, 15 - value)
// 0 - 15 => 4 bits
export const declarationValuePrecedence = (value: string): number =>
descending(seperatorPrecedence(value))
/* eslint-enable no-return-assign, no-cond-assign, no-implicit-coercion */