Coverage for phml\nodes\text.py: 100%
15 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 functools import cached_property
3from .literal import Literal
6class Text(Literal):
7 """Text (Literal) represents a Text ([DOM]).
9 Example:
11 ```html
12 <span>Foxtrot</span>
13 ```
15 Yields:
17 ```javascript
18 {
19 type: 'element',
20 tagName: 'span',
21 properties: {},
22 children: [{type: 'text', value: 'Foxtrot'}]
23 }
24 ```
25 """
27 @cached_property
28 def num_lines(self) -> int:
29 """Determine the number of lines the text has."""
30 return len([line for line in self.value.split("\n") if line.strip() != ""])
32 def stringify(self, indent: int = 0) -> str:
33 """Build indented html string of html text.
35 Returns:
36 str: Built html of text
37 """
38 if self.parent is None or not any(
39 tag in self.get_ancestry() for tag in ["pre", "python", "script", "style"]
40 ):
41 lines = [line.lstrip() for line in self.value.split("\n") if line.strip() != ""]
42 for i, line in enumerate(lines):
43 lines[i] = (' ' * indent) + line
44 return "\n".join(lines)
45 return self.value
47 def __repr__(self) -> str:
48 return f"literal.text('{self.value}')"