For each child element, if matched, replace it with the text returned from the predicate.
(
parent: etree.Element, predicate: Callable[[etree.Element], str | None]
)
| 87 | |
| 88 | |
| 89 | def _replace_elements_with_text( |
| 90 | parent: etree.Element, predicate: Callable[[etree.Element], str | None] |
| 91 | ) -> None: |
| 92 | """For each child element, if matched, replace it with the text returned from the predicate.""" |
| 93 | carry_text = "" |
| 94 | for child in reversed(parent): # Reversed for the ability to mutate during iteration. |
| 95 | # Remove matching elements but carry any `tail` text to preceding elements. |
| 96 | new_text = predicate(child) |
| 97 | if new_text is not None: |
| 98 | carry_text = new_text + (child.tail or "") + carry_text |
| 99 | parent.remove(child) |
| 100 | elif carry_text: |
| 101 | child.tail = (child.tail or "") + carry_text |
| 102 | carry_text = "" |
| 103 | if carry_text: |
| 104 | parent.text = (parent.text or "") + carry_text |
no test coverage detected
searching dependent graphs…