MCPcopy
hub / github.com/csev/py4e / insert_before

Method insert_before

code3/bs4/element.py:368–385  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 1

test_insert_beforeMethod · 0.45

Calls 3

extractMethod · 0.45
indexMethod · 0.45
insertMethod · 0.45

Tested by 1

test_insert_beforeMethod · 0.36