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
|
import algorithm
import sequtils
import streams
import sugar
import css/mediaquery
import css/parser
import css/select
import css/selparser
import css/sheet
import css/values
import html/dom
import html/tags
type
ApplyResult = object
normal: seq[CSSDeclaration]
important: seq[CSSDeclaration]
RuleList* = array[PseudoElem, seq[CSSDeclaration]]
proc applyProperty(elem: Element, d: CSSDeclaration, pseudo: PseudoElem) =
var parent: CSSSpecifiedValues
if elem.parentElement != nil:
parent = elem.parentElement.css
else:
parent = rootProperties()
case pseudo
of PSEUDO_NONE:
elem.css.applyValue(parent, d)
of PSEUDO_BEFORE, PSEUDO_AFTER:
if elem.pseudo[pseudo] == nil:
elem.pseudo[pseudo] = elem.css.inheritProperties()
elem.pseudo[pseudo].applyValue(parent, d)
elem.cssapplied = true
elem.rendered = false
func applies(mq: MediaQuery): 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()
of CONDITION_AND:
return mq.anda.applies() and mq.andb.applies()
of CONDITION_OR:
return mq.ora.applies() or mq.orb.applies()
of CONDITION_FEATURE:
case mq.feature.t
of FEATURE_COLOR:
return true #TODO
of FEATURE_GRID:
return mq.feature.b
of FEATURE_HOVER:
return mq.feature.b
of FEATURE_PREFERS_COLOR_SCHEME:
return mq.feature.b
func applies(mqlist: MediaQueryList): bool =
for mq in mqlist:
if mq.applies():
return true
return false
type ToSorts = array[PseudoElem, seq[(int, seq[CSSDeclaration])]]
proc calcRule(tosorts: var ToSorts, elem: Element, rule: CSSRuleBase) =
if rule of CSSRuleDef:
let rule = CSSRuleDef(rule)
for sel in rule.sels:
let match = elem.selectorsMatch(sel)
if match.success:
let spec = getSpecificity(sel)
tosorts[match.pseudo].add((spec,rule.decls))
elif rule of CSSMediaQueryDef:
let def = CSSMediaQueryDef(rule)
if def.query.applies():
for child in def.children:
tosorts.calcRule(elem, child)
func calcRules(elem: Element, rules: CSSStylesheet): RuleList =
var tosorts: ToSorts
for rule in rules:
tosorts.calcRule(elem, rule)
for i in PseudoElem:
tosorts[i].sort((x, y) => cmp(x[0], y[0]))
result[i] = collect(newSeq):
for item in tosorts[i]:
for dl in item[1]:
dl
#TODO couldn't these two procedures be merged?
proc applyNormal(ares: var ApplyResult, decls: seq[CSSDeclaration]) =
for decl in decls:
if decl.important:
ares.important.add(decl)
else:
ares.normal.add(decl)
proc applyImportant(ares: var ApplyResult, decls: seq[CSSDeclaration]) =
for decl in decls:
if decl.important:
ares.important.add(decl)
else:
ares.normal.add(decl)
proc applyRules(element: Element, ua, user, author: RuleList, pseudo: PseudoElem) =
var ares: ApplyResult
ares.applyNormal(ua[pseudo])
ares.applyNormal(user[pseudo])
ares.applyNormal(author[pseudo])
ares.applyImportant(author[pseudo])
if pseudo == PSEUDO_NONE:
let style = element.attr("style")
if style.len > 0:
let inline_rules = newStringStream(style).parseListOfDeclarations2()
ares.applyNormal(inline_rules)
ares.applyImportant(inline_rules)
ares.applyImportant(user[pseudo])
ares.applyImportant(ua[pseudo])
for rule in ares.normal:
element.applyProperty(rule, pseudo)
for rule in ares.important:
element.applyProperty(rule, pseudo)
func applyMediaQuery(ss: CSSStylesheet): CSSStylesheet =
for rule in ss:
if rule of CSSRuleDef:
result.add(rule)
elif rule of CSSMediaQueryDef:
let rule = CSSMediaQueryDef(rule)
if rule.query.applies():
result.add(rule.children.applyMediaQuery())
proc applyRules*(document: Document, ua, user: CSSStylesheet) =
var stack: seq[Element]
var embedded_rules: seq[CSSStylesheet]
stack.add(document.head)
var rules_head: CSSStylesheet
for child in document.head.children:
if child.tagType == TAG_STYLE:
let style = HTMLStyleElement(child)
rules_head.add(style.sheet)
if rules_head.len > 0:
embedded_rules.add(rules_head)
stack.setLen(0)
stack.add(document.root)
while stack.len > 0:
let elem = stack.pop()
var rules_local: CSSStylesheet
for child in document.head.children:
if child.tagType == TAG_STYLE:
let style = HTMLStyleElement(child)
rules_local.add(style.sheet)
if rules_local.len > 0:
embedded_rules.add(rules_local)
if not elem.cssapplied:
if elem.parentElement != nil:
elem.css = elem.parentElement.css.inheritProperties()
else:
elem.css = rootProperties()
let uarules = calcRules(elem, ua)
let userrules = calcRules(elem, user)
let this_rules = embedded_rules.concat()
let authorrules = calcRules(elem, this_rules)
for pseudo in PseudoElem:
elem.applyRules(uarules, userrules, authorrules, pseudo)
for i in countdown(elem.children.high, 0):
stack.add(elem.children[i])
if rules_local.len > 0:
discard embedded_rules.pop()
proc applyStylesheets*(document: Document, uass, userss: CSSStylesheet) =
let uass = uass.applyMediaQuery()
let userss = userss.applyMediaQuery()
document.applyRules(uass, userss)
proc refreshStyle*(elem: Element) =
elem.cssapplied = false
for child in elem.children:
child.refreshStyle()
|