about summary refs log tree commit diff stats
path: root/src/css/selectorparser.nim
blob: 6b719ba6990c015e9b863925f2728a10d8e4e140 (plain) (blame)
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
import options
import streams
import strutils
import unicode

import css/cssparser
import html/tags

type
  SelectorType* = enum
    TYPE_SELECTOR, ID_SELECTOR, ATTR_SELECTOR, CLASS_SELECTOR,
    UNIVERSAL_SELECTOR, PSEUDO_SELECTOR, PSELEM_SELECTOR, COMBINATOR_SELECTOR

  QueryMode = enum
    QUERY_TYPE, QUERY_CLASS, QUERY_ATTR, QUERY_DELIM, QUERY_VALUE,
    QUERY_PSEUDO, QUERY_PSELEM, QUERY_DESC_COMBINATOR, QUERY_CHILD_COMBINATOR,
    QUERY_NEXT_SIBLING_COMBINATOR, QUERY_SUBSEQ_SIBLING_COMBINATOR

  PseudoElem* = enum
    PSEUDO_NONE, PSEUDO_BEFORE, PSEUDO_AFTER,
    # internal
    PSEUDO_INPUT_TEXT, PSEUDO_TEXTAREA_TEXT, PSEUDO_IMAGE, PSEUDO_NEWLINE

  PseudoClass* = enum
    PSEUDO_FIRST_CHILD, PSEUDO_LAST_CHILD, PSEUDO_ONLY_CHILD, PSEUDO_HOVER,
    PSEUDO_ROOT, PSEUDO_NTH_CHILD, PSEUDO_NTH_LAST_CHILD, PSEUDO_CHECKED,
    PSEUDO_FOCUS, PSEUDO_IS, PSEUDO_NOT, PSEUDO_WHERE, PSEUDO_LANG,
    PSEUDO_LINK, PSEUDO_VISITED

  CombinatorType* = enum
    DESCENDANT_COMBINATOR, CHILD_COMBINATOR, NEXT_SIBLING_COMBINATOR,
    SUBSEQ_SIBLING_COMBINATOR

  SelectorParser = object
    selectors: seq[ComplexSelector]
    query: QueryMode
    combinator: Selector

  Selector* = ref object of RootObj # compound selector
    case t*: SelectorType
    of TYPE_SELECTOR:
      tag*: TagType
    of ID_SELECTOR:
      id*: string
    of ATTR_SELECTOR:
      attr*: string
      value*: string
      rel*: char
    of CLASS_SELECTOR:
      class*: string
    of UNIVERSAL_SELECTOR: #TODO namespaces?
      discard
    of PSEUDO_SELECTOR:
      pseudo*: PseudoData
    of PSELEM_SELECTOR:
      elem*: PseudoElem
    of COMBINATOR_SELECTOR:
      ct*: CombinatorType
      csels*: SelectorList

  PseudoData* = object
    case t*: PseudoClass
    of PSEUDO_NTH_CHILD, PSEUDO_NTH_LAST_CHILD:
      anb*: CSSAnB
      ofsels*: Option[SelectorList]
    of PSEUDO_IS, PSEUDO_WHERE, PSEUDO_NOT:
      fsels*: SelectorList
    of PSEUDO_LANG:
      s*: string
    else: discard

  # Kind of an oversimplification, but the distinction between complex and
  # compound selectors isn't too significant.
  ComplexSelector* = seq[Selector]

  SelectorList* = seq[ComplexSelector]

# For debugging
proc tostr(ftype: enum): string =
  return ($ftype).split('_')[1..^1].join("-").tolower()

proc `$`*(sellist: ComplexSelector): string

proc `$`*(sel: Selector): string =
  case sel.t
  of TYPE_SELECTOR:
    return tagName(sel.tag)
  of ID_SELECTOR:
    return '#' & sel.id
  of ATTR_SELECTOR:
    var rel = ""
    if sel.rel == '=':
      rel = "="
    elif sel.rel == ' ':
      discard
    else:
      rel = sel.rel & '='
    return '[' & sel.attr & rel & sel.value & ']'
  of CLASS_SELECTOR:
    return '.' & sel.class
  of UNIVERSAL_SELECTOR:
    return "*"
  of PSEUDO_SELECTOR:
    result = ':' & sel.pseudo.t.tostr()
    case sel.pseudo.t
    of PSEUDO_IS, PSEUDO_NOT, PSEUDO_WHERE:
      result &= '('
      for fsel in sel.pseudo.fsels:
        result &= $fsel
        if fsel != sel.pseudo.fsels[^1]:
          result &= ','
      result &= ')'
    of PSEUDO_NTH_CHILD, PSEUDO_NTH_LAST_CHILD:
      result &= '(' & $sel.pseudo.anb.A & 'n' & $sel.pseudo.anb.B
      if sel.pseudo.ofsels.issome:
        result &= " of "
        for fsel in sel.pseudo.ofsels.get:
          result &= $fsel
          if fsel != sel.pseudo.ofsels.get[^1]:
            result &= ','
      result &= ')'
    else: discard
  of PSELEM_SELECTOR:
    return "::" & sel.elem.tostr()
  of COMBINATOR_SELECTOR:
    var delim: char
    case sel.ct
    of DESCENDANT_COMBINATOR: delim = ' '
    of CHILD_COMBINATOR: delim = '>'
    of NEXT_SIBLING_COMBINATOR: delim = '+'
    of SUBSEQ_SIBLING_COMBINATOR: delim = '~'
    for slist in sel.csels:
      result &= $slist
      if slist != sel.csels[^1]:
        result &= delim

proc `$`*(sellist: ComplexSelector): string =
  for sel in sellist:
    result &= $sel

func getSpecificity*(sels: ComplexSelector): int

func getSpecificity(sel: Selector): int =
  case sel.t
  of ID_SELECTOR:
    result += 1000000
  of CLASS_SELECTOR, ATTR_SELECTOR:
    result += 1000
  of PSEUDO_SELECTOR:
    case sel.pseudo.t
    of PSEUDO_IS, PSEUDO_NOT:
      var best = 0
      for child in sel.pseudo.fsels:
        let s = getSpecificity(child)
        if s > best:
          best = s
      result += best
    of PSEUDO_NTH_CHILD, PSEUDO_NTH_LAST_CHILD:
      if sel.pseudo.ofsels.issome:
        var best = 0
        for child in sel.pseudo.ofsels.get:
          let s = getSpecificity(child)
          if s > best:
            best = s
        result += best
      result += 1000
    of PSEUDO_WHERE: discard
    else: result += 1000
  of TYPE_SELECTOR, PSELEM_SELECTOR:
    result += 1
  of UNIVERSAL_SELECTOR:
    discard
  of COMBINATOR_SELECTOR:
    for child in sel.csels:
      result += getSpecificity(child)

func getSpecificity*(sels: ComplexSelector): int =
  for sel in sels:
    result += getSpecificity(sel)

func pseudo*(sels: ComplexSelector): PseudoElem =
  var sel = sels[^1]
  while sel.t == COMBINATOR_SELECTOR:
    sel = sel.csels[^1][^1]
  if sel.t == PSELEM_SELECTOR:
    return sel.elem
  return PSEUDO_NONE

proc addSelector(state: var SelectorParser, sel: Selector) =
  if state.combinator != nil:
    state.combinator.csels[^1].add(sel)
  else:
    state.selectors[^1].add(sel)

proc getLastSel(state: SelectorParser): Selector =
  if state.combinator != nil:
    return state.combinator.csels[^1][^1]
  else:
    return state.selectors[^1][^1]

proc addComplexSelector(state: var SelectorParser) =
  if state.combinator != nil:
    state.selectors[^1].add(state.combinator)
    state.combinator = nil
  state.selectors.add(newSeq[Selector]())

func getComplexSelectors(state: var SelectorParser): seq[ComplexSelector] =
  result = state.selectors
  if state.combinator != nil:
    result[^1].add(state.combinator)

proc parseSelectorCombinator(state: var SelectorParser, ct: CombinatorType, csstoken: CSSToken) =
  if csstoken.tokenType notin {CSS_IDENT_TOKEN, CSS_HASH_TOKEN, CSS_COLON_TOKEN} and
     (csstoken.tokenType != CSS_DELIM_TOKEN or csstoken.rvalue != Rune('.')):
    return
  if state.combinator != nil and state.combinator.ct != ct:
    let nc = Selector(t: COMBINATOR_SELECTOR, ct: ct)
    nc.csels.add(@[state.combinator])
    state.combinator = nc

  if state.combinator == nil:
    state.combinator = Selector(t: COMBINATOR_SELECTOR, ct: ct)

  state.combinator.csels.add(state.selectors[^1])
  if state.combinator.csels[^1].len > 0:
    state.combinator.csels.add(newSeq[Selector]())
  state.selectors[^1].setLen(0)
  state.query = QUERY_TYPE

proc parseSelectorToken(state: var SelectorParser, csstoken: CSSToken) =
  case state.query
  of QUERY_DESC_COMBINATOR:
    state.parseSelectorCombinator(DESCENDANT_COMBINATOR, csstoken)
  of QUERY_CHILD_COMBINATOR:
    if csstoken.tokenType == CSS_WHITESPACE_TOKEN:
      return
    state.parseSelectorCombinator(CHILD_COMBINATOR, csstoken)
  of QUERY_NEXT_SIBLING_COMBINATOR:
    if csstoken.tokenType == CSS_WHITESPACE_TOKEN:
      return
    state.parseSelectorCombinator(NEXT_SIBLING_COMBINATOR, csstoken)
  of QUERY_SUBSEQ_SIBLING_COMBINATOR:
    if csstoken.tokenType == CSS_WHITESPACE_TOKEN:
      return
    state.parseSelectorCombinator(SUBSEQ_SIBLING_COMBINATOR, csstoken)
  else: discard

  template add_pseudo_element(element: PseudoElem) =
    state.addSelector(Selector(t: PSELEM_SELECTOR, elem: element))
  case csstoken.tokenType
  of CSS_IDENT_TOKEN:
    case state.query
    of QUERY_CLASS:
      state.addSelector(Selector(t: CLASS_SELECTOR, class: csstoken.value))
    of QUERY_TYPE:
      state.addSelector(Selector(t: TYPE_SELECTOR, tag: tagType(csstoken.value)))
    of QUERY_PSEUDO:
      template add_pseudo_class(class: PseudoClass) =
        state.addSelector(Selector(t: PSEUDO_SELECTOR, pseudo: PseudoData(t: class)))
      case csstoken.value
      of "before":
        add_pseudo_element PSEUDO_BEFORE
      of "after":
        add_pseudo_element PSEUDO_AFTER
      of "first-child":
        add_pseudo_class PSEUDO_FIRST_CHILD
      of "last-child":
        add_pseudo_class PSEUDO_LAST_CHILD
      of "only-child":
        add_pseudo_class PSEUDO_ONLY_CHILD
      of "hover":
        add_pseudo_class PSEUDO_HOVER
      of "root":
        add_pseudo_class PSEUDO_ROOT
      of "checked":
        add_pseudo_class PSEUDO_CHECKED
      of "focus":
        add_pseudo_class PSEUDO_FOCUS
      of "link":
        add_pseudo_class PSEUDO_LINK
      of "visited":
        add_pseudo_class PSEUDO_VISITED
    of QUERY_PSELEM:
      case csstoken.value
      of "before":
        add_pseudo_element PSEUDO_BEFORE
      of "after":
        add_pseudo_element PSEUDO_AFTER
      else: discard
    else: discard
    state.query = QUERY_TYPE
  of CSS_DELIM_TOKEN:
    case csstoken.rvalue
    of Rune('.'):
      state.query = QUERY_CLASS
    of Rune('>'):
      if state.selectors[^1].len > 0 or state.combinator != nil:
        state.query = QUERY_CHILD_COMBINATOR
    of Rune('+'):
      if state.selectors[^1].len > 0 or state.combinator != nil:
        state.query = QUERY_NEXT_SIBLING_COMBINATOR
    of Rune('~'):
      if state.selectors[^1].len > 0 or state.combinator != nil:
        state.query = QUERY_SUBSEQ_SIBLING_COMBINATOR
    of Rune('*'):
      state.addSelector(Selector(t: UNIVERSAL_SELECTOR))
    else: discard
  of CSS_HASH_TOKEN:
    state.addSelector(Selector(t: ID_SELECTOR, id: csstoken.value))
  of CSS_COMMA_TOKEN:
    if state.selectors[^1].len > 0:
      state.addComplexSelector()
  of CSS_WHITESPACE_TOKEN:
    if state.selectors[^1].len > 0 or state.combinator != nil:
      state.query = QUERY_DESC_COMBINATOR
  of CSS_COLON_TOKEN:
    if state.query == QUERY_PSEUDO:
      state.query = QUERY_PSELEM
    else:
      state.query = QUERY_PSEUDO
  else: discard

proc parseSelectorSimpleBlock(state: var SelectorParser, cssblock: CSSSimpleBlock) =
  case cssblock.token.tokenType
  of CSS_LBRACKET_TOKEN:
    state.query = QUERY_ATTR
    for cval in cssblock.value:
      if cval of CSSToken:
        let csstoken = (CSSToken)cval
        case csstoken.tokenType
        of CSS_IDENT_TOKEN:
          case state.query
          of QUERY_ATTR:
            state.query = QUERY_DELIM
            state.addSelector(Selector(t: ATTR_SELECTOR, attr: csstoken.value, rel: ' '))
          of QUERY_VALUE:
            state.getLastSel().value = csstoken.value
            break
          else: discard
        of CSS_STRING_TOKEN:
          case state.query
          of QUERY_VALUE:
            state.getLastSel().value = csstoken.value
            break
          else: discard
        of CSS_DELIM_TOKEN:
          case csstoken.rvalue
          of Rune('~'), Rune('|'), Rune('^'), Rune('$'), Rune('*'):
            if state.query == QUERY_DELIM:
              state.getLastSel().rel = char(csstoken.rvalue)
          of Rune('='):
            if state.query == QUERY_DELIM:
              if state.getLastSel().rel == ' ':
                state.getLastSel().rel = '='
              state.query = QUERY_VALUE
          else: discard
        else: discard
    state.query = QUERY_TYPE
  else: discard

proc parseSelectorFunction(state: var SelectorParser, cssfunction: CSSFunction)

proc parseSelectorFunctionBody(state: var SelectorParser, body: seq[CSSComponentValue]): seq[ComplexSelector] =
  let osels = state.selectors
  let ocomb = state.combinator
  state.combinator = nil
  state.selectors = newSeq[ComplexSelector]()
  state.addComplexSelector()
  for cval in body:
    if cval of CSSToken:
      state.parseSelectorToken(CSSToken(cval))
    elif cval of CSSSimpleBlock:
      state.parseSelectorSimpleBlock(CSSSimpleBlock(cval))
    elif cval of CSSFunction:
      state.parseSelectorFunction(CSSFunction(cval))
  result = state.getComplexSelectors()
  state.selectors = osels
  state.combinator = ocomb

proc parseNthChild(state: var SelectorParser, cssfunction: CSSFunction, data: PseudoData) =
  var data = data
  let (anb, i) = parseAnB(cssfunction.value)
  if anb.issome:
    data.anb = anb.get
    var nthchild = Selector(t: PSEUDO_SELECTOR, pseudo: data)
    var i = i
    while i < cssfunction.value.len and cssfunction.value[i] == CSS_WHITESPACE_TOKEN:
      inc i
    if i >= cssfunction.value.len:
      state.addSelector(nthchild)
    else:
      if cssfunction.value[i] == CSS_IDENT_TOKEN and CSSToken(cssfunction.value[i]).value == "of":
        if i < cssfunction.value.len:
          let body = cssfunction.value[i..^1]
          let val = state.parseSelectorFunctionBody(body)
          if val.len > 0:
            nthchild.pseudo.ofsels = some(val)
            state.addSelector(nthchild)
  state.query = QUERY_TYPE

proc parseSelectorFunction(state: var SelectorParser, cssfunction: CSSFunction) =
  if state.query != QUERY_PSEUDO:
    return
  let ftype = case cssfunction.name
  of "not": PSEUDO_NOT
  of "is": PSEUDO_IS
  of "where": PSEUDO_WHERE
  of "nth-child":
    state.parseNthChild(cssfunction, PseudoData(t: PSEUDO_NTH_CHILD))
    return
  of "nth-last-child":
    state.parseNthChild(cssfunction, PseudoData(t: PSEUDO_NTH_LAST_CHILD))
    return
  of "lang":
    if cssfunction.value.len == 0: return
    var i = 0
    template tok: CSSComponentValue = cssfunction.value[i]
    while i < cssfunction.value.len:
      if tok != CSS_WHITESPACE_TOKEN: break
      inc i
    if i >= cssfunction.value.len: return
    if tok != CSS_IDENT_TOKEN: return
    state.addSelector(Selector(t: PSEUDO_SELECTOR, pseudo: PseudoData(t: PSEUDO_LANG, s: CSSToken(tok).value)))
    return
  else: return
  state.query = QUERY_TYPE
  var data = PseudoData(t: ftype)
  var fun = Selector(t: PSEUDO_SELECTOR, pseudo: data)
  state.addSelector(fun)
  fun.pseudo.fsels = state.parseSelectorFunctionBody(cssfunction.value)

func parseSelectors*(cvals: seq[CSSComponentValue]): seq[ComplexSelector] = {.cast(noSideEffect).}:
  var state = SelectorParser()
  state.addComplexSelector()
  for cval in cvals:
    if cval of CSSToken:
      state.parseSelectorToken(CSSToken(cval))
    elif cval of CSSSimpleBlock:
      state.parseSelectorSimpleBlock(CSSSimpleBlock(cval))
    elif cval of CSSFunction:
      state.parseSelectorFunction(CSSFunction(cval))
  if state.combinator != nil:
    if state.combinator.csels.len == 1:
      if state.combinator.ct == DESCENDANT_COMBINATOR:
        # otherwise it's an invalid combinator
        state.selectors[^1].add(state.combinator.csels[0])
      else:
        state.selectors.setLen(0)
    elif state.combinator.csels[^1].len != 0:
      state.selectors[^1].add(state.combinator)
    state.combinator = nil
  if state.selectors.len > 0 and state.selectors[^1].len == 0:
    # invalid selector
    state.selectors.setLen(0)
  return state.selectors

proc parseSelectors*(stream: Stream): seq[ComplexSelector] =
  return parseSelectors(parseListOfComponentValues(stream))