Coverage for phml\nodes\root.py: 32%
28 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-30 09:38 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-30 09:38 -0600
1from __future__ import annotations
3from typing import TYPE_CHECKING, Iterator, Optional
5from .parent import Parent
7if TYPE_CHECKING:
8 from .comment import Comment
9 from .doctype import DocType
10 from .element import Element
11 from .position import Position
12 from .text import Text
15class Root(Parent):
16 """Root (Parent) represents a document.
18 Root can be used as the root of a tree, or as a value
19 of the content field on a 'template' Element, never as a child.
20 """
22 def __init__(
23 self,
24 position: Optional[Position] = None,
25 children: Optional[list] = None,
26 ): # pylint: disable=useless-parent-delegation
27 super().__init__(position, children)
28 self.parent = None
30 def __eq__(self, obj) -> bool:
31 if obj is None:
32 return False
34 if hasattr(obj, "type") and self.type == obj.type:
35 for c, oc in zip(self.children, obj.children):
36 if c != oc:
37 return False
38 return True
39 else:
40 return False
42 def stringify(self) -> str:
43 """Build indented html string of documents elements and their children.
45 Returns:
46 str: Built html of document
47 """
48 out = []
49 out.extend([child.stringify() for child in self.children])
50 return "\n".join(out)
52 def __repr__(self) -> str:
53 return f"root [{len(self.children)}]"