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

1from __future__ import annotations 

2 

3from typing import TYPE_CHECKING, Iterator, Optional 

4 

5from .parent import Parent 

6 

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 

13 

14 

15class Root(Parent): 

16 """Root (Parent) represents a document. 

17 

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 """ 

21 

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 

29 

30 def __eq__(self, obj) -> bool: 

31 if obj is None: 

32 return False 

33 

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 

41 

42 def stringify(self) -> str: 

43 """Build indented html string of documents elements and their children. 

44 

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) 

51 

52 def __repr__(self) -> str: 

53 return f"root [{len(self.children)}]"