Makes the given element the immediate predecessor of this one. The two elements will have the same parent, and the given element will be immediately before this one.
(self, predecessor)
| 366 | self.insert(len(self.contents), tag) |
| 367 | |
| 368 | def insert_before(self, predecessor): |
| 369 | """Makes the given element the immediate predecessor of this one. |
| 370 | |
| 371 | The two elements will have the same parent, and the given element |
| 372 | will be immediately before this one. |
| 373 | """ |
| 374 | if self is predecessor: |
| 375 | raise ValueError("Can't insert an element before itself.") |
| 376 | parent = self.parent |
| 377 | if parent is None: |
| 378 | raise ValueError( |
| 379 | "Element has no parent, so 'before' has no meaning.") |
| 380 | # Extract first so that the index won't be screwed up if they |
| 381 | # are siblings. |
| 382 | if isinstance(predecessor, PageElement): |
| 383 | predecessor.extract() |
| 384 | index = parent.index(self) |
| 385 | parent.insert(index, predecessor) |
| 386 | |
| 387 | def insert_after(self, successor): |
| 388 | """Makes the given element the immediate successor of this one. |