about summary refs log tree commit diff stats
path: root/src/html/dom.nim
blob: 1a393134e941014b1788bc5d29d7460a621e93ec (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
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
510
511
512
513
514
515
516
import terminal
import uri
import unicode
import strutils
import tables
import streams
import sequtils
import sugar
import algorithm

import ../css/style
import ../css/cssparser
import ../css/selector

import ../types/enums

import ../utils/twtstr

import ../io/twtio

const css = staticRead"../../res/default.css"
let stylesheet = parseCSS(newStringStream(css))

type
  EventTarget* = ref EventTargetObj
  EventTargetObj = object of RootObj

  Node* = ref NodeObj
  NodeObj = object of EventTargetObj
    nodeType*: NodeType
    childNodes*: seq[Node]
    children*: seq[Element]
    isConnected*: bool
    nextSibling*: Node
    previousSibling*: Node
    parentNode*: Node
    parentElement*: Element
    ownerDocument*: Document

    rawtext*: string
    fmttext*: seq[string]
    x*: int
    y*: int
    ex*: int
    ey*: int
    width*: int
    height*: int
    hidden*: bool
    box*: CSSBox

  Attr* = ref AttrObj
  AttrObj = object of NodeObj
    namespaceURI*: string
    prefix*: string
    localName*: string
    name*: string
    value*: string
    ownerElement*: Element

  Document* = ref DocumentObj
  DocumentObj = object of NodeObj
    location*: Uri
    type_elements*: array[low(TagType)..high(TagType), seq[Element]]
    id_elements*: Table[string, seq[Element]]
    class_elements*: Table[string, seq[Element]]
    all_elements*: seq[Element]
    head*: HTMLElement
    body*: HTMLElement

  CharacterData* = ref CharacterDataObj
  CharacterDataObj = object of NodeObj
    data*: string
    length*: int

  Text* = ref TextObj
  TextObj = object of CharacterDataObj
    wholeText*: string

  Comment* = ref CommentObj
  CommentObj = object of CharacterDataObj

  Element* = ref ElementObj
  ElementObj = object of NodeObj
    namespaceURI*: string
    prefix*: string
    localName*: string
    tagName*: string
    tagType*: TagType

    id*: string
    classList*: seq[string]
    attributes*: Table[string, Attr]
    style*: CSS2Properties
    cssvalues*: seq[CSSComputedValue]

  HTMLElement* = ref HTMLElementObj
  HTMLElementObj = object of ElementObj

  HTMLInputElement* = ref HTMLInputElementObj
  HTMLInputElementObj = object of HTMLElementObj
    itype*: InputType
    autofocus*: bool
    required*: bool
    value*: string
    size*: int

  HTMLAnchorElement* = ref HTMLAnchorElementObj
  HTMLAnchorElementObj = object of HTMLElementObj
    href*: string

  HTMLSelectElement* = ref HTMLSelectElementObj
  HTMLSelectElementObj = object of HTMLElementObj
    name*: string
    value*: string
    valueSet*: bool

  HTMLSpanElement* = ref HTMLSpanElementObj
  HTMLSpanElementObj = object of HTMLElementObj

  HTMLOptionElement* = ref HTMLOptionElementObj
  HTMLOptionElementObj = object of HTMLElementObj
    value*: string
  
  HTMLHeadingElement* = ref HTMLHeadingElementObj
  HTMLHeadingElementObj = object of HTMLElementObj
    rank*: uint16

  HTMLBRElement* = ref HTMLBRElementObj
  HTMLBRElementObj = object of HTMLElementObj


func firstChild(node: Node): Node =
  if node.childNodes.len == 0:
    return nil
  return node.childNodes[0]

func lastChild(node: Node): Node =
  if node.childNodes.len == 0:
    return nil
  return node.childNodes[^1]

func firstElementChild(node: Node): Element =
  if node.children.len == 0:
    return nil
  return node.children[0]

func lastElementChild(node: Node): Element =
  if node.children.len == 0:
    return nil
  return node.children[^1]

func `$`*(element: Element): string =
  return "Element of " & $element.tagType

#TODO
func nodeAttr*(node: Node): HtmlElement =
  case node.nodeType
  of TEXT_NODE: return HtmlElement(node.parentElement)
  of ELEMENT_NODE: return HtmlElement(node)
  else: assert(false)

func getStyle*(node: Node): CSS2Properties =
  case node.nodeType
  of TEXT_NODE: return node.parentElement.style
  of ELEMENT_NODE: return Element(node).style
  else: assert(false)

func displayed*(node: Node): bool =
  return node.rawtext.len > 0 and node.getStyle().display != DISPLAY_NONE

func isTextNode*(node: Node): bool =
  return node.nodeType == TEXT_NODE

func isElemNode*(node: Node): bool =
  return node.nodeType == ELEMENT_NODE

func isComment*(node: Node): bool =
  return node.nodeType == COMMENT_NODE

func isCData*(node: Node): bool =
  return node.nodeType == CDATA_SECTION_NODE

func isDocument*(node: Node): bool =
  return node.nodeType == DOCUMENT_NODE

func getFmtLen*(htmlNode: Node): int =
  return htmlNode.fmttext.join().runeLen()

func getRawLen*(htmlNode: Node): int =
  return htmlNode.rawtext.runeLen()

func firstNode*(htmlNode: Node): bool =
  return htmlNode.parentElement != nil and htmlNode.parentElement.childNodes[0] == htmlNode

func lastNode*(htmlNode: Node): bool =
  return htmlNode.parentElement != nil and htmlNode.parentElement.childNodes[^1] == htmlNode

func toInputType*(str: string): InputType =
  case str
  of "button": INPUT_BUTTON
  of "checkbox": INPUT_CHECKBOX
  of "color": INPUT_COLOR
  of "date": INPUT_DATE
  of "datetime_local": INPUT_DATETIME_LOCAL
  of "email": INPUT_EMAIL
  of "file": INPUT_FILE
  of "hidden": INPUT_HIDDEN
  of "image": INPUT_IMAGE
  of "month": INPUT_MONTH
  of "number": INPUT_NUMBER
  of "password": INPUT_PASSWORD
  of "radio": INPUT_RADIO
  of "range": INPUT_RANGE
  of "reset": INPUT_RESET
  of "search": INPUT_SEARCH
  of "submit": INPUT_SUBMIT
  of "tel": INPUT_TEL
  of "text": INPUT_TEXT
  of "time": INPUT_TIME
  of "url": INPUT_URL
  of "week": INPUT_WEEK
  else: INPUT_UNKNOWN

func toInputSize*(str: string): int =
  if str.len == 0:
    return 20
  for c in str:
    if not c.isDigit():
      return 20
  return str.parseInt()

func getFmtInput(inputElement: HtmlInputElement): seq[string] =
  case inputElement.itype
  of INPUT_TEXT, INPUT_SEARCH:
    let valueFit = fitValueToSize(inputElement.value, inputElement.size)
    return valueFit.ansiStyle(styleUnderscore).ansiReset().buttonFmt()
  of INPUT_SUBMIT:
    return inputElement.value.buttonFmt()
  else: discard

func getRawInput(inputElement: HtmlInputElement): string =
  case inputElement.itype
  of INPUT_TEXT, INPUT_SEARCH:
    return inputElement.value.fitValueToSize(inputElement.size).buttonRaw()
  of INPUT_SUBMIT:
    return inputElement.value.buttonRaw()
  else: discard

#TODO
func ancestor*(htmlNode: Node, tagType: TagType): HtmlElement =
  result = HtmlElement(htmlNode.parentElement)
  while result != nil and result.tagType != tagType:
    result = HtmlElement(result.parentElement)

proc getRawText*(htmlNode: Node): string =
  if htmlNode.isElemNode():
    case HtmlElement(htmlNode).tagType
    of TAG_INPUT: return HtmlInputElement(htmlNode).getRawInput()
    else: return ""
  elif htmlNode.isTextNode():
    let chardata = CharacterData(htmlNode)
    #eprint "char data", chardata.data
    if htmlNode.parentElement != nil and htmlNode.parentElement.tagType != TAG_PRE:
      result = chardata.data.remove("\n")
      #if unicode.strip(result).runeLen() > 0:
      #  if htmlNode.getStyle().display != DISPLAY_INLINE:
      #    result = unicode.strip(result)
      #else:
      #  result = ""
    else:
      result = unicode.strip(chardata.data)
    if htmlNode.parentElement != nil and htmlNode.parentElement.tagType == TAG_OPTION:
      result = result.buttonRaw()
  else:
    assert(false)

func getFmtText*(node: Node): seq[string] =
  if node.isElemNode():
    case HtmlElement(node).tagType
    of TAG_INPUT: return HtmlInputElement(node).getFmtInput()
    else: return @[]
  elif node.isTextNode():
    let chardata = CharacterData(node)
    result &= chardata.data
    if node.parentElement != nil:
      let style = node.getStyle()
      if style.hasColor():
        result = result.ansiFgColor(style.termColor())

      if node.parentElement.tagType == TAG_OPTION:
        result = result.ansiFgColor(fgRed).ansiReset()

      if style.bold:
        result = result.ansiStyle(styleBright).ansiReset()
      if style.fontStyle == FONTSTYLE_ITALIC or style.fontStyle == FONTSTYLE_OBLIQUE:
        result = result.ansiStyle(styleItalic).ansiReset()
      if style.underscore:
        result = result.ansiStyle(styleUnderscore).ansiReset()
    else:
      assert(false, node.rawtext)
  else:
    assert(false)

func newText*(): Text =
  new(result)
  result.nodeType = TEXT_NODE

func newComment*(): Comment =
  new(result)
  result.nodeType = COMMENT_NODE

func newHtmlElement*(tagType: TagType): HTMLElement =
  case tagType
  of TAG_INPUT:
    result = new(HTMLInputElement)
  of TAG_A:
    result = new(HTMLAnchorElement)
  of TAG_SELECT:
    result = new(HTMLSelectElement)
  of TAG_OPTION:
    result = new(HTMLOptionElement)
  of TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6:
    result = new(HTMLHeadingElement)
  of TAG_BR:
    result = new(HTMLBRElement)
  of TAG_SPAN:
    result = new(HTMLSpanElement)
  else:
    new(result)

  result.nodeType = ELEMENT_NODE
  result.tagType = tagType
  result.style = CSS2Properties()

func newDocument*(): Document =
  new(result)
  result.head = newHtmlElement(TAG_HEAD)
  result.body = newHtmlElement(TAG_BODY)
  result.nodeType = DOCUMENT_NODE

func newAttr*(parent: Element, key: string, value: string): Attr =
  new(result)
  result.nodeType = ATTRIBUTE_NODE
  result.ownerElement = parent
  result.name = key
  result.value = value

func getAttrValue*(element: Element, s: string): string =
  let attr = element.attributes.getOrDefault(s, nil)
  if attr != nil:
    return attr.value
  return ""

#TODO case sensitivity
func attrSelectorMatches(elem: Element, sel: Selector): bool =
  case sel.rel
  of ' ': return sel.attr in elem.attributes
  of '=': return elem.getAttrValue(sel.attr) == sel.value
  of '~': return sel.value in unicode.split(elem.getAttrValue(sel.attr))
  of '|':
    let val = elem.getAttrValue(sel.attr)
    return val == sel.value or sel.value.startsWith(val & '-')
  of '^': return elem.getAttrValue(sel.attr).startsWith(sel.value)
  of '$': return elem.getAttrValue(sel.attr).endsWith(sel.value)
  of '*': return elem.getAttrValue(sel.attr).contains(sel.value)
  else: return false

func pseudoSelectorMatches(elem: Element, sel: Selector): bool =
  case sel.pseudo
  of "first-child": return elem.parentNode.firstElementChild == elem
  of "last-child": return elem.parentNode.lastElementChild == elem
  else: return false

func pseudoElemSelectorMatches(elem: Element, sel: Selector): bool =
  case sel.elem
  of "after": return false
  of "before": return false
  else: return false

func selectorMatches(elem: Element, sel: Selector): bool =
  case sel.t
  of TYPE_SELECTOR:
    return elem.tagType == sel.tag
  of CLASS_SELECTOR:
    return sel.class in elem.classList
  of ID_SELECTOR:
    return sel.id == elem.id
  of ATTR_SELECTOR:
    return elem.attrSelectorMatches(sel)
  of PSEUDO_SELECTOR:
    return pseudoSelectorMatches(elem, sel)
  of PSELEM_SELECTOR:
    return pseudoElemSelectorMatches(elem, sel)
  of UNIVERSAL_SELECTOR:
    return true
  of FUNC_SELECTOR:
    return false

func selectorsMatch(elem: Element, selectors: SelectorList): bool =
  for sel in selectors.sels:
    if not selectorMatches(elem, sel):
      return false
  return true

func selectElems(document: Document, sel: Selector): seq[Element] =
  case sel.t
  of TYPE_SELECTOR:
    return document.type_elements[sel.tag]
  of ID_SELECTOR:
    return document.id_elements[sel.id]
  of CLASS_SELECTOR:
    return document.class_elements[sel.class]
  of UNIVERSAL_SELECTOR:
    return document.all_elements
  of ATTR_SELECTOR:
    return document.all_elements.filter((elem) => attrSelectorMatches(elem, sel))
  of PSEUDO_SELECTOR:
    return document.all_elements.filter((elem) => pseudoSelectorMatches(elem, sel))
  of PSELEM_SELECTOR:
    return document.all_elements.filter((elem) => pseudoElemSelectorMatches(elem, sel))
  of FUNC_SELECTOR:
    case sel.name
    of "not":
      return document.all_elements.filter((elem) => not selectorsMatch(elem, sel.selectors))
    of "is", "where":
      return document.all_elements.filter((elem) => selectorsMatch(elem, sel.selectors))
    return newSeq[Element]()

func selectElems(document: Document, selectors: SelectorList): seq[Element] =
  assert(selectors.len > 0)
  let sellist = optimizeSelectorList(selectors)
  result = document.selectElems(selectors[0])
  var i = 1

  while i < sellist.len:
    if sellist[i].t == FUNC_SELECTOR:
      case sellist[i].name
      of "not":
        result = result.filter((elem) => not selectorsMatch(elem, sellist[i].selectors))
      of "is", "where":
        result = result.filter((elem) => selectorsMatch(elem, sellist[i].selectors))
      else: discard
    else:
      result = result.filter((elem) => selectorMatches(elem, sellist[i]))
    inc i

proc querySelector*(document: Document, q: string): seq[Element] =
  let ss = newStringStream(q)
  let cvals = parseCSSListOfComponentValues(ss)
  let selectors = parseSelectors(cvals)

  for sel in selectors:
    result.add(document.selectElems(sel))

func calcRules(elem: Element, rules: CSSStylesheet): seq[CSSSimpleBlock] =
  var tosort: seq[tuple[s:int,b:CSSSimpleBlock]]
  for rule in rules.value:
    let selectors = parseSelectors(rule.prelude) #TODO perf: compute this once
    for sel in selectors:
      if elem.selectorsMatch(sel):
        let spec = getSpecificity(sel)
        tosort.add((spec,rule.oblock))

  tosort.sort((x, y) => cmp(x.s,y.s))
  return tosort.map((x) => x.b)

proc applyRules*(document: Document, rules: CSSStylesheet): seq[tuple[e:Element,d:CSSDeclaration]] =
  var stack: seq[Element]

  stack.add(document.firstElementChild)
  while stack.len > 0:
    let elem = stack.pop()
    for oblock in calcRules(elem, rules):
      let decls = parseCSSListOfDeclarations(oblock.value)
      for item in decls:
        if item of CSSDeclaration:
          if ((CSSDeclaration)item).important:
            result.add((elem, (CSSDeclaration)item))
          else:
            elem.style.applyProperty((CSSDeclaration)item)

    for child in elem.children:
      stack.add(child)

proc addBoxes*(elem: Element) =
  var b = false
  for child in elem.childNodes:
    if child.nodeType == ELEMENT_NODE:
      if ((Element)child).style.display == DISPLAY_BLOCK:
        b = true
        break
  for child in elem.childNodes:
    if child.nodeType == ELEMENT_NODE:
      if b:
        child.box = CSSBox(display: DISPLAY_BLOCK)
      else:
        child.box = CSSBox()

proc generateBoxModel(document: Document) =
  var stack: seq[Element]

  stack.add(document.firstElementChild)
  document.firstElementChild.box = CSSBox()
  while stack.len > 0:
    let elem = stack.pop()
    elem.addBoxes()
    for child in elem.children:
      stack.add(child)

proc applyDefaultStylesheet*(document: Document) =
  let important = document.applyRules(stylesheet)
  for rule in important:
    rule.e.style.applyProperty(rule.d)
    rule.e.cssvalues.add(getComputedValue(rule.d))

  document.generateBoxModel()