Coverage for phml\transform\extract.py: 100%
18 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 16:33 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 16:33 -0600
1from phml.nodes import AST, All_Nodes, Comment, Element, Root, Text
3__all__ = ["to_string"]
6def to_string(node: AST | All_Nodes) -> str:
7 """Get the raw text content of the element. Works similar to
8 the DOMs Node#textContent getter.
10 Args:
11 node (Root | Element | Text): Node to get the text content from
13 Returns:
14 str: Raw inner text without formatting.
15 """
17 if isinstance(node, AST):
18 node = node.tree
20 if isinstance(node, Text | Comment):
21 return node.value
23 def concat_text(element: Element | Root) -> list[str]:
24 result = []
26 for child in element.children:
27 if isinstance(child, (Element, Root)):
28 result.extend(concat_text(child))
29 elif isinstance(child, Text):
30 result.append(child.value)
31 return result
33 if isinstance(node, Root | Element):
34 # Recursive concat
35 return " ".join(concat_text(node))
37 return None