Coverage for phml\utils\transform\extract.py: 0%

18 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-30 09:38 -0600

1from phml.builder import p 

2from phml.nodes import AST, All_Nodes, Comment, Element, Root, Text 

3 

4 

5def to_string(node: AST | All_Nodes) -> str: 

6 """Get the raw text content of the element. Works similar to 

7 the DOMs Node#textContent getter. 

8 

9 Args: 

10 node (Root | Element | Text): Node to get the text content from 

11 

12 Returns: 

13 str: Raw inner text without formatting. 

14 """ 

15 

16 if isinstance(node, AST): 

17 node = node.tree 

18 

19 if isinstance(node, Text | Comment): 

20 return node.value 

21 

22 def concat_text(el: Element | Root) -> list[str]: 

23 result = [] 

24 

25 for child in el.children: 

26 if isinstance(child, (Element, Root)): 

27 result.extend(concat_text(child)) 

28 elif isinstance(child, Text): 

29 result.append(child.value) 

30 return result 

31 

32 if isinstance(node, Root | Element): 

33 # Recursive concat 

34 return "".join(concat_text(node)) 

35 

36 return None