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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
# Tree building.
#
#TODO: this is currently a separate pass from layout, meaning at least
# two tree traversals are required. Ideally, these should be collapsed
# into a single pass, reusing parts of previous layout passes when
# possible.
#
# ---
#
# This wouldn't be nearly as complex as it is if not for CSS's asinine
# anonymous table box generation rules. In particular:
# * Runs of misparented boxes inside a table/table row/table row group
# must be placed in appropriate anonymous wrappers. For example, if
# we encounter two consecutive `display: block's inside a `display:
# table-row', these must be wrapped around a single `display:
# table-cell'.
# * Runs of misparented table row/table row group/table cell boxes must
# be wrapped in an anonymous table, or in some cases an anonymous
# table row and *then* an anonymous table. e.g. a `display:
# table-row', `display: table-cell', then a `display: table-row-group'
# will all be wrapped in a single table.
# * If this weren't enough, we also have to *split up* the entire table
# into an inner and an outer table. The outer table wraps the inner
# table and the caption. The inner table (of DisplayTableWrapper)
# includes the rows/row groups.
# Whatever your reason may be for looking at this: good luck.
import chame/tags
import css/box
import css/cascade
import css/cssvalues
import css/lunit
import css/selectorparser
import html/catom
import html/dom
import types/bitmap
import types/color
import types/refstring
import utils/twtstr
type
StyledNodeType = enum
stElement, stText, stImage, stBr, stCounter
# Abstraction over the DOM to pretend that elements, text, replaced
# and pseudo-elements are derived from the same type.
StyledNode = object
element: Element
computed: CSSValues
pseudo: PseudoElement
skipChildren: bool
case t: StyledNodeType
of stText:
text: RefString
of stElement:
anonChildren: seq[StyledNode]
of stImage:
bmp: NetworkBitmap
of stBr: # <br> element
discard
of stCounter: # counters
counterName: CAtom
counterStyle: CSSListStyleType
counterSuffix: bool
CSSCounter = object
element: Element
name: CAtom
n: int32
TreeContext = object
markLinks: bool
quoteLevel: int
counters: seq[CSSCounter]
rootProperties: CSSValues
TreeFrame = object
parent: Element
computed: CSSValues
children: seq[StyledNode]
lastChildWasInline: bool
captionSeen: bool
anonComputed: CSSValues
anonInlineComputed: CSSValues
pctx: ptr TreeContext
# Forward declarations
proc build(ctx: var TreeContext; cached: CSSBox; styledNode: StyledNode): CSSBox
template ctx(frame: TreeFrame): var TreeContext =
frame.pctx[]
when defined(debug):
func `$`*(node: StyledNode): string =
case node.t
of stText:
return node.text
of stElement:
if node.pseudo != peNone:
return $node.element.tagType & "::" & $node.pseudo
return $node.element
of stImage:
return "#image"
of stBr:
return "#br"
of stCounter:
return "#counter"
iterator mritems(counters: var seq[CSSCounter]): var CSSCounter =
for i in countdown(counters.high, 0):
yield counters[i]
proc incCounter(ctx: var TreeContext; name: CAtom; n: int32; element: Element) =
var found = false
for counter in ctx.counters.mritems:
if counter.name == name:
let n64 = clamp(int64(counter.n) + int64(n), int32.low, int32.high)
counter.n = int32(n64)
found = true
break
if not found: # instantiate a new counter
ctx.counters.add(CSSCounter(name: name, n: n, element: element))
proc setCounter(ctx: var TreeContext; name: CAtom; n: int32; element: Element) =
var found = false
for counter in ctx.counters.mritems:
if counter.name == name:
counter.n = n
found = true
break
if not found: # instantiate a new counter
ctx.counters.add(CSSCounter(name: name, n: n, element: element))
proc resetCounter(ctx: var TreeContext; name: CAtom; n: int32;
element: Element) =
var found = false
for counter in ctx.counters.mritems:
if counter.name == name and
counter.element.parentNode == element.parentNode and
counter.element.elIndex <= element.elIndex:
counter.element = element
counter.n = n
found = true
break
if not found:
ctx.counters.add(CSSCounter(name: name, n: n, element: element))
proc counter(ctx: var TreeContext; name: CAtom): int32 =
for counter in ctx.counters.mritems:
if counter.name == name:
return counter.n
return 0
func inheritFor(frame: TreeFrame; display: CSSDisplay): CSSValues =
result = frame.computed.inheritProperties()
result{"display"} = display
proc initTreeFrame(ctx: var TreeContext; parent: Element; computed: CSSValues):
TreeFrame =
result = TreeFrame(parent: parent, computed: computed, pctx: addr ctx)
proc getAnonInlineComputed(frame: var TreeFrame): CSSValues =
if frame.anonInlineComputed == nil:
if frame.computed{"display"} == DisplayInline:
frame.anonInlineComputed = frame.computed
else:
frame.anonInlineComputed = frame.computed.inheritProperties()
return frame.anonInlineComputed
proc displayed(frame: TreeFrame; text: RefString): bool =
if text.len == 0:
return false
return frame.computed{"display"} == DisplayInline or
frame.lastChildWasInline or
frame.computed{"white-space"} in WhiteSpacePreserve or
not text.onlyWhitespace()
#TODO implement table columns
const DisplayNoneLike = {
DisplayNone, DisplayTableColumn, DisplayTableColumnGroup
}
proc displayed(frame: TreeFrame; element: Element): bool =
return element.computed{"display"} notin DisplayNoneLike
proc initStyledAnon(element: Element; computed: CSSValues;
children: sink seq[StyledNode] = @[]): StyledNode =
result = StyledNode(
t: stElement,
element: element,
anonChildren: children,
computed: computed,
skipChildren: true
)
proc getInternalTableParent(frame: var TreeFrame; display: CSSDisplay):
var seq[StyledNode] =
if frame.anonComputed == nil:
frame.anonComputed = frame.inheritFor(display)
frame.children.add(initStyledAnon(frame.parent, frame.anonComputed))
return frame.children[^1].anonChildren
# Add an anonymous table to children, and return based on display either
# * row, row group: the table children
# * cell: its last anonymous row (if there isn't one, create it)
# * caption: its outer box
proc addAnonTable(frame: var TreeFrame; parentDisplay, display: CSSDisplay):
var seq[StyledNode] =
if frame.anonComputed == nil or
frame.anonComputed{"display"} notin DisplayInnerTable + {DisplayTableRow}:
let anonDisplay = if parentDisplay == DisplayInline:
DisplayInlineTable
else:
DisplayTable
let (outer, inner) = frame.inheritFor(anonDisplay).splitTable()
frame.anonComputed = outer
frame.children.add(initStyledAnon(frame.parent, outer, @[initStyledAnon(
frame.parent,
inner
)]))
if display == DisplayTableCaption:
frame.anonComputed = frame.children[^1].computed
return frame.children[^1].anonChildren
if display in RowGroupBox + {DisplayTableRow}:
frame.anonComputed = frame.children[^1].computed
return frame.children[^1].anonChildren[0].anonChildren
assert display == DisplayTableCell
if frame.anonComputed{"display"} == DisplayTableRow:
return frame.children[^1].anonChildren[0].anonChildren[^1].anonChildren
frame.anonComputed = frame.inheritFor(DisplayTableRow)
frame.children[^1].anonChildren[0].anonChildren.add(initStyledAnon(
frame.parent,
frame.anonComputed
))
return frame.children[^1].anonChildren[0].anonChildren[^1].anonChildren
proc getParent(frame: var TreeFrame; computed: CSSValues; display: CSSDisplay):
var seq[StyledNode] =
let parentDisplay = frame.computed{"display"}
case parentDisplay
of DisplayInnerFlex, DisplayInnerGrid:
if display in DisplayOuterInline:
if frame.anonComputed == nil:
frame.anonComputed = frame.inheritFor(DisplayBlock)
frame.children.add(initStyledAnon(frame.parent, frame.anonComputed))
return frame.children[^1].anonChildren
of DisplayTableRow:
if display != DisplayTableCell:
return frame.getInternalTableParent(DisplayTableCell)
frame.anonComputed = nil
of RowGroupBox:
if display != DisplayTableRow:
return frame.getInternalTableParent(DisplayTableRow)
frame.anonComputed = nil
of DisplayTableWrapper:
if display notin RowGroupBox + {DisplayTableRow}:
return frame.getInternalTableParent(DisplayTableRow)
frame.anonComputed = nil
of DisplayInnerTable:
if frame.children.len > 0 and display != DisplayTableCaption:
return frame.children[0].anonChildren
of DisplayListItem:
if frame.computed{"list-style-position"} == ListStylePositionOutside and
frame.children.len >= 2:
return frame.children[1].anonChildren
of DisplayTableCell:
if frame.anonComputed == nil:
frame.anonComputed = frame.inheritFor(DisplayFlowRoot)
frame.children.add(initStyledAnon(frame.parent, frame.anonComputed))
return frame.children[^1].anonChildren
elif display in DisplayInternalTable:
return frame.addAnonTable(parentDisplay, display)
else:
frame.captionSeen = false
frame.anonComputed = nil
return frame.children
proc addListItem(frame: var TreeFrame; node: sink StyledNode) =
var node = node
# Generate a marker box.
let computed = node.computed.inheritProperties()
computed{"white-space"} = WhitespacePre
let markerText = StyledNode(
t: stCounter,
element: node.element,
computed: computed,
counterName: satListItem.toAtom(),
counterStyle: node.computed{"list-style-type"},
counterSuffix: true
)
case node.computed{"list-style-position"}
of ListStylePositionOutside:
# Generate separate boxes for the content and marker.
let computed = node.computed.inheritProperties()
computed{"display"} = DisplayBlock
node.anonChildren.add(initStyledAnon(node.element, computed, @[markerText]))
node.anonChildren.add(initStyledAnon(node.element, computed))
of ListStylePositionInside:
node.anonChildren.add(markerText)
frame.getParent(node.computed, node.computed{"display"}).add(node)
proc addTable(frame: var TreeFrame; node: sink StyledNode) =
var node = node
let (outer, inner) = node.computed.splitTable()
node.computed = outer
node.anonChildren.add(initStyledAnon(node.element, inner))
frame.getParent(node.computed, node.computed{"display"}).add(node)
proc add(frame: var TreeFrame; node: sink StyledNode) =
let display = node.computed{"display"}
if frame.captionSeen and display == DisplayTableCaption:
return
if node.t == stElement and node.anonChildren.len == 0:
case display
of DisplayListItem:
frame.addListItem(node)
frame.lastChildWasInline = false
return # already added
of DisplayInnerTable:
frame.addTable(node)
frame.lastChildWasInline = false
return # already added
else: discard
frame.getParent(node.computed, display).add(node)
frame.lastChildWasInline = display in DisplayOuterInline
frame.captionSeen = frame.captionSeen or display == DisplayTableCaption
proc addAnon(frame: var TreeFrame; computed: CSSValues;
children: sink seq[StyledNode]) =
frame.add(initStyledAnon(frame.parent, computed, children))
proc addElement(frame: var TreeFrame; element: Element) =
if element.computed == nil:
element.applyStyle()
if frame.displayed(element):
frame.add(StyledNode(
t: stElement,
element: element,
computed: element.computed
))
proc addPseudo(frame: var TreeFrame; pseudo: PseudoElement) =
let computed = frame.parent.getComputedStyle(pseudo)
if computed != nil and computed{"display"} notin DisplayNoneLike and
computed{"content"}.len > 0:
frame.add(StyledNode(
t: stElement,
pseudo: pseudo,
element: frame.parent,
computed: computed
))
proc addText(frame: var TreeFrame; text: RefString) =
if frame.displayed(text):
frame.add(StyledNode(
t: stText,
element: frame.parent,
text: text,
computed: frame.getAnonInlineComputed()
))
proc addCounter(frame: var TreeFrame; name: CAtom; style: CSSListStyleType) =
frame.add(StyledNode(
t: stCounter,
element: frame.parent,
counterName: name,
counterStyle: style,
computed: frame.getAnonInlineComputed()
))
proc addText(frame: var TreeFrame; s: sink string) =
#TODO should probably cache these...
frame.addText(newRefString(s))
proc addImage(frame: var TreeFrame; bmp: NetworkBitmap) =
if bmp != nil and bmp.cacheId != -1:
frame.add(StyledNode(
t: stImage,
element: frame.parent,
bmp: bmp,
computed: frame.getAnonInlineComputed()
))
else:
frame.addText("[img]")
proc addBr(frame: var TreeFrame) =
frame.add(StyledNode(
t: stBr,
element: frame.parent,
computed: frame.computed
))
proc addElementChildren(frame: var TreeFrame) =
for it in frame.parent.childList:
if it of Element:
let element = Element(it)
frame.addElement(element)
elif it of Text:
#TODO collapse subsequent text nodes into one StyledNode
# (it isn't possible in HTML, only with JS DOM manipulation)
let text = Text(it)
frame.addText(text.data)
proc addOptionChildren(frame: var TreeFrame; option: HTMLOptionElement) =
if option.select != nil and option.select.attrb(satMultiple):
frame.addText("[")
let cdata = newRefString(if option.selected: "*" else: " ")
let computed = option.computed.inheritProperties()
computed{"color"} = cssColor(ANSIColor(1)) # red
computed{"white-space"} = WhitespacePre
block anon:
var aframe = frame.ctx.initTreeFrame(option, computed)
aframe.addText(cdata)
frame.addAnon(computed, move(aframe.children))
frame.addText("]")
frame.addElementChildren()
proc addAnchorChildren(frame: var TreeFrame) =
if frame.ctx.markLinks:
frame.addPseudo(peLinkMarker)
frame.addElementChildren()
proc addChildren(frame: var TreeFrame) =
case frame.parent.tagType
of TAG_INPUT:
let cdata = HTMLInputElement(frame.parent).inputString()
if cdata != nil:
frame.addText(cdata)
of TAG_TEXTAREA:
#TODO cache (do the same as with input, and add borders in render)
frame.addText(HTMLTextAreaElement(frame.parent).textAreaString())
of TAG_IMG: frame.addImage(HTMLImageElement(frame.parent).bitmap)
of TAG_CANVAS: frame.addImage(HTMLCanvasElement(frame.parent).bitmap)
of TAG_VIDEO: frame.addText("[video]")
of TAG_AUDIO: frame.addText("[audio]")
of TAG_BR: frame.addBr()
of TAG_IFRAME: frame.addText("[iframe]")
of TAG_FRAME: frame.addText("[frame]")
of TAG_OPTION:
let option = HTMLOptionElement(frame.parent)
frame.addOptionChildren(option)
of TAG_A:
frame.addAnchorChildren()
elif frame.parent.tagType(Namespace.SVG) == TAG_SVG:
frame.addImage(SVGSVGElement(frame.parent).bitmap)
else:
frame.addElementChildren()
proc addContent(frame: var TreeFrame; content: CSSContent) =
case content.t
of ContentString:
frame.addText(content.s)
of ContentOpenQuote:
let quotes = frame.computed{"quotes"}
if quotes == nil:
frame.addText(quoteStart(frame.ctx.quoteLevel))
elif quotes.qs.len > 0:
frame.addText(quotes.qs[min(frame.ctx.quoteLevel, quotes.qs.high)].s)
else:
return
inc frame.ctx.quoteLevel
of ContentCloseQuote:
if frame.ctx.quoteLevel > 0:
dec frame.ctx.quoteLevel
let quotes = frame.computed{"quotes"}
if quotes == nil:
frame.addText(quoteEnd(frame.ctx.quoteLevel))
elif quotes.qs.len > 0:
frame.addText(quotes.qs[min(frame.ctx.quoteLevel, quotes.qs.high)].e)
of ContentNoOpenQuote:
inc frame.ctx.quoteLevel
of ContentNoCloseQuote:
if frame.ctx.quoteLevel > 0:
dec frame.ctx.quoteLevel
of ContentCounter:
frame.addCounter(content.counter, content.counterStyle)
proc buildChildren(frame: var TreeFrame; styledNode: StyledNode) =
for child in styledNode.anonChildren:
frame.add(child)
if not styledNode.skipChildren:
if styledNode.pseudo == peNone:
frame.addPseudo(peBefore)
frame.addChildren()
frame.addPseudo(peAfter)
else:
for content in frame.computed{"content"}:
frame.addContent(content)
proc buildBox(ctx: var TreeContext; frame: TreeFrame; cached: CSSBox): CSSBox =
let display = frame.computed{"display"}
let box = if display == DisplayInline:
InlineBox(computed: frame.computed, element: frame.parent)
else:
BlockBox(computed: frame.computed, element: frame.parent)
var last: CSSBox = nil
for child in frame.children:
let childBox = ctx.build(nil, child)
childBox.parent = box
if last != nil:
last.next = childBox
else:
box.firstChild = childBox
last = childBox
if display in DisplayInlineBlockLike:
let wrapper = InlineBlockBox(
computed: ctx.rootProperties,
element: frame.parent,
firstChild: box
)
box.parent = wrapper
return wrapper
return box
proc applyCounters(ctx: var TreeContext; styledNode: StyledNode) =
for counter in styledNode.computed{"counter-reset"}:
ctx.resetCounter(counter.name, counter.num, styledNode.element)
var liSeen = false
for counter in styledNode.computed{"counter-increment"}:
liSeen = counter.name == satListItem.toAtom()
ctx.incCounter(counter.name, counter.num, styledNode.element)
if not liSeen and styledNode.computed{"display"} == DisplayListItem:
ctx.incCounter(satListItem.toAtom(), 1, styledNode.element)
for counter in styledNode.computed{"counter-set"}:
ctx.setCounter(counter.name, counter.num, styledNode.element)
proc build(ctx: var TreeContext; cached: CSSBox; styledNode: StyledNode):
CSSBox =
case styledNode.t
of stElement:
ctx.applyCounters(styledNode)
let countersLen = ctx.counters.len
var frame = ctx.initTreeFrame(styledNode.element, styledNode.computed)
frame.buildChildren(styledNode)
let box = ctx.buildBox(frame, cached)
ctx.counters.setLen(countersLen)
return box
of stText:
return InlineTextBox(
computed: styledNode.computed,
element: styledNode.element,
text: styledNode.text
)
of stBr:
return InlineNewLineBox(
computed: styledNode.computed,
element: styledNode.element
)
of stCounter:
let counter = ctx.counter(styledNode.counterName)
let addSuffix = styledNode.counterSuffix # only used for markers
return InlineTextBox(
computed: styledNode.computed,
element: styledNode.element,
text: styledNode.counterStyle.listMarker(counter, addSuffix)
)
of stImage:
return InlineImageBox(
computed: styledNode.computed,
element: styledNode.element,
bmp: styledNode.bmp
)
# Root
proc buildTree*(element: Element; cached: CSSBox; markLinks: bool): BlockBox =
if element.computed == nil:
element.applyStyle()
let styledNode = StyledNode(
t: stElement,
element: element,
computed: element.computed
)
var ctx = TreeContext(
rootProperties: rootProperties(),
markLinks: markLinks
)
return BlockBox(ctx.build(cached, styledNode))
|