Replace marker with elem.
(self, root: etree.Element, elem: etree.Element)
| 276 | yield from self.iterparent(child) |
| 277 | |
| 278 | def replace_marker(self, root: etree.Element, elem: etree.Element) -> None: |
| 279 | """ Replace marker with elem. """ |
| 280 | for (p, c) in self.iterparent(root): |
| 281 | text = ''.join(c.itertext()).strip() |
| 282 | if not text: |
| 283 | continue |
| 284 | |
| 285 | # To keep the output from screwing up the |
| 286 | # validation by putting a `<div>` inside of a `<p>` |
| 287 | # we actually replace the `<p>` in its entirety. |
| 288 | |
| 289 | # The `<p>` element may contain more than a single text content |
| 290 | # (`nl2br` can introduce a `<br>`). In this situation, `c.text` returns |
| 291 | # the very first content, ignore children contents or tail content. |
| 292 | # `len(c) == 0` is here to ensure there is only text in the `<p>`. |
| 293 | if c.text and c.text.strip() == self.marker and len(c) == 0: |
| 294 | for i in range(len(p)): |
| 295 | if p[i] == c: |
| 296 | p[i] = elem |
| 297 | break |
| 298 | |
| 299 | def set_level(self, elem: etree.Element) -> None: |
| 300 | """ Adjust header level according to base level. """ |