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
|
import unicode
import strutils
import tables
import streams
import sequtils
import sugar
import algorithm
import css/selector
import css/parser
import css/values
import html/dom
import types/enums
#TODO case sensitivity
type SelectResult = object
success: bool
pseudo: PseudoElem
func selectres(s: bool, p: PseudoElem = PSEUDO_NONE): SelectResult =
return SelectResult(success: s, pseudo: p)
func psuccess(s: SelectResult): bool =
return s.pseudo == PSEUDO_NONE and s.success
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
of "hover": return elem.hover
of "root": return elem == elem.ownerDocument.root
else: return false
func pseudoElemSelectorMatches(elem: Element, sel: Selector): SelectResult =
case sel.elem
of "after": return selectres(true, PSEUDO_AFTER)
of "before": return selectres(true, PSEUDO_AFTER)
else: return selectres(false)
func selectorsMatch(elem: Element, selectors: SelectorList): SelectResult
func selectorMatches(elem: Element, sel: Selector): SelectResult =
case sel.t
of TYPE_SELECTOR:
return selectres(elem.tagType == sel.tag)
of CLASS_SELECTOR:
return selectres(sel.class in elem.classList)
of ID_SELECTOR:
return selectres(sel.id == elem.id)
of ATTR_SELECTOR:
return selectres(elem.attrSelectorMatches(sel))
of PSEUDO_SELECTOR:
return selectres(pseudoSelectorMatches(elem, sel))
of PSELEM_SELECTOR:
return pseudoElemSelectorMatches(elem, sel)
of UNIVERSAL_SELECTOR:
return selectres(true)
of FUNC_SELECTOR:
return selectres(false)
of COMBINATOR_SELECTOR:
#combinator without at least two members makes no sense
assert sel.csels.len > 1
let match = elem.selectorsMatch(sel.csels[^1])
if match.success:
var i = sel.csels.len - 2
case sel.ct
of DESCENDANT_COMBINATOR:
var e = elem.parentElement
while e != nil and i >= 0:
let res = e.selectorsMatch(sel.csels[i])
if res.pseudo != PSEUDO_NONE:
return selectres(false)
if res.success:
dec i
e = e.parentElement
of CHILD_COMBINATOR:
var e = elem.parentElement
while e != nil and i >= 0:
let res = e.selectorsMatch(sel.csels[i])
if res.pseudo != PSEUDO_NONE:
return selectres(false)
if not res.success:
return selectres(false)
dec i
e = e.parentElement
of NEXT_SIBLING_COMBINATOR:
var e = elem.previousElementSibling
while e != nil and i >= 0:
let res = e.selectorsMatch(sel.csels[i])
if res.pseudo != PSEUDO_NONE:
return selectres(false)
if not res.success:
eprint "fail", e.tagType
return selectres(false)
dec i
e = e.previousElementSibling
of SUBSEQ_SIBLING_COMBINATOR:
var e = elem.previousElementSibling
while e != nil and i >= 0:
let res = e.selectorsMatch(sel.csels[i])
if res.pseudo != PSEUDO_NONE:
return selectres(false)
if res.success:
dec i
e = e.previousElementSibling
return selectres(i == -1, match.pseudo)
else:
return selectres(false)
func selectorsMatch(elem: Element, selectors: SelectorList): SelectResult =
for sel in selectors.sels:
let res = selectorMatches(elem, sel)
if not res.success:
return selectres(false)
if res.pseudo != PSEUDO_NONE:
if result.pseudo != PSEUDO_NONE:
return selectres(false)
result.pseudo = res.pseudo
result.success = 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.fsels).psuccess)
of "is", "where":
return document.all_elements.filter((elem) => selectorsMatch(elem, sel.fsels).psuccess)
return newSeq[Element]()
of COMBINATOR_SELECTOR:
return document.all_elements.filter((elem) => selectorMatches(elem, sel))
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].fsels).psuccess)
of "is", "where":
result = result.filter((elem) => selectorsMatch(elem, sellist[i].fsels).psuccess)
else: discard
else:
result = result.filter((elem) => selectorMatches(elem, sellist[i]).psuccess)
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))
proc applyProperty(elem: Element, decl: CSSDeclaration, pseudo: PseudoElem) =
let cval = getComputedValue(decl, elem.cssvalues)
case pseudo
of PSEUDO_NONE:
elem.cssvalues[cval.t] = cval
of PSEUDO_BEFORE:
elem.cssvalues_before[cval.t] = cval
of PSEUDO_AFTER:
elem.cssvalues_after[cval.t] = cval
elem.cssapplied = true
type
ParsedRule* = tuple[sels: seq[SelectorList], oblock: CSSSimpleBlock]
ParsedStylesheet* = seq[ParsedRule]
ApplyResult = object
normal: seq[tuple[e:Element,d:CSSDeclaration,p:PseudoElem]]
important: seq[tuple[e:Element,d:CSSDeclaration,p:PseudoElem]]
proc parseStylesheet*(s: Stream): ParsedStylesheet =
for v in parseCSS(s).value:
let sels = parseSelectors(v.prelude)
if sels.len > 1 or sels[^1].len > 0:
result.add((sels: sels, oblock: v.oblock))
func calcRules(elem: Element, rules: ParsedStylesheet):
array[low(PseudoElem)..high(PseudoElem), seq[CSSSimpleBlock]] =
var tosorts: array[low(PseudoElem)..high(PseudoElem), seq[tuple[s:int,b:CSSSimpleBlock]]]
for rule in rules:
for sel in rule.sels:
let match = elem.selectorsMatch(sel)
if match.success:
let spec = getSpecificity(sel)
tosorts[match.pseudo].add((spec,rule.oblock))
for i in low(PseudoElem)..high(PseudoElem):
tosorts[i].sort((x, y) => cmp(x.s,y.s))
result[i] = tosorts[i].map((x) => x.b)
proc applyRules*(document: Document, pss: ParsedStylesheet, reset: bool = false): ApplyResult =
var stack: seq[Element]
document.root.cssvalues.rootProperties()
stack.add(document.root)
while stack.len > 0:
let elem = stack.pop()
if not elem.cssapplied:
if reset:
elem.cssvalues.rootProperties()
let rules_pseudo = calcRules(elem, pss)
for pseudo in low(PseudoElem)..high(PseudoElem):
let rules = rules_pseudo[pseudo]
for rule in rules:
let decls = parseCSSListOfDeclarations(rule.value)
for item in decls:
if item of CSSDeclaration:
let decl = CSSDeclaration(item)
if decl.important:
result.important.add((elem, decl, pseudo))
else:
result.normal.add((elem, decl, pseudo))
var i = elem.children.len - 1
while i >= 0:
let child = elem.children[i]
stack.add(child)
dec i
proc applyAuthorRules*(document: Document): ApplyResult =
var stack: seq[Element]
var embedded_rules: seq[ParsedStylesheet]
stack.add(document.head)
var rules_head = ""
for child in document.head.children:
if child.tagType == TAG_STYLE:
for ct in child.childNodes:
if ct.nodeType == TEXT_NODE:
rules_head &= Text(ct).data
stack.setLen(0)
stack.add(document.root)
if rules_head.len > 0:
let parsed = newStringStream(rules_head).parseStylesheet()
embedded_rules.add(parsed)
while stack.len > 0:
let elem = stack.pop()
var rules_local = ""
for child in elem.children:
if child.tagType == TAG_STYLE:
for ct in child.childNodes:
if ct.nodeType == TEXT_NODE:
rules_local &= Text(ct).data
if rules_local.len > 0:
let parsed = newStringStream(rules_local).parseStylesheet()
embedded_rules.add(parsed)
if not elem.cssapplied:
let this_rules = embedded_rules.concat()
let rules_pseudo = calcRules(elem, this_rules)
for pseudo in low(PseudoElem)..high(PseudoElem):
let rules = rules_pseudo[pseudo]
for rule in rules:
let decls = parseCSSListOfDeclarations(rule.value)
for item in decls:
if item of CSSDeclaration:
let decl = CSSDeclaration(item)
if decl.important:
result.important.add((elem, decl, pseudo))
else:
result.normal.add((elem, decl, pseudo))
var i = elem.children.len - 1
while i >= 0:
let child = elem.children[i]
stack.add(child)
dec i
if rules_local.len > 0:
discard embedded_rules.pop()
proc applyStylesheets*(document: Document, uass: ParsedStylesheet, userss: ParsedStylesheet) =
let ua = document.applyRules(uass, true)
let user = document.applyRules(userss)
let author = document.applyAuthorRules()
var elems: seq[Element]
for rule in ua.normal:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for rule in user.normal:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for rule in author.normal:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for rule in author.important:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for rule in user.important:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for rule in ua.important:
if not rule.e.cssapplied:
elems.add(rule.e)
rule.e.applyProperty(rule.d, rule.p)
for elem in elems:
if elem.parentElement != nil:
elem.cssvalues.inheritProperties(elem.parentElement.cssvalues)
|