about summary refs log tree commit diff stats
path: root/src/css/cascade.nim
blob: 448c1df19f0d09cbdecfd0059cc29643c22603b7 (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
import algorithm
import options
import strutils

import css/cssparser
import css/match
import css/mediaquery
import css/selectorparser
import css/sheet
import css/stylednode
import css/values
import html/dom
import html/enums
import layout/layoutunit
import types/color
import types/opt

import chame/tags

type
  DeclarationList* = array[PseudoElem, seq[CSSDeclaration]]

  DeclarationListMap* = object
    ua: DeclarationList # user agent
    user: DeclarationList
    author: seq[DeclarationList]

func appliesLR(feature: MediaFeature, window: Window,
    n: LayoutUnit): bool =
  let a = px(feature.lengthrange.a, window.attrs, 0)
  let b = px(feature.lengthrange.b, window.attrs, 0)
  if not feature.lengthaeq and a == n:
    return false
  if a > n:
    return false
  if not feature.lengthbeq and b == n:
    return false
  if b < n:
    return false
  return true

func applies(feature: MediaFeature, window: Window): bool =
  case feature.t
  of FEATURE_COLOR:
    return 8 in feature.range
  of FEATURE_GRID:
    return feature.b
  of FEATURE_HOVER:
    return feature.b
  of FEATURE_PREFERS_COLOR_SCHEME:
    return feature.b
  of FEATURE_WIDTH:
    return feature.appliesLR(window, toLayoutUnit(window.attrs.width_px))
  of FEATURE_HEIGHT:
    return feature.appliesLR(window, toLayoutUnit(window.attrs.height_px))

func applies(mq: MediaQuery, window: Window): bool =
  case mq.t
  of CONDITION_MEDIA:
    case mq.media
    of MEDIA_TYPE_ALL: return true
    of MEDIA_TYPE_PRINT: return false
    of MEDIA_TYPE_SCREEN: return true
    of MEDIA_TYPE_SPEECH: return false
    of MEDIA_TYPE_TTY: return true
    of MEDIA_TYPE_UNKNOWN: return false
  of CONDITION_NOT:
    return not mq.n.applies(window)
  of CONDITION_AND:
    return mq.anda.applies(window) and mq.andb.applies(window)
  of CONDITION_OR:
    return mq.ora.applies(window) or mq.orb.applies(window)
  of CONDITION_FEATURE:
    return mq.feature.applies(window)

func applies*(mqlist: MediaQueryList, window: Window): bool =
  for mq in mqlist:
    if mq.applies(window):
      return true
  return false

type
  ToSorts = array[PseudoElem, seq[(int, seq[CSSDeclaration])]]

proc calcRule(tosorts: var ToSorts, styledNode: StyledNode, rule: CSSRuleDef) =
  for sel in rule.sels:
    if styledNode.selectorsMatch(sel):
      let spec = getSpecificity(sel)
      tosorts[sel.pseudo].add((spec, rule.decls))

func calcRules(styledNode: StyledNode, sheet: CSSStylesheet): DeclarationList =
  var tosorts: ToSorts
  let elem = Element(styledNode.node)
  for rule in sheet.gen_rules(elem.tagType, elem.id, elem.classList.toks):
    tosorts.calcRule(styledNode, rule)
  for i in PseudoElem:
    tosorts[i].sort((proc(x, y: (int, seq[CSSDeclaration])): int =
      cmp(x[0], y[0])
    ), order = Ascending)
    result[i] = newSeqOfCap[CSSDeclaration](tosorts[i].len)
    for item in tosorts[i]:
      result[i].add(item[1])

func calcPresentationalHints(element: Element): CSSComputedValues =
  template set_cv(a, b: untyped) =
    if result == nil:
      new(result)
    result{a} = b
  template map_width =
    let s = parseDimensionValues(element.attr("width"))
    if s.isSome:
      set_cv "width", s.get
  template map_height =
    let s = parseDimensionValues(element.attr("height"))
    if s.isSome:
      set_cv "height", s.get
  template map_width_nozero =
    let s = parseDimensionValues(element.attr("width"))
    if s.isSome and s.get.num != 0:
      set_cv "width", s.get
  template map_height_nozero =
    let s = parseDimensionValues(element.attr("height"))
    if s.isSome and s.get.num != 0:
      set_cv "height", s.get
  template map_bgcolor =
    let s = element.attr("bgcolor")
    if s != "":
      let c = parseLegacyColor(s)
      if c.isSome:
        set_cv "background-color", c.get
  template map_size =
    let s = element.attrul("size")
    if s.isSome:
      set_cv "width", CSSLength(num: float64(s.get), unit: UNIT_CH)
  template map_valign =
    case element.attr("valign").toLowerAscii()
    of "top": set_cv "vertical-align", CSSVerticalAlign(keyword: VERTICAL_ALIGN_TOP)
    of "middle": set_cv "vertical-align", CSSVerticalAlign(keyword: VERTICAL_ALIGN_MIDDLE)
    of "bottom": set_cv "vertical-align", CSSVerticalAlign(keyword: VERTICAL_ALIGN_BOTTOM)
    of "baseline": set_cv "vertical-align", CSSVerticalAlign(keyword: VERTICAL_ALIGN_BASELINE)
  template map_align =
    case element.attr("align").toLowerAscii()
    of "center", "middle": set_cv "text-align", TEXT_ALIGN_CHA_CENTER
    of "left": set_cv "text-align", TEXT_ALIGN_CHA_LEFT
    of "right": set_cv "text-align", TEXT_ALIGN_CHA_RIGHT
  template map_table_align =
    case element.attr("align").toLowerAscii()
    of "left":
     set_cv "float", FLOAT_LEFT
    of "right":
      set_cv "float", FLOAT_RIGHT
    of "center":
      set_cv "margin-left", CSSLengthAuto #TODO should be inline-start
      set_cv "margin-right", CSSLengthAuto #TODO should be inline-end
  template map_text =
    let s = element.attr("text")
    if s != "":
      let c = parseLegacyColor(s)
      if c.isSome:
        set_cv "color", c.get
  template map_color =
    let s = element.attr("color")
    if s != "":
      let c = parseLegacyColor(s)
      if c.isSome:
        set_cv "color", c.get
  template map_colspan =
    let colspan = element.attrulgz("colspan")
    if colspan.isSome:
      let i = colspan.get
      if i <= 1000:
        set_cv "-cha-colspan", int(i)
  template map_rowspan =
    let rowspan = element.attrul("rowspan")
    if rowspan.isSome:
      let i = rowspan.get
      if i <= 65534:
        set_cv "-cha-rowspan", int(i)
  template map_list_type_ol =
    let ctype = element.attr("type")
    if ctype.len > 0:
      case ctype[0]
      of '1': set_cv "list-style-type", LIST_STYLE_TYPE_DECIMAL
      of 'a': set_cv "list-style-type", LIST_STYLE_TYPE_LOWER_ALPHA
      of 'A': set_cv "list-style-type", LIST_STYLE_TYPE_UPPER_ALPHA
      of 'i': set_cv "list-style-type", LIST_STYLE_TYPE_LOWER_ROMAN
      of 'I': set_cv "list-style-type", LIST_STYLE_TYPE_UPPER_ROMAN
      else: discard
  template map_list_type_ul =
    let ctype = element.attr("type")
    if ctype.len > 0:
      case ctype.toLowerAscii()
      of "none": set_cv "list-style-type", LIST_STYLE_TYPE_NONE
      of "disc": set_cv "list-style-type", LIST_STYLE_TYPE_DISC
      of "circle": set_cv "list-style-type", LIST_STYLE_TYPE_CIRCLE
      of "square": set_cv "list-style-type", LIST_STYLE_TYPE_SQUARE

  case element.tagType
  of TAG_DIV:
    map_align
  of TAG_TABLE:
    map_height_nozero
    map_width_nozero
    map_bgcolor
    map_table_align
  of TAG_TD, TAG_TH:
    map_height_nozero
    map_width_nozero
    map_bgcolor
    map_valign
    map_align
    map_colspan
    map_rowspan
  of TAG_THEAD, TAG_TBODY, TAG_TFOOT, TAG_TR:
    map_height
    map_bgcolor
    map_valign
    map_align
  of TAG_COL:
    map_width
  of TAG_IMG, TAG_CANVAS:
    map_width
    map_height
  of TAG_BODY:
    map_bgcolor
    map_text
  of TAG_TEXTAREA:
    let textarea = HTMLTextAreaElement(element)
    let cols = textarea.attrul("cols").get(20)
    let rows = textarea.attrul("rows").get(1)
    set_cv "width", CSSLength(unit: UNIT_CH, num: float64(cols))
    set_cv "height", CSSLength(unit: UNIT_EM, num: float64(rows))
  of TAG_FONT:
    map_color
  of TAG_INPUT:
    let input = HTMLInputElement(element)
    if input.inputType in InputTypeWithSize:
      map_size
  of TAG_OL:
    map_list_type_ol
  of TAG_UL:
    map_list_type_ul
  else: discard

proc applyDeclarations(styledNode: StyledNode, parent: CSSComputedValues,
    map: DeclarationListMap) =
  let pseudo = PSEUDO_NONE
  var builder = newComputedValueBuilder(parent)

  builder.addValues(map.ua[pseudo], ORIGIN_USER_AGENT)
  builder.addValues(map.user[pseudo], ORIGIN_USER)
  for rule in map.author:
    builder.addValues(rule[pseudo], ORIGIN_AUTHOR)
  if styledNode.node != nil:
    let element = Element(styledNode.node)
    let style = element.style_cached
    if style != nil:
      builder.addValues(style.decls, ORIGIN_AUTHOR)
    builder.preshints = element.calcPresentationalHints()

  styledNode.computed = builder.buildComputedValues()

# Either returns a new styled node or nil.
proc applyDeclarations(pseudo: PseudoElem, styledParent: StyledNode,
    map: DeclarationListMap): StyledNode =
  var builder = newComputedValueBuilder(styledParent.computed)

  builder.addValues(map.ua[pseudo], ORIGIN_USER_AGENT)
  builder.addValues(map.user[pseudo], ORIGIN_USER)
  for rule in map.author:
    builder.addValues(rule[pseudo], ORIGIN_AUTHOR)

  if builder.hasValues():
    let cvals = builder.buildComputedValues()
    result = styledParent.newStyledElement(pseudo, cvals)

func applyMediaQuery(ss: CSSStylesheet, window: Window): CSSStylesheet =
  if ss == nil: return nil
  result = ss
  for mq in ss.mq_list:
    if mq.query.applies(window):
      result.add(mq.children.applyMediaQuery(window))

func calcRules(styledNode: StyledNode, ua, user: CSSStylesheet,
    author: seq[CSSStylesheet]): DeclarationListMap =
  let uadecls = calcRules(styledNode, ua)
  var userdecls: DeclarationList
  if user != nil:
    userdecls = calcRules(styledNode, user)
  var authordecls: seq[DeclarationList]
  for rule in author:
    authordecls.add(calcRules(styledNode, rule))
  return DeclarationListMap(
    ua: uadecls,
    user: userdecls,
    author: authordecls
  )

proc applyStyle(parent, styledNode: StyledNode, map: DeclarationListMap) =
  let parentComputed = if parent != nil:
    parent.computed
  else:
    rootProperties()
  styledNode.applyDeclarations(parentComputed, map)

type CascadeLevel = tuple[
  styledParent: StyledNode,
  child: Node,
  pseudo: PseudoElem,
  cachedChild: StyledNode
]

# Builds a StyledNode tree, optionally based on a previously cached version.
proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledNode): StyledNode =
  let html = document.html
  if html == nil:
    return

  var author: seq[CSSStylesheet]
  for sheet in document.sheets():
    author.add(sheet.applyMediaQuery(document.window))

  var styledStack: seq[CascadeLevel]
  styledStack.add((nil, html, PSEUDO_NONE, cachedTree))

  var root: StyledNode

  while styledStack.len > 0:
    var (styledParent, child, pseudo, cachedChild) = styledStack.pop()

    var styledChild: StyledNode
    let valid = cachedChild != nil and cachedChild.isValid()
    if valid:
      if cachedChild.t == STYLED_ELEMENT:
        if cachedChild.pseudo == PSEUDO_NONE:
          # We can't just copy cachedChild.children from the previous pass, as
          # any child could be invalid.
          styledChild = styledParent.newStyledElement(Element(cachedChild.node), cachedChild.computed, cachedChild.depends)
        else:
          # Pseudo elements can't have invalid children.
          styledChild = cachedChild
          styledChild.parent = styledParent
      else:
        # Text
        styledChild = cachedChild
        styledChild.parent = styledParent
      if styledParent == nil:
        # Root element
        root = styledChild
      else:
        styledParent.children.add(styledChild)
    else:
      # From here on, computed values of this node's children are invalid
      # because of property inheritance.
      cachedChild = nil
      if pseudo != PSEUDO_NONE:
        let declmap = styledParent.calcRules(ua, user, author)
        case pseudo
        of PSEUDO_BEFORE, PSEUDO_AFTER:
          let styledPseudo = pseudo.applyDeclarations(styledParent, declmap)
          if styledPseudo != nil:
            styledParent.children.add(styledPseudo)
            let contents = styledPseudo.computed{"content"}
            for content in contents:
              styledPseudo.children.add(styledPseudo.newStyledReplacement(content))
        of PSEUDO_INPUT_TEXT:
          let content = HTMLInputElement(styledParent.node).inputString()
          if content.len > 0:
            let styledText = styledParent.newStyledText(content)
            # Note: some pseudo-elements (like input text) generate text nodes
            # directly, so we have to cache them like this.
            styledText.pseudo = pseudo
            styledParent.children.add(styledText)
        of PSEUDO_TEXTAREA_TEXT:
          let content = HTMLTextAreaElement(styledParent.node).textAreaString()
          if content.len > 0:
            let styledText = styledParent.newStyledText(content)
            styledText.pseudo = pseudo
            styledParent.children.add(styledText)
        of PSEUDO_IMAGE:
          let src = Element(styledParent.node).attr("src")
          let content = CSSContent(t: CONTENT_IMAGE, s: src)
          let styledText = styledParent.newStyledReplacement(content)
          styledText.pseudo = pseudo
          styledParent.children.add(styledText)
        of PSEUDO_NEWLINE:
          let content = CSSContent(t: CONTENT_NEWLINE)
          let styledText = styledParent.newStyledReplacement(content)
          styledText.pseudo = pseudo
          styledParent.children.add(styledText)
        of PSEUDO_NONE: discard
      else:
        assert child != nil
        if styledParent != nil:
          if child.nodeType == ELEMENT_NODE:
            styledChild = styledParent.newStyledElement(Element(child))
            styledParent.children.add(styledChild)
            let map = styledChild.calcRules(ua, user, author)
            applyStyle(styledParent, styledChild, map)
          elif child.nodeType == TEXT_NODE:
            let text = Text(child)
            styledChild = styledParent.newStyledText(text)
            styledParent.children.add(styledChild)
        else:
          # Root element
          styledChild = newStyledElement(Element(child))
          let declmap = styledChild.calcRules(ua, user, author)
          applyStyle(styledParent, styledChild, declmap)
          root = styledChild

    if styledChild != nil and styledChild.t == STYLED_ELEMENT and styledChild.node != nil:
      styledChild.applyDependValues()
      # i points to the child currently being inspected.
      var i = if cachedChild != nil:
        cachedChild.children.len - 1
      else:
        -1
      template stack_append(styledParent: StyledNode, child: Node) =
        if cachedChild != nil:
          var cached: StyledNode
          while i >= 0:
            let it = cachedChild.children[i]
            dec i
            if it.node == child:
              cached = it
              break
          styledStack.add((styledParent, child, PSEUDO_NONE, cached))
        else:
          styledStack.add((styledParent, child, PSEUDO_NONE, nil))

      template stack_append(styledParent: StyledNode, ps: PseudoElem) =
        if cachedChild != nil:
          var cached: StyledNode
          let oldi = i
          while i >= 0:
            let it = cachedChild.children[i]
            dec i
            if it.pseudo == ps:
              cached = it
              break
          # When calculating pseudo-element rules, their dependencies are added
          # to their parent's dependency list; so invalidating a pseudo-element
          # invalidates its parent too, which in turn automatically rebuilds
          # the pseudo-element.
          # In other words, we can just do this:
          if cached != nil:
            styledStack.add((styledParent, nil, ps, cached))
          else:
            i = oldi # move pointer back to where we started
        else:
          styledStack.add((styledParent, nil, ps, nil))

      let elem = Element(styledChild.node)

      stack_append styledChild, PSEUDO_AFTER

      if elem.tagType == TAG_TEXTAREA:
        stack_append styledChild, PSEUDO_TEXTAREA_TEXT
      elif elem.tagType == TAG_IMG or elem.tagType == TAG_IMAGE:
        stack_append styledChild, PSEUDO_IMAGE
      elif elem.tagType == TAG_BR:
        stack_append styledChild, PSEUDO_NEWLINE
      else:
        for i in countdown(elem.childList.high, 0):
          if elem.childList[i].nodeType in {ELEMENT_NODE, TEXT_NODE}:
            stack_append styledChild, elem.childList[i]
        if elem.tagType == TAG_INPUT:
          stack_append styledChild, PSEUDO_INPUT_TEXT

      stack_append styledChild, PSEUDO_BEFORE
  return root

proc applyStylesheets*(document: Document, uass, userss: CSSStylesheet,
    previousStyled: StyledNode): StyledNode =
  let uass = uass.applyMediaQuery(document.window)
  let userss = userss.applyMediaQuery(document.window)
  return document.applyRules(uass, userss, previousStyled)