Replace marker with elem.
(self, root: etree.Element, elem: etree.Element)
| 298 | yield from self.iterparent(child) |
| 299 | |
| 300 | def replace_marker(self, root: etree.Element, elem: etree.Element) -> None: |
| 301 | """ Replace marker with elem. """ |
| 302 | for (p, c) in self.iterparent(root): |
| 303 | text = ''.join(c.itertext()).strip() |
| 304 | if not text: |
| 305 | continue |
| 306 | |
| 307 | # To keep the output from screwing up the |
| 308 | # validation by putting a `<div>` inside of a `<p>` |
| 309 | # we actually replace the `<p>` in its entirety. |
| 310 | |
| 311 | # The `<p>` element may contain more than a single text content |
| 312 | # (`nl2br` can introduce a `<br>`). In this situation, `c.text` returns |
| 313 | # the very first content, ignore children contents or tail content. |
| 314 | # `len(c) == 0` is here to ensure there is only text in the `<p>`. |
| 315 | if c.text and c.text.strip() == self.marker and len(c) == 0: |
| 316 | for i in range(len(p)): |
| 317 | if p[i] == c: |
| 318 | p[i] = elem |
| 319 | break |
| 320 | |
| 321 | def set_level(self, elem: etree.Element) -> None: |
| 322 | """ Adjust header level according to base level. """ |