Coverage for phml\utils\transform\extract.py: 83%
18 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 11:07 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 11:07 -0600
1# pylint: disable=missing-module-docstring
2from phml.nodes import AST, All_Nodes, Comment, Element, Root, Text
4__all__ = ["to_string"]
7def to_string(node: AST | All_Nodes) -> str:
8 """Get the raw text content of the element. Works similar to
9 the DOMs Node#textContent getter.
11 Args:
12 node (Root | Element | Text): Node to get the text content from
14 Returns:
15 str: Raw inner text without formatting.
16 """
18 if isinstance(node, AST):
19 node = node.tree
21 if isinstance(node, Text | Comment):
22 return node.value
24 def concat_text(element: Element | Root) -> list[str]:
25 result = []
27 for child in element.children:
28 if isinstance(child, (Element, Root)):
29 result.extend(concat_text(child))
30 elif isinstance(child, Text):
31 result.append(child.value)
32 return result
34 if isinstance(node, Root | Element):
35 # Recursive concat
36 return " ".join(concat_text(node))
38 return None