-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.v
370 lines (321 loc) · 7.08 KB
/
types.v
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
module css
import css.ast
import css.datatypes
pub type ColorValue = datatypes.Color | string
// value between 0.0 and 1.0
pub type AlphaValue = Keyword | f64
pub type DimensionValue = Keyword
| datatypes.CalcSum
| datatypes.Length
| datatypes.Percentage
| f64
pub type Keyword = string
pub type Image = Gradient | Keyword | Url
pub type TextCombineUprightDigits = int
pub type TextCombineUpright = Keyword | TextCombineUprightDigits
// text-overflow: ellipsis "[..]"; will become `css.TextOverflow(css.TextEllipsis('[..]'))`
pub type TextEllipsis = string
pub type TextOverflow = Keyword | TextEllipsis
pub type ShadowValue = Keyword | Shadow
pub type BorderLineStyle = Keyword | datatypes.LineStyle
pub type FlexSize = Keyword | f64
pub type FlexDirection = Keyword | datatypes.FlexDirectionKind
pub type FlexWrap = Keyword | datatypes.FlexWrapKind
pub type FontStretch = Keyword | datatypes.FontStretchKind | datatypes.Percentage
pub type FontWeight = Keyword | int
pub type FontFamily = []string
pub type Value = AlphaValue
| Background
| Border
| BorderColors
| BorderLineStyle
| BorderRadius
| BorderStyles
| ColorValue
| DimensionValue
| FlexBox
| FlexDirection
| FlexSize
| FlexWrap
| Font
| FontFamily
| FontStretch
| FontWeight
| FourDimensions
| Gap
| Gradient
| Image
| Keyword
| Overflow
| Shadow
| SingleBorder
| SingleBorderRadius
| Text
| TextCombineUpright
| TextOverflow
| string
pub struct Attribute {
pub mut:
name string
matcher ast.AttributeMatchType
value ?string
}
pub fn (a Attribute) str() string {
mut s := '[${a.name}'
if v := a.value {
s += match a.matcher {
.@none { '' }
.exact { '="${v}"' }
.contains { '*="${v}"' }
.starts_with { '^="${v}"' }
.ends_with { '$="${v}"' }
}
}
s += ']'
return s
}
pub fn (a Attribute) == (b Attribute) bool {
if a.name != b.name {
return false
}
if va := a.value {
vb := b.value or { return false }
return match a.matcher {
.@none {
// should never reach this
false
}
.exact {
va == vb
}
.contains {
va.contains(vb)
}
.starts_with {
va.starts_with(vb)
}
.ends_with {
va.ends_with(vb)
}
}
}
// a[href]
return true
}
pub type Class = string
pub fn (c Class) str() string {
return '.' + c
}
pub type Combinator = string
pub fn (cm Combinator) str() string {
if cm != ' ' {
return ' ' + cm + ' '
} else {
return ' '
}
}
pub type Id = string
pub fn (id Id) str() string {
return '#' + id
}
pub type Type = string
pub fn (typ Type) str() string {
return typ
}
pub struct PseudoClass {
pub mut:
name string
children []Selector
}
pub fn (pc PseudoClass) str() string {
mut s := ':${pc.name}'
if pc.children.len > 0 {
s += '(${pc.children})'
}
return s
}
pub struct PseudoElement {
pub mut:
name string
children []Selector
}
pub fn (pe PseudoElement) str() string {
mut s := '::${pe.name}'
if pe.children.len > 0 {
s += '(${pe.children})'
}
return s
}
pub type Selector = Attribute | Class | Combinator | Id | PseudoClass | PseudoElement | Type
pub fn (selectors []Selector) str() string {
mut res := ''
for s in selectors {
res += match s {
Class { s.str() }
Id { s.str() }
Type { s.str() }
Combinator { s.str() }
PseudoClass { s.str() }
PseudoElement { s.str() }
Attribute { s.str() }
}
}
return res
}
// check if `other_selectors` is a subset of `current_selectors`
// matches the part of `current_selectors` after the last Combinator
pub fn (current_selectors []Selector) matches(other_selectors []Selector) bool {
// last_selectors are all selectors that come after the last Combinator
// in `current_selectors`
mut last_selectors := []Selector{}
for s in current_selectors {
if s is Combinator {
last_selectors.clear()
} else {
last_selectors << s
}
}
for s in last_selectors {
match s {
PseudoClass {
if s.name == 'not' {
if s.children.matches(other_selectors) {
return false
}
}
// TODO: :is
}
PseudoElement {
// pseudo elements should be matched in combination with a DOM
return true
}
Attribute {
for a in other_selectors {
if a is Attribute && s != a {
return false
}
}
}
else {
if s !in other_selectors {
return false
}
}
}
}
return true
}
// Specificity represents CSS specificity
// https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
pub struct Specificity {
pub mut:
col1 int
col2 int
col3 int
}
pub fn (s &Specificity) str() string {
return '${s.col1}-${s.col2}-${s.col3}'
}
pub fn Specificity.from_selectors(selectors []Selector) Specificity {
mut s := Specificity{}
for current_selector in selectors {
match current_selector {
Id {
s.col1++
}
Attribute, Class {
s.col2++
}
PseudoClass {
if current_selector.name == 'where' {
// :where doesn't count for specificity, same applies for its children
continue
} else if current_selector.name !in ['is', 'has', 'not'] {
// :is, :has and :not don't count for specificity, but their children do
s.col2++
}
s += Specificity.from_selectors(current_selector.children)
}
Type {
if current_selector != '*' {
// the universal selector does not count for specificity
s.col3++
}
}
PseudoElement {
s.col3++
s += Specificity.from_selectors(current_selector.children)
}
else {}
}
}
return s
}
fn (a Specificity) == (b Specificity) bool {
return a.col1 == b.col1 && a.col2 == b.col2 && a.col3 == b.col3
}
fn (a Specificity) < (b Specificity) bool {
if a.col1 < b.col1 {
return true
} else if a.col1 == b.col1 {
if a.col2 < b.col2 {
return true
} else if a.col2 == b.col2 {
return a.col3 < b.col3
}
}
return false
}
fn (a Specificity) + (b Specificity) Specificity {
return Specificity{
col1: a.col1 + b.col1
col2: a.col2 + b.col2
col3: a.col3 + b.col3
}
}
fn (a Specificity) - (b Specificity) Specificity {
return Specificity{
col1: a.col1 - b.col1
col2: a.col2 - b.col2
col3: a.col3 - b.col3
}
}
pub struct RawValue {
pub mut:
value Value
important bool
}
// Rule represents a css rule with only a single selector
pub struct Rule {
pub mut:
specificity Specificity
selectors []Selector
declarations map[string]RawValue
}
@[inline]
pub fn (r Rule) matches(selectors []Selector) bool {
return r.selectors.matches(selectors)
}
@[inline]
pub fn (rules []Rule) get_matching(selectors []Selector) []Rule {
return rules.filter(it.matches(selectors))
}
pub fn (rules []Rule) get_styles() map[string]Value {
// TODO: merge grouped properties together e.g. `background` gets split
// into 'background-color', 'background-width' etc.
// OR combine them from `background-color` to `background` only
mut styles := map[string]Value{}
mut importants := map[string]bool{}
// TODO: reduce iterations
for rule in rules {
for property, value in rule.declarations {
if !value.important && importants[property] == false {
set_grouped(property, value.value, mut styles)
} else if value.important {
set_grouped(property, value.value, mut styles)
importants[property] = true
}
}
}
return styles
}