dompy-parser 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dompy/models.py ADDED
@@ -0,0 +1,717 @@
1
+ """Nodes and tree operations for an HTML document."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from collections.abc import Callable, Iterable, Iterator, Sequence
7
+ from functools import cached_property
8
+ from typing import Generic, TypeVar
9
+
10
+ HTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
11
+ SVG_NAMESPACE = "http://www.w3.org/2000/svg"
12
+ MATHML_NAMESPACE = "http://www.w3.org/1998/Math/MathML"
13
+ HTML_WHITESPACE = " \t\n\r\f"
14
+ _ASCII_LOWER = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")
15
+ _ASCII_UPPER = str.maketrans("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
16
+ _TOKEN_PATTERN = re.compile(r"[^ \t\n\r\f]+")
17
+ _T = TypeVar("_T")
18
+
19
+
20
+ def ascii_lower(value: str) -> str:
21
+ return value.translate(_ASCII_LOWER)
22
+
23
+
24
+ def tokens(value: str) -> list[str]:
25
+ return list(dict.fromkeys(_TOKEN_PATTERN.findall(value)))
26
+
27
+
28
+ class _LiveCollection(Sequence[_T], Generic[_T]):
29
+ """Resolve a collection against the tree whenever it is read."""
30
+
31
+ def __init__(self, members: Callable[[], Iterable[_T]]) -> None:
32
+ self._members = members
33
+
34
+ def __iter__(self) -> Iterator[_T]:
35
+ return iter(self._members())
36
+
37
+ def __len__(self) -> int:
38
+ return sum(1 for _ in self._members())
39
+
40
+ def __getitem__(self, index):
41
+ return list(self._members())[index]
42
+
43
+ def __eq__(self, other):
44
+ if not isinstance(other, (list, tuple, _LiveCollection)):
45
+ return NotImplemented
46
+ return list(self) == list(other)
47
+
48
+ def item(self, index: int) -> _T | None:
49
+ if index < 0:
50
+ return None
51
+ try:
52
+ return self[index]
53
+ except IndexError:
54
+ return None
55
+
56
+
57
+ class Node:
58
+ ELEMENT_NODE = 1
59
+ TEXT_NODE = 3
60
+ PROCESSING_INSTRUCTION_NODE = 7
61
+ COMMENT_NODE = 8
62
+ DOCUMENT_NODE = 9
63
+ DOCUMENT_TYPE_NODE = 10
64
+ DOCUMENT_FRAGMENT_NODE = 11
65
+
66
+ def __init__(self) -> None:
67
+ self._parent: ParentNode | None = None
68
+ self._children: list[Node] = []
69
+ self._document = None
70
+
71
+ @property
72
+ def ownerDocument(self):
73
+ return self._document
74
+
75
+ @property
76
+ def parentNode(self) -> ParentNode | None:
77
+ return self._parent
78
+
79
+ @property
80
+ def parentElement(self) -> Element | None:
81
+ return self._parent if isinstance(self._parent, Element) else None
82
+
83
+ @cached_property
84
+ def childNodes(self) -> _LiveCollection[Node]:
85
+ return _LiveCollection(lambda: self._children)
86
+
87
+ @property
88
+ def firstChild(self) -> Node | None:
89
+ return self._children[0] if self._children else None
90
+
91
+ @property
92
+ def lastChild(self) -> Node | None:
93
+ return self._children[-1] if self._children else None
94
+
95
+ @property
96
+ def previousSibling(self) -> Node | None:
97
+ if self._parent is None:
98
+ return None
99
+ siblings = self._parent._children
100
+ index = siblings.index(self)
101
+ return siblings[index - 1] if index else None
102
+
103
+ @property
104
+ def nextSibling(self) -> Node | None:
105
+ if self._parent is None:
106
+ return None
107
+ siblings = self._parent._children
108
+ index = siblings.index(self) + 1
109
+ return siblings[index] if index < len(siblings) else None
110
+
111
+ @property
112
+ def isConnected(self) -> bool:
113
+ return self.getRootNode().nodeType == self.DOCUMENT_NODE
114
+
115
+ def getRootNode(self) -> Node:
116
+ node = self
117
+ while node._parent is not None:
118
+ node = node._parent
119
+ return node
120
+
121
+ def contains(self, other: Node | None) -> bool:
122
+ while other is not None:
123
+ if other is self:
124
+ return True
125
+ other = other._parent
126
+ return False
127
+
128
+ def hasChildNodes(self) -> bool:
129
+ return bool(self._children)
130
+
131
+ def _descendants(self) -> Iterator[Node]:
132
+ stack = list(reversed(self._children))
133
+ while stack:
134
+ node = stack.pop()
135
+ yield node
136
+ stack.extend(reversed(node._children))
137
+
138
+ @property
139
+ def textContent(self) -> str | None:
140
+ return None
141
+
142
+ @textContent.setter
143
+ def textContent(self, value: str | None) -> None:
144
+ pass
145
+
146
+ nodeValue = textContent
147
+
148
+ def remove(self) -> None:
149
+ if self._parent is not None:
150
+ self._parent.removeChild(self)
151
+
152
+ def _set_document(self, document) -> None:
153
+ stack = [(self, document)]
154
+ while stack:
155
+ node, owner = stack.pop()
156
+ node._document = owner
157
+ stack.extend((child, owner) for child in node._children)
158
+ if isinstance(node, Element) and node.templateContent is not None:
159
+ stack.append(
160
+ (node.templateContent, owner._template_owner() if owner else None)
161
+ )
162
+
163
+ def _copy(self) -> Node:
164
+ return type(self)()
165
+
166
+ def cloneNode(self, deep: bool = False) -> Node:
167
+ clone = self._copy()
168
+ clone._document = self.ownerDocument
169
+ stack = [(self, clone)]
170
+ while stack:
171
+ source, target = stack.pop()
172
+ document = (
173
+ target
174
+ if target.nodeType == self.DOCUMENT_NODE
175
+ else target.ownerDocument
176
+ )
177
+ if isinstance(source, Element) and source.templateContent is not None:
178
+ target.templateContent._document = (
179
+ document._template_owner() if document else None
180
+ )
181
+ if deep:
182
+ stack.append((source.templateContent, target.templateContent))
183
+ if deep:
184
+ for child in source._children:
185
+ child_copy = child._copy()
186
+ child_copy._document = document
187
+ target._append(child_copy)
188
+ stack.append((child, child_copy))
189
+ return clone
190
+
191
+ def __repr__(self) -> str:
192
+ return f"<{type(self).__name__} {self.nodeName!r}>"
193
+
194
+
195
+ class CharacterData(Node):
196
+ def __init__(self, data: str = "") -> None:
197
+ super().__init__()
198
+ self.data = data
199
+
200
+ @property
201
+ def textContent(self) -> str:
202
+ return self.data
203
+
204
+ @textContent.setter
205
+ def textContent(self, value: str | None) -> None:
206
+ self.data = "" if value is None else value
207
+
208
+ nodeValue = textContent
209
+
210
+ def _copy(self) -> CharacterData:
211
+ return type(self)(self.data)
212
+
213
+
214
+ class Text(CharacterData):
215
+ nodeName = "#text"
216
+ nodeType = Node.TEXT_NODE
217
+
218
+
219
+ class Comment(CharacterData):
220
+ nodeName = "#comment"
221
+ nodeType = Node.COMMENT_NODE
222
+
223
+
224
+ class ProcessingInstruction(CharacterData):
225
+ nodeType = Node.PROCESSING_INSTRUCTION_NODE
226
+
227
+ def __init__(self, target: str, data: str = "") -> None:
228
+ super().__init__(data)
229
+ self.target = self.nodeName = target
230
+
231
+ def _copy(self) -> ProcessingInstruction:
232
+ return ProcessingInstruction(self.target, self.data)
233
+
234
+
235
+ class DocumentType(Node):
236
+ nodeType = Node.DOCUMENT_TYPE_NODE
237
+
238
+ def __init__(self, name: str, public_id: str = "", system_id: str = "") -> None:
239
+ super().__init__()
240
+ self.name = self.nodeName = name
241
+ self.publicId = public_id
242
+ self.systemId = system_id
243
+
244
+ def _copy(self) -> DocumentType:
245
+ return DocumentType(self.name, self.publicId, self.systemId)
246
+
247
+
248
+ class ParentNode(Node):
249
+ @cached_property
250
+ def children(self) -> _LiveCollection[Element]:
251
+ return _LiveCollection(
252
+ lambda: (node for node in self._children if isinstance(node, Element))
253
+ )
254
+
255
+ @property
256
+ def childElementCount(self) -> int:
257
+ return len(self.children)
258
+
259
+ @property
260
+ def firstElementChild(self) -> Element | None:
261
+ return next(
262
+ (node for node in self._children if isinstance(node, Element)), None
263
+ )
264
+
265
+ @property
266
+ def lastElementChild(self) -> Element | None:
267
+ return next(
268
+ (node for node in reversed(self._children) if isinstance(node, Element)),
269
+ None,
270
+ )
271
+
272
+ @property
273
+ def textContent(self) -> str | None:
274
+ return "".join(
275
+ node.data for node in self._descendants() if isinstance(node, Text)
276
+ )
277
+
278
+ @textContent.setter
279
+ def textContent(self, value: str | None) -> None:
280
+ self._replace_children([Text(value)] if value else [])
281
+
282
+ def _elements(self) -> Iterator[Element]:
283
+ return (node for node in self._descendants() if isinstance(node, Element))
284
+
285
+ def querySelector(self, selector: str) -> Element | None:
286
+ from .selectors import select
287
+
288
+ return select(self, selector, first=True)
289
+
290
+ def querySelectorAll(self, selector: str) -> list[Element]:
291
+ from .selectors import select
292
+
293
+ return select(self, selector)
294
+
295
+ def _append(self, node: Node) -> None:
296
+ """Attach a fresh node while parsing or cloning."""
297
+ self._children.append(node)
298
+ node._parent = self
299
+
300
+ def _replace_children(self, nodes: list[Node]) -> None:
301
+ for child in self._children:
302
+ child._parent = None
303
+ self._children.clear()
304
+ self._children.extend(nodes)
305
+ for child in nodes:
306
+ child._parent = self
307
+ child._set_document(self.ownerDocument)
308
+
309
+ def _insert(
310
+ self, node: Node, reference: Node | None, *, replacing: bool = False
311
+ ) -> None:
312
+ if not isinstance(node, Node):
313
+ raise TypeError("Expected a Node")
314
+ if reference is not None and reference._parent is not self:
315
+ raise ValueError("Reference node is not a child of this parent")
316
+ if node is reference:
317
+ return
318
+
319
+ # The host of a template fragment also counts as an ancestor for insertion.
320
+ ancestor = self
321
+ while ancestor is not None:
322
+ if ancestor is node:
323
+ raise ValueError("Cannot insert a node into itself or its descendants")
324
+ ancestor = (
325
+ ancestor._host
326
+ if isinstance(ancestor, DocumentFragment) and ancestor._host
327
+ else ancestor._parent
328
+ )
329
+
330
+ incoming = (
331
+ node._children.copy() if isinstance(node, DocumentFragment) else [node]
332
+ )
333
+ if any(child.nodeType == self.DOCUMENT_NODE for child in incoming):
334
+ raise ValueError("Cannot insert a document into a node")
335
+ if self.nodeType == self.DOCUMENT_NODE:
336
+ children = [child for child in self._children if child is not node]
337
+ index = len(children) if reference is None else children.index(reference)
338
+ children[index : index + int(replacing)] = incoming
339
+ self._validate_children(children)
340
+ elif any(child.nodeType == self.DOCUMENT_TYPE_NODE for child in incoming):
341
+ raise ValueError("A doctype can only be a child of a document")
342
+
343
+ document = self if self.nodeType == self.DOCUMENT_NODE else self.ownerDocument
344
+ for child in incoming:
345
+ child.remove()
346
+ index = (
347
+ len(self._children)
348
+ if reference is None
349
+ else self._children.index(reference)
350
+ )
351
+ if replacing:
352
+ reference._parent = None
353
+ self._children[index : index + int(replacing)] = incoming
354
+ for child in incoming:
355
+ child._parent = self
356
+ if child.ownerDocument is not document:
357
+ child._set_document(document)
358
+
359
+ def appendChild(self, node: Node) -> Node:
360
+ self._insert(node, None)
361
+ return node
362
+
363
+ def insertBefore(self, node: Node, reference: Node | None) -> Node:
364
+ self._insert(node, reference)
365
+ return node
366
+
367
+ def replaceChild(self, node: Node, old: Node) -> Node:
368
+ if not isinstance(old, Node):
369
+ raise TypeError("Expected a Node")
370
+ self._insert(node, old, replacing=True)
371
+ return old
372
+
373
+ def removeChild(self, node: Node) -> Node:
374
+ if not isinstance(node, Node):
375
+ raise TypeError("Expected a Node")
376
+ if node._parent is not self:
377
+ raise ValueError("Node is not a child of this parent")
378
+ self._children.remove(node)
379
+ node._parent = None
380
+ return node
381
+
382
+
383
+ class ElementContainer(ParentNode):
384
+ def getElementsByTagName(self, tag: str) -> _LiveCollection[Element]:
385
+ if tag == "*":
386
+ return _LiveCollection(self._elements)
387
+ lower = ascii_lower(tag)
388
+ return _LiveCollection(
389
+ lambda: (
390
+ node
391
+ for node in self._elements()
392
+ if node.localName
393
+ == (lower if node.namespaceURI == HTML_NAMESPACE else tag)
394
+ )
395
+ )
396
+
397
+ def getElementsByClassName(self, class_name: str) -> _LiveCollection[Element]:
398
+ classes = set(tokens(class_name))
399
+ if not classes:
400
+ return _LiveCollection(tuple)
401
+
402
+ def members():
403
+ document = (
404
+ self if self.nodeType == self.DOCUMENT_NODE else self.ownerDocument
405
+ )
406
+ if document is not None and document.compatMode == "BackCompat":
407
+ folded = {ascii_lower(token) for token in classes}
408
+ return (
409
+ node
410
+ for node in self._elements()
411
+ if folded.issubset(map(ascii_lower, node.classList))
412
+ )
413
+ return (
414
+ node for node in self._elements() if classes.issubset(node.classList)
415
+ )
416
+
417
+ return _LiveCollection(members)
418
+
419
+ def getElementsByTag(self, tag: str) -> list[Element]:
420
+ return list(self.getElementsByTagName(tag))
421
+
422
+ def getElementsByClass(self, class_name: str) -> list[Element]:
423
+ return list(self.getElementsByClassName(class_name))
424
+
425
+
426
+ class DocumentFragment(ParentNode):
427
+ nodeName = "#document-fragment"
428
+ nodeType = Node.DOCUMENT_FRAGMENT_NODE
429
+
430
+ def __init__(self) -> None:
431
+ super().__init__()
432
+ self._host: Element | None = None
433
+
434
+ def getElementById(self, id: str) -> Element | None:
435
+ if not id:
436
+ return None
437
+ return next((node for node in self._elements() if node.id == id), None)
438
+
439
+
440
+ class ClassList(Sequence[str]):
441
+ """An ordered view of an element's class attribute."""
442
+
443
+ def __init__(self, element: Element) -> None:
444
+ self._element = element
445
+
446
+ @property
447
+ def value(self) -> str:
448
+ return self._element.className
449
+
450
+ @value.setter
451
+ def value(self, value: str) -> None:
452
+ self._element.className = value
453
+
454
+ def __iter__(self) -> Iterator[str]:
455
+ return iter(tokens(self.value))
456
+
457
+ def __getitem__(self, index):
458
+ return tokens(self.value)[index]
459
+
460
+ def __len__(self) -> int:
461
+ return len(tokens(self.value))
462
+
463
+ def __str__(self) -> str:
464
+ return self.value
465
+
466
+ @staticmethod
467
+ def _validate(*values: str) -> None:
468
+ for value in values:
469
+ if not isinstance(value, str):
470
+ raise TypeError("Expected a string class token")
471
+ if not value or any(char in HTML_WHITESPACE for char in value):
472
+ raise ValueError(
473
+ "Class tokens must be nonempty and contain no HTML whitespace"
474
+ )
475
+
476
+ def contains(self, value: str) -> bool:
477
+ return value in self
478
+
479
+ def add(self, *values: str) -> None:
480
+ self._validate(*values)
481
+ self.value = " ".join(dict.fromkeys([*self, *values]))
482
+
483
+ def remove(self, *values: str) -> None:
484
+ self._validate(*values)
485
+ if self._element.hasAttribute("class"):
486
+ self.value = " ".join(value for value in self if value not in values)
487
+
488
+ def toggle(self, value: str, force: bool | None = None) -> bool:
489
+ self._validate(value)
490
+ present = value in self
491
+ if force is None:
492
+ force = not present
493
+ if force and not present:
494
+ self.add(value)
495
+ if not force and present:
496
+ self.remove(value)
497
+ return bool(force)
498
+
499
+ def replace(self, old: str, new: str) -> bool:
500
+ self._validate(old, new)
501
+ if old not in self:
502
+ return False
503
+ self.value = " ".join(
504
+ dict.fromkeys(new if value == old else value for value in self)
505
+ )
506
+ return True
507
+
508
+
509
+ class Element(ElementContainer):
510
+ nodeType = Node.ELEMENT_NODE
511
+
512
+ def __init__(
513
+ self,
514
+ tag: str = "html",
515
+ attributes: dict[str, str] | None = None,
516
+ *,
517
+ namespace: str = HTML_NAMESPACE,
518
+ ) -> None:
519
+ super().__init__()
520
+ self._tag = ascii_lower(tag) if namespace == HTML_NAMESPACE else tag
521
+ self._namespace = namespace
522
+ self.attributes = dict(attributes or {})
523
+ self._template_content = None
524
+ if self.tag == "template" and self.namespaceURI == HTML_NAMESPACE:
525
+ self._template_content = DocumentFragment()
526
+ self._template_content._host = self
527
+
528
+ @property
529
+ def tag(self) -> str:
530
+ return self._tag
531
+
532
+ localName = tag
533
+
534
+ @property
535
+ def namespaceURI(self) -> str:
536
+ return self._namespace
537
+
538
+ @property
539
+ def tagName(self) -> str:
540
+ if self.namespaceURI == HTML_NAMESPACE:
541
+ return self.tag.translate(_ASCII_UPPER)
542
+ return self.tag
543
+
544
+ nodeName = tagName
545
+
546
+ @property
547
+ def templateContent(self) -> DocumentFragment | None:
548
+ return self._template_content
549
+
550
+ @property
551
+ def content(self) -> DocumentFragment | str:
552
+ if self.templateContent is not None:
553
+ return self.templateContent
554
+ return self.innerHTML
555
+
556
+ @property
557
+ def innerHTML(self) -> str:
558
+ from .serialization import serialize_children
559
+
560
+ return serialize_children(self)
561
+
562
+ @innerHTML.setter
563
+ def innerHTML(self, markup: str | None) -> None:
564
+ from .parser import DompyParser
565
+
566
+ fragment = DompyParser.parseFragment(
567
+ "" if markup is None else markup,
568
+ context=self,
569
+ scripting=self.ownerDocument._scripting
570
+ if self.ownerDocument is not None
571
+ else False,
572
+ )
573
+ target = self if self.templateContent is None else self.templateContent
574
+ target._replace_children(fragment._children)
575
+ fragment._children.clear()
576
+
577
+ @property
578
+ def outerHTML(self) -> str:
579
+ from .serialization import serialize
580
+
581
+ return serialize(self)
582
+
583
+ @property
584
+ def previousElementSibling(self) -> Element | None:
585
+ node = self.previousSibling
586
+ while node is not None and not isinstance(node, Element):
587
+ node = node.previousSibling
588
+ return node
589
+
590
+ @property
591
+ def nextElementSibling(self) -> Element | None:
592
+ node = self.nextSibling
593
+ while node is not None and not isinstance(node, Element):
594
+ node = node.nextSibling
595
+ return node
596
+
597
+ def _attribute_name(self, name: str) -> str:
598
+ return ascii_lower(name) if self.namespaceURI == HTML_NAMESPACE else name
599
+
600
+ def getAttribute(self, name: str) -> str | None:
601
+ return self.attributes.get(self._attribute_name(name))
602
+
603
+ def hasAttribute(self, name: str) -> bool:
604
+ return self._attribute_name(name) in self.attributes
605
+
606
+ def setAttribute(self, name: str, value: str) -> None:
607
+ if not name or any(char in HTML_WHITESPACE + "\x00\"'/>=" for char in name):
608
+ raise ValueError("Invalid attribute name")
609
+ self.attributes[self._attribute_name(name)] = str(value)
610
+
611
+ def removeAttribute(self, name: str) -> None:
612
+ self.attributes.pop(self._attribute_name(name), None)
613
+
614
+ @property
615
+ def id(self) -> str:
616
+ return self.getAttribute("id") or ""
617
+
618
+ @id.setter
619
+ def id(self, value: str) -> None:
620
+ self.setAttribute("id", value)
621
+
622
+ @property
623
+ def className(self) -> str:
624
+ return self.getAttribute("class") or ""
625
+
626
+ @className.setter
627
+ def className(self, value: str) -> None:
628
+ self.setAttribute("class", value)
629
+
630
+ @cached_property
631
+ def classList(self) -> ClassList:
632
+ return ClassList(self)
633
+
634
+ def matches(self, selector: str) -> bool:
635
+ from .selectors import match
636
+
637
+ return match(self, selector)
638
+
639
+ def closest(self, selector: str) -> Element | None:
640
+ from .selectors import closest
641
+
642
+ return closest(self, selector)
643
+
644
+ def _copy(self) -> Element:
645
+ return Element(self.tag, self.attributes, namespace=self.namespaceURI)
646
+
647
+ def __str__(self) -> str:
648
+ return self.outerHTML
649
+
650
+
651
+ class Tags:
652
+ DIV = "div"
653
+ LI = "li"
654
+ UL = "ul"
655
+ HEADER = "header"
656
+ FOOTER = "footer"
657
+ TITLE = "title"
658
+ HTML = "html"
659
+ BODY = "body"
660
+ HEAD = "head"
661
+ META = "meta"
662
+ LINK = "link"
663
+ INPUT = "input"
664
+ LABEL = "label"
665
+ SPAN = "span"
666
+ NAV = "nav"
667
+ IMG = "img"
668
+ H1 = "h1"
669
+ H2 = "h2"
670
+ H3 = "h3"
671
+ H4 = "h4"
672
+ H5 = "h5"
673
+ H6 = "h6"
674
+ P = "p"
675
+ A = "a"
676
+ MAIN = "main"
677
+ SECTION = "section"
678
+ I = "i" # noqa: E741
679
+ B = "b"
680
+ VIDEO = "video"
681
+ AUDIO = "audio"
682
+ SOURCE = "source"
683
+ FIGURE = "figure"
684
+ FIG_CAPTION = "figcaption"
685
+ BUTTON = "button"
686
+ AREA = "area"
687
+ BASE = "base"
688
+ BR = "br"
689
+ EMBED = "embed"
690
+ HR = "hr"
691
+ IFRAME = "iframe"
692
+ PARAM = "param"
693
+ TRACK = "track"
694
+ COMMAND = "command"
695
+
696
+ selfClosingTags = frozenset(
697
+ {
698
+ AREA,
699
+ BASE,
700
+ BR,
701
+ "col",
702
+ EMBED,
703
+ HR,
704
+ IMG,
705
+ INPUT,
706
+ LINK,
707
+ META,
708
+ PARAM,
709
+ SOURCE,
710
+ TRACK,
711
+ "wbr",
712
+ "basefont",
713
+ "bgsound",
714
+ "frame",
715
+ "keygen",
716
+ }
717
+ )