-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.py
482 lines (398 loc) · 16.5 KB
/
dom.py
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
import itertools
import re
import functools
from collections.abc import MutableMapping, Iterator
from logger import *
MAX_LRU_CACHE = 128
class ClassList:
def __init__(self):
self.__classes = set()
def add(self, classname):
assert isinstance(classname, str), "ClassList::add() - classname must be a string"
self.__classes.add(classname)
return self
def contains(self, classname):
assert isinstance(classname, str), "ClassList::contains() - classname must be a string"
return classname in self.__classes
def remove(self, classname):
assert isinstance(classname, str), "ClassList::remove() - classname must be a string"
self.__classes.remove(classname)
return self
'''
if classes contain 'classname', removes it
else adds
'''
def toggle(self, classname):
assert isinstance(classname, str), "ClassList::toggle() - classname must be a string"
if self.contains(classname):
self.remove(classname)
else:
self.add(classname)
'''
Just a dict that checks types
Keys - string
values - HTMLDomNodes
Used for quick selection by ID from HTML document
'''
class IdStorage(MutableMapping):
def __init__(self, *args, **kwargs):
self.store = dict()
self.update(dict(*args, **kwargs)) # use the free update to set keys
def __getitem__(self, key):
return self.store[key]
def __setitem__(self, key, value):
assert isinstance(key, str), "IdStorage::set() - key must be a string"
assert isinstance(value, HTMLDomNode), "IdStorage::set() - value must be a HTMLDomNode"
if (key in self.store):
Logger.warning("IdStorage::set() - malformed HTML, id {0} is not unique".format(key))
self.store[key] = value
def __delitem__(self, key):
del self.store[key]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
'''
Iterates through passed startElement and its children.
WARNING: loops only through Elements.
'''
class HTMLDomIterator(Iterator):
def __init__(self, startElem):
assert isinstance(startElem, HTMLDomNode), "HTMLDomIterator::__init__() - startElem is not HTMLDomNode"
self.firstScanned = False
self.cur = startElem
self.curIndex = 0
# stack is a storage of tuples(Element, last index)
self.stack = []
def __iter__(self):
return self
def __next__(self):
# extreme case - we need to iterate through first element only once
if not self.firstScanned:
self.firstScanned = True
if(self.cur == None):
raise StopIteration
return self.cur
children = self.cur.children()
if len(children) != 0:
self.stack.append((self.cur, self.curIndex + 1))
self.cur = children[self.curIndex]
self.curIndex = 0
else:
if len(self.stack) != 0:
self.cur, self.curIndex = self.__popSearch()
if(self.cur == None):
raise StopIteration
return self.cur
'''
tries to search for next element, popping from stack and looping through children
If element is found, returns it, else returns None
'''
def __popSearch(self):
while(len(self.stack) != 0):
cur, curIndex = self.stack.pop()
children = cur.children()
if curIndex >= len(children):
continue
# more children available from this element
self.stack.append((cur, curIndex + 1))
cur, curIndex = (children[curIndex], 0)
return (cur, curIndex)
return (None, None)
'''
Just a node, not element(comment, etc)
'''
class HTMLDomNode:
# regular expression for css selectors
CssIdentifierRe = r'\-?[_a-zA-Z]+[_a-zA-Z0-9-]*'
CssPrefixRe = r'[.#]'
QuotedRe = r'(?:"[^"]*?")|(?:\'[^\']*?\')'
CssAttrRe = r'\[(?P<attrKey>' + CssIdentifierRe + r')((?P<attrAction>[~|^$*]?=)(?P<attrVal>' + QuotedRe + r'))?\]'
# precomiling this re, because only this regexp is used mostly always
CssUnitRe = re.compile(r'(?:^|\s+)(?P<asterisk>\*)|(?P<prefix>' + CssPrefixRe + r')?(?P<identifier>' + CssIdentifierRe + r')(?P<attr>' + CssAttrRe + r')?(?:$|\s+)')
def __init__(self, document, parent=None, tag="", attrs=None, text=""):
if attrs is None:
attrs = []
assert isinstance(document, HTMLDocument) and \
isinstance(tag, str) and \
isinstance(attrs, list) and \
isinstance(text, str), "HTMLDomNode: invalid arg types"
if parent is not None:
assert isinstance(parent, HTMLDomNode), "HTMLDomNode: parent must be a HTMLDomNode instance"
self.__document = document
self.__parent = parent
self.__previousElementSibling = None
self.__previousSibling = None
self.__nextElementSibling = None
self.__nextSibling = None
self.__tag = tag
self.__attrs = {}
self.__childNodes = []
self.__children = []
self.__classList = ClassList()
self.__text = text
self.__makeAttrs(attrs)
self.__processAttrs()
def childNodes(self):
return self.__childNodes
'''
returns child ELEMENTS, not only Nodes
WARNING: may be slow(calculating array in function), but we do not want any before-time optimization
'''
def children(self):
return self.__children
def classList(self):
return self.__classList
def document(self):
return self.__document
def firstChild(self):
if len(self.__childNodes) == 0:
return None
return self.__childNodes[0]
def firstElementChild(self):
if len(self.__children) == 0:
return None
return self.__children[0]
'''
WARNING! If 'name' is not a key in attrs dict, returns None
'''
def getAttribute(self, name):
assert isinstance(name, str), "HTMLDomNode::getAttribute() - name must be a string"
if not(self.hasAttribute(name)):
return None
return self.__attrs[name]
'''
searches ALL elements with class name 'className', starting from THIS node
As this operation may be a bit expensive, using lru_cache
'''
@functools.lru_cache(MAX_LRU_CACHE)
def getElementsByClassName(self, className):
assert isinstance(className, str), "HTMLDomNode::getElementsByClassName() - name must be a string"
return list(filter(lambda x: x.classList().contains(className), HTMLDomIterator(self)))
'''
searches ALL elements with tag name 'tagName', starting from THIS node
As this operation may be a bit expensive, using lru_cache
'''
@functools.lru_cache(MAX_LRU_CACHE)
def getElementsByTagName(self, tagName):
assert isinstance(tagName, str), "HTMLDomNode::getElementsByTagName() - name must be a string"
return list(filter(lambda x: x.tagName() == tagName, HTMLDomIterator(self)))
def hasAttribute(self, name):
assert isinstance(name, str), "HTMLDomNode::hasAttribute() - name must be a string"
return name in self.__attrs
def lastChild(self):
if len(self.__childNodes) == 0:
return None
return self.__childNodes[-1]
def lastElementChild(self):
if len(self.__children) == 0:
return None
return self.__children[-1]
def nextSibling(self):
return self.__nextSibling
'''
returns sibling ELEMENT, not Node
WARNING: may be slow(calculating in function), but we do not want any before-time optimization
'''
def nextElementSibling(self):
return self.__nextElementSibling
def parentNode(self):
return self.__parent
'''
returns parent ELEMENT, not Node
WARNING: may be slow(calculating in function), but we do not want any before-time optimization
'''
def parentElement(self):
curParent = self.parentNode()
while curParent and not isinstance(curParent, HTMLDomElement):
curParent = self.parentNode()
return curParent
def previousSibling(self):
return self.__previousSibling
'''
returns sibling ELEMENT, not Node
WARNING: may be slow(calculating in function), but we do not want any before-time optimization
'''
def previousElementSibling(self):
return self.__previousElementSibling
def querySelector(self, selectors):
res = self.querySelectorAll(selectors)
return res[0] if res and len(res) > 0 else None
@functools.lru_cache(MAX_LRU_CACHE)
def querySelectorAll(self, selectors):
# last match elements
lastMatches = None
# last match objects
lastM = None
for m in HTMLDomNode.CssUnitRe.finditer(selectors):
newMatches = self.__cssParseSelector(m)
newMatches = self.__cssParseArgs(m, newMatches)
if lastMatches:
# combiner is chars between two selectors
combiner = selectors[lastM.end(): m.start()].strip()
#normalizing
if combiner == '':
combiner = ' '
newMatches = self.__cssCombineMatches(combiner, self.__getIdDictFromList(newMatches), self.__getIdDictFromList(lastMatches))
lastM = m
lastMatches = newMatches
return lastMatches
'''
WARNING! Throws if name is not a correct key from 'attrs' dict
'''
def setAttribute(self, name, value):
assert isinstance(name, str), "HTMLDomNode::setAttribute() - name must be a string"
self.__attrs[name] = value
def tagName(self):
return self.__tag
def text(self):
return self.__text
'''
Helper function for query selector
Combines two selectors(two groups, new Matches and lastMatches) using combiner
*Matches is dict as {id(node): node}
'''
def __cssCombineMatches(self, combiner, newMatches, lastMatches):
assert isinstance(newMatches, dict) and isinstance(lastMatches, dict), "HTMLDomNode::__cssCombineMatches() - invalid newMatches or lastMatches, must be a dict"
# subfunction for ',' combiner
def updateAndGet(d1, d2):
d1.update(d2)
return d1.values()
actions = {
',': lambda: updateAndGet(lastMatches, newMatches),
' ': lambda: list(filter(lambda node: True in map(lambda ancestor: id(ancestor) in lastMatches, node.__getAllAncestors()), newMatches.values())),
'>': lambda: list(filter(lambda node: id(node.parentNode()) in lastMatches , newMatches.values())),
'+': lambda: list(filter(lambda node: id(node.previousSibling()) in lastMatches, newMatches.values())),
'~': lambda: list(filter(lambda node: True in map(lambda sibl: id(sibl) in lastMatches, node.__getAllPrevSiblings()), newMatches.values())),
}
return actions[combiner]()
'''
Helper function for querySelector
parser attributes in []
'''
def __cssParseArgs(self, matchObj, matches):
if not matchObj['attrKey']:
return matches
key = matchObj['attrKey']
op = matchObj['attrAction']
val = matchObj['attrVal']
# matches [key]
if not op and not val:
return list(filter(lambda x: x.hasAttribute(key), matches))
# unquoting val
val = val.strip("'\"")
filters = {
'=' : lambda x: True if x.getAttribute(key) and x.getAttribute(key) == val else False,
# contains WORD
'~=' : lambda x: True if x.getAttribute(key) and val in x.getAttribute(key).split(' ') else False,
'|=' : lambda x: True if x.getAttribute(key) and x.getAttribute(key).startswith(val) else False,
'^=' : lambda x: True if x.getAttribute(key) and x.getAttribute(key).startswith(val) else False,
'$=' : lambda x: True if x.getAttribute(key) and x.getAttribute(key).endswith(val) else False,
# contains SUBSTRING
'*=' : lambda x: True if x.getAttribute(key) and val in x.getAttribute(key) else False
}
return list(filter(filters[op], matches))
'''
Helper function for querySelector.
Selects needed nodes.
'''
def __cssParseSelector(self, matchObj):
if(matchObj.group('asterisk')):
return [self] + self.children()
# prefix is . or #
prefix = matchObj.group('prefix')
identifier = matchObj.group('identifier')
if not identifier:
raise ValueError("HTMLDomNode::__parseCss() - invalid Css selector")
operations = {
'.': self.getElementsByClassName,
'#': self.document().getElementById
}
if prefix:
res = operations[prefix](identifier)
# normalizing result
if not isinstance(res, list):
res = [res]
if len(res) == 1 and res[0] == None:
res = []
return res
return self.getElementsByTagName(identifier)
def __getAllAncestors(self):
ancestors = []
curAncestor = self.parentElement()
while (curAncestor):
ancestors.append(curAncestor)
curAncestor = curAncestor.parentElement()
return ancestors
def __getAllPrevSiblings(self):
siblings = []
curSibling = self.previousElementSibling()
while (curSibling):
siblings.append(curSibling)
curSibling = curSibling.previousElementSibling()
return siblings
def __getIdDictFromList(self, l):
assert isinstance(l, list), "HTMLDomNode::__getIdDictFromHtmlDomNodeList() - l must be a list"
return dict(map(lambda x: {id(x), x}, l))
'''
Makes from passed list of tuples a dictionary of attributes
'''
def __makeAttrs(self, attrs):
for attr in attrs:
self.__attrs[attr[0]] = attr[1]
'''
Does some operations with attributes.
For example, splits string value of 'class' attribute to separate values
'''
def __processAttrs(self):
self.__processAttrsClassList()
self.__processAttrsId()
'''
Forming classlist
'''
def __processAttrsClassList(self):
classesStr = self.getAttribute("class")
if classesStr is None or classesStr == '':
return
# spliting by spaces, removing extra spaces, joining
classList = list(itertools.filterfalse(lambda x: x == '', classesStr.split(' ')))
for item in classList:
self.__classList.add(item)
'''
Forming ID storage in HTMLDocument
'''
def __processAttrsId(self):
id = self.getAttribute("id")
if(id is None or id == ''):
return
self.document().getIdStorage()[id] = self
def _setLeftElementSibling(self, sibl):
assert isinstance(sibl, HTMLDomElement), "HTMLDomNode::_setLeftElementSibling() - sibl is not HTMLDomElement"
self.__previousElementSibling = sibl
def _setLeftSibling(self, sibl):
assert isinstance(sibl, HTMLDomNode), "HTMLDomNode::_setLeftSibling() - sibl is not HTMLDomNode"
self.__previousSibling = sibl
def _setRightElementSibling(self, sibl):
assert isinstance(sibl, HTMLDomElement), "HTMLDomNode::_setRightElementSibling() - sibl is not HTMLDomElement"
self.__nextElementSibling = sibl
def _setRightSibling(self, sibl):
assert isinstance(sibl, HTMLDomNode), "HTMLDomNode::_setRightSibling() - sibl is not HTMLDomNode"
self.__nextSibling = sibl
class HTMLDomElement(HTMLDomNode):
def __init__(self, document, parent=None, tag="", attrs=None):
HTMLDomNode.__init__(self, document, parent, tag, attrs)
def appendChild(self, child):
assert isinstance(child, HTMLDomElement) or isinstance(child, HTMLDomNode), "HTMLDomNode: child must be an instance of HTMLDomElement or HTMLDomNode!"
self.childNodes().append(child)
if isinstance(child, HTMLDomElement):
self.children().append(child)
class HTMLDocument(HTMLDomElement):
def __init__(self):
HTMLDomNode.__init__(self, self, None, "document", None)
self.__idStorage = IdStorage()
def getElementById(self, id):
if not(id in self.__idStorage):
return None
return self.__idStorage[id]
def getIdStorage(self):
return self.__idStorage