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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
import std/options
import std/strutils
import css/cssparser
import html/catom
import utils/twtstr
type
SelectorType* = enum
stType, stId, stAttr, stClass, stUniversal, stPseudoClass, stPseudoElement
PseudoElem* = enum
peNone, peBefore, peAfter,
# internal
peInputText, peTextareaText, peImage, peNewline, peVideo, peAudio, peCanvas
PseudoClass* = enum
pcFirstChild, pcLastChild, pcOnlyChild, pcHover, pcRoot, pcNthChild,
pcNthLastChild, pcChecked, pcFocus, pcIs, pcNot, pcWhere, pcLang, pcLink,
pcVisited
CombinatorType* = enum
ctNone, ctDescendant, ctChild, ctNextSibling, ctSubsequentSibling
SelectorParser = object
selectors: seq[ComplexSelector]
cvals: seq[CSSComponentValue]
at: int
failed: bool
factory: CAtomFactory
RelationType* {.size: sizeof(int) div 2.} = enum
rtExists, rtEquals, rtToken, rtBeginDash,
rtStartsWith, rtEndsWith, rtContains
RelationFlag* {.size: sizeof(int) div 2.} = enum
rfNone, rfI, rfS
SelectorRelation* = object
t*: RelationType
flag*: RelationFlag
Selector* = ref object # Simple selector
case t*: SelectorType
of stType:
tag*: CAtom
when defined(debug):
tags: string
of stId:
id*: CAtom
when defined(debug):
ids: string
of stClass:
class*: CAtom
when defined(debug):
classs: string
of stAttr:
attr*: CAtom
when defined(debug):
attrs: string
value*: string
rel*: SelectorRelation
of stUniversal: #TODO namespaces?
discard
of stPseudoClass:
pseudo*: PseudoData
of stPseudoElement:
elem*: PseudoElem
PseudoData* = object
case t*: PseudoClass
of pcNthChild, pcNthLastChild:
anb*: CSSAnB
ofsels*: SelectorList
of pcIs, pcWhere, pcNot:
fsels*: SelectorList
of pcLang:
s*: string
else: discard
CompoundSelector* = object
ct*: CombinatorType # relation to the next entry in a ComplexSelector.
sels*: seq[Selector]
ComplexSelector* = seq[CompoundSelector]
SelectorList* = seq[ComplexSelector]
iterator items*(sels: CompoundSelector): Selector {.inline.} =
for it in sels.sels:
yield it
func `[]`*(sels: CompoundSelector; i: int): Selector {.inline.} =
return sels.sels[i]
func `[]`*(sels: CompoundSelector; i: BackwardsIndex): Selector {.inline.} =
return sels.sels[i]
func len*(sels: CompoundSelector): int {.inline.} =
return sels.sels.len
proc add*(sels: var CompoundSelector; sel: Selector) {.inline.} =
sels.sels.add(sel)
# For debugging
func tostr(ftype: enum): string =
return ($ftype).split('_')[1..^1].join('-').toLowerAscii()
func `$`*(cxsel: ComplexSelector): string
func `$`*(sel: Selector): string =
case sel.t
of stType:
when defined(debug):
return sel.tags
else:
return "ATOM" & $int(sel.tag)
of stId:
when defined(debug):
return "#" & sel.ids
else:
return "#ATOM" & $int(sel.id)
of stAttr:
let rel = case sel.rel.t
of rtExists: ""
of rtEquals: "="
of rtToken: "~="
of rtBeginDash: "|="
of rtStartsWith: "^="
of rtEndsWith: "$="
of rtContains: "*="
let flag = case sel.rel.flag
of rfNone: ""
of rfI: " i"
of rfS: " s"
let attrs = when defined(debug):
sel.attrs
else:
"ATOM" & $int(sel.attr)
return '[' & attrs & rel & sel.value & flag & ']'
of stClass:
when defined(debug):
return "." & sel.classs
else:
return ".ATOM" & $int(sel.id)
of stUniversal:
return "*"
of stPseudoClass:
result = ':' & sel.pseudo.t.tostr()
case sel.pseudo.t
of pcIs, pcNot, pcWhere:
result &= '('
for fsel in sel.pseudo.fsels:
result &= $fsel
if fsel != sel.pseudo.fsels[^1]:
result &= ", "
result &= ')'
of pcNthChild, pcNthLastChild:
result &= '(' & $sel.pseudo.anb.A & 'n' & $sel.pseudo.anb.B
if sel.pseudo.ofsels.len != 0:
result &= " of "
for fsel in sel.pseudo.ofsels:
result &= $fsel
if fsel != sel.pseudo.ofsels[^1]:
result &= ','
result &= ')'
else: discard
of stPseudoElement:
return "::" & sel.elem.tostr()
func `$`*(sels: CompoundSelector): string =
for sel in sels:
result &= $sel
func `$`*(cxsel: ComplexSelector): string =
for sels in cxsel:
result &= $sels
case sels.ct
of ctDescendant: result &= ' '
of ctChild: result &= " > "
of ctNextSibling: result &= " + "
of ctSubsequentSibling: result &= " ~ "
of ctNone: discard
func `$`*(slist: SelectorList): string =
var s = false
for cxsel in slist:
if s:
result &= ", "
result &= $cxsel
s = true
func getSpecificity*(cxsel: ComplexSelector): int
func getSpecificity(sel: Selector): int =
case sel.t
of stId:
result += 1000000
of stClass, stAttr:
result += 1000
of stPseudoClass:
case sel.pseudo.t
of pcIs, pcNot:
var best = 0
for child in sel.pseudo.fsels:
let s = getSpecificity(child)
if s > best:
best = s
result += best
of pcNthChild, pcNthLastChild:
if sel.pseudo.ofsels.len != 0:
var best = 0
for child in sel.pseudo.ofsels:
let s = getSpecificity(child)
if s > best:
best = s
result += best
result += 1000
of pcWhere: discard
else: result += 1000
of stType, stPseudoElement:
result += 1
of stUniversal:
discard
func getSpecificity*(sels: CompoundSelector): int =
for sel in sels:
result += getSpecificity(sel)
func getSpecificity*(cxsel: ComplexSelector): int =
for sels in cxsel:
result += getSpecificity(sels)
func pseudo*(cxsel: ComplexSelector): PseudoElem =
if cxsel[^1][^1].t == stPseudoElement:
return cxsel[^1][^1].elem
return peNone
proc consume(state: var SelectorParser): CSSComponentValue =
result = state.cvals[state.at]
inc state.at
proc has(state: var SelectorParser; i = 0): bool =
return not state.failed and state.at + i < state.cvals.len
proc peek(state: var SelectorParser; i = 0): CSSComponentValue =
return state.cvals[state.at + i]
template fail() =
state.failed = true
return
template get_tok(cval: CSSComponentValue): CSSToken =
let c = cval
if not (c of CSSToken): fail
CSSToken(c)
proc parseSelectorList(cvals: seq[CSSComponentValue]; factory: CAtomFactory):
SelectorList
# Functions that may contain other selectors, functions, etc.
proc parseRecursiveSelectorFunction(state: var SelectorParser;
class: PseudoClass; body: seq[CSSComponentValue]): Selector =
var fun = Selector(
t: stPseudoClass,
pseudo: PseudoData(t: class),
)
fun.pseudo.fsels = parseSelectorList(body, state.factory)
if fun.pseudo.fsels.len == 0: fail
return fun
proc parseNthChild(state: var SelectorParser; cssfunction: CSSFunction;
data: PseudoData): Selector =
var data = data
var (anb, i) = parseAnB(cssfunction.value)
if anb.isNone: fail
data.anb = anb.get
var nthchild = Selector(t: stPseudoClass, pseudo: data)
while i < cssfunction.value.len and cssfunction.value[i] == cttWhitespace:
inc i
if i >= cssfunction.value.len:
return nthchild
if not (get_tok cssfunction.value[i]).value.equalsIgnoreCase("of"): fail
if i == cssfunction.value.len: fail
nthchild.pseudo.ofsels = cssfunction.value[i..^1]
.parseSelectorList(state.factory)
if nthchild.pseudo.ofsels.len == 0: fail
return nthchild
proc skipWhitespace(state: var SelectorParser) =
while state.has() and state.peek() of CSSToken and
CSSToken(state.peek()).tokenType == cttWhitespace:
inc state.at
proc parseLang(cvals: seq[CSSComponentValue]): Selector =
var state = SelectorParser(cvals: cvals)
state.skipWhitespace()
if not state.has(): fail
let tok = get_tok state.consume()
if tok.tokenType != cttIdent: fail
return Selector(t: stPseudoClass, pseudo: PseudoData(t: pcLang, s: tok.value))
proc parseSelectorFunction(state: var SelectorParser; cssfunction: CSSFunction):
Selector =
return case cssfunction.name.toLowerAscii()
of "not":
state.parseRecursiveSelectorFunction(pcNot, cssfunction.value)
of "is":
state.parseRecursiveSelectorFunction(pcIs, cssfunction.value)
of "where":
state.parseRecursiveSelectorFunction(pcWhere, cssfunction.value)
of "nth-child":
state.parseNthChild(cssfunction, PseudoData(t: pcNthChild))
of "nth-last-child":
state.parseNthChild(cssfunction, PseudoData(t: pcNthLastChild))
of "lang":
parseLang(cssfunction.value)
else: fail
proc parsePseudoSelector(state: var SelectorParser): Selector =
if not state.has(): fail
let cval = state.consume()
if cval of CSSToken:
template add_pseudo_element(element: PseudoElem) =
return Selector(t: stPseudoElement, elem: element)
let tok = CSSToken(cval)
case tok.tokenType
of cttIdent:
template add_pseudo_class(class: PseudoClass) =
return Selector(t: stPseudoClass, pseudo: PseudoData(t: class))
case tok.value.toLowerAscii()
of "before": add_pseudo_element peBefore
of "after": add_pseudo_element peAfter
of "first-child": add_pseudo_class pcFirstChild
of "last-child": add_pseudo_class pcLastChild
of "only-child": add_pseudo_class pcOnlyChild
of "hover": add_pseudo_class pcHover
of "root": add_pseudo_class pcRoot
of "checked": add_pseudo_class pcChecked
of "focus": add_pseudo_class pcFocus
of "link": add_pseudo_class pcLink
of "visited": add_pseudo_class pcVisited
else: fail
of cttColon:
if not state.has(): fail
let tok = get_tok state.consume()
if tok.tokenType != cttIdent: fail
case tok.value.toLowerAscii()
of "before": add_pseudo_element peBefore
of "after": add_pseudo_element peAfter
else: fail
else: fail
elif cval of CSSFunction:
return state.parseSelectorFunction(CSSFunction(cval))
else: fail
proc parseComplexSelector(state: var SelectorParser): ComplexSelector
proc parseAttributeSelector(state: var SelectorParser;
cssblock: CSSSimpleBlock): Selector =
if cssblock.token.tokenType != cttLbracket: fail
var state2 = SelectorParser(cvals: cssblock.value)
state2.skipWhitespace()
if not state2.has(): fail
let attr = get_tok state2.consume()
if attr.tokenType != cttIdent: fail
state2.skipWhitespace()
if not state2.has():
return Selector(
t: stAttr,
attr: state.factory.toAtom(attr.value),
rel: SelectorRelation(t: rtExists)
)
let delim = get_tok state2.consume()
if delim.tokenType != cttDelim: fail
let rel = case delim.cvalue
of '~': rtToken
of '|': rtBeginDash
of '^': rtStartsWith
of '$': rtEndsWith
of '*': rtContains
of '=': rtEquals
else: fail
if rel != rtEquals:
let delim = get_tok state2.consume()
if delim.tokenType != cttDelim or delim.cvalue != '=': fail
state2.skipWhitespace()
if not state2.has(): fail
let value = get_tok state2.consume()
if value.tokenType notin {cttIdent, cttString}: fail
state2.skipWhitespace()
var flag = rfNone
if state2.has():
let delim = get_tok state2.consume()
if delim.tokenType != cttIdent: fail
if delim.value.equalsIgnoreCase("i"):
flag = rfI
elif delim.value.equalsIgnoreCase("s"):
flag = rfS
return Selector(
t: stAttr,
attr: state.factory.toAtom(attr.value),
value: value.value,
rel: SelectorRelation(
t: rel,
flag: flag
)
)
proc parseClassSelector(state: var SelectorParser): Selector =
if not state.has(): fail
let tok = get_tok state.consume()
if tok.tokenType != cttIdent: fail
let class = state.factory.toAtom(tok.value)
result = Selector(t: stClass, class: class)
when defined(debug):
result.classs = tok.value
proc parseCompoundSelector(state: var SelectorParser): CompoundSelector =
result = CompoundSelector()
while state.has():
let cval = state.peek()
if cval of CSSToken:
let tok = CSSToken(cval)
case tok.tokenType
of cttIdent:
inc state.at
let s = tok.value.toLowerAscii()
let tag = state.factory.toAtom(s)
let sel = Selector(t: stType, tag: tag)
when defined(debug):
sel.tags = s
result.add(sel)
of cttColon:
inc state.at
result.add(state.parsePseudoSelector())
of cttHash:
inc state.at
let id = state.factory.toAtom(tok.value)
result.add(Selector(t: stId, id: id))
when defined(debug):
result[^1].ids = tok.value
of cttComma: break
of cttDelim:
case tok.cvalue
of '.':
inc state.at
result.add(state.parseClassSelector())
of '*':
inc state.at
result.add(Selector(t: stUniversal))
of '>', '+', '~': break
else: fail
of cttWhitespace:
# skip trailing whitespace
if not state.has(1) or state.peek(1) == cttComma:
inc state.at
elif state.peek(1) == cttDelim:
let tok = CSSToken(state.peek(1))
if tok.cvalue in {'>', '+', '~'}:
inc state.at
break
else: fail
elif cval of CSSSimpleBlock:
inc state.at
result.add(state.parseAttributeSelector(CSSSimpleBlock(cval)))
else:
fail
proc parseComplexSelector(state: var SelectorParser): ComplexSelector =
while true:
state.skipWhitespace()
let sels = state.parseCompoundSelector()
result.add(sels)
if sels.len == 0: fail
if not state.has():
break # finish
let tok = get_tok state.consume()
case tok.tokenType
of cttDelim:
case tok.cvalue
of '>': result[^1].ct = ctChild
of '+': result[^1].ct = ctNextSibling
of '~': result[^1].ct = ctSubsequentSibling
else: fail
of cttWhitespace:
result[^1].ct = ctDescendant
of cttComma:
break # finish
else: fail
if result.len == 0 or result[^1].ct != ctNone:
fail
proc parseSelectorList(cvals: seq[CSSComponentValue]; factory: CAtomFactory):
SelectorList =
var state = SelectorParser(cvals: cvals, factory: factory)
var res: SelectorList = @[]
while state.has():
res.add(state.parseComplexSelector())
if not state.failed:
return res
proc parseSelectors*(cvals: seq[CSSComponentValue]; factory: CAtomFactory):
seq[ComplexSelector] =
return parseSelectorList(cvals, factory)
proc parseSelectors*(ibuf: string; factory: CAtomFactory):
seq[ComplexSelector] =
return parseSelectors(parseComponentValues(ibuf), factory)
|