MCPcopy Index your code
hub / github.com/csev/py4e / insert

Method insert

code3/pagerank/bs4/element.py:296–358  ·  view source on GitHub ↗
(self, position, new_child)

Source from the content-addressed store, hash-verified

294 _lastRecursiveChild = _last_descendant
295
296 def insert(self, position, new_child):
297 if new_child is self:
298 raise ValueError("Cannot insert a tag into itself.")
299 if (isinstance(new_child, str)
300 and not isinstance(new_child, NavigableString)):
301 new_child = NavigableString(new_child)
302
303 position = min(position, len(self.contents))
304 if hasattr(new_child, 'parent') and new_child.parent is not None:
305 # We're 'inserting' an element that's already one
306 # of this object's children.
307 if new_child.parent is self:
308 current_index = self.index(new_child)
309 if current_index < position:
310 # We're moving this element further down the list
311 # of this object's children. That means that when
312 # we extract this element, our target index will
313 # jump down one.
314 position -= 1
315 new_child.extract()
316
317 new_child.parent = self
318 previous_child = None
319 if position == 0:
320 new_child.previous_sibling = None
321 new_child.previous_element = self
322 else:
323 previous_child = self.contents[position - 1]
324 new_child.previous_sibling = previous_child
325 new_child.previous_sibling.next_sibling = new_child
326 new_child.previous_element = previous_child._last_descendant(False)
327 if new_child.previous_element is not None:
328 new_child.previous_element.next_element = new_child
329
330 new_childs_last_element = new_child._last_descendant(False)
331
332 if position >= len(self.contents):
333 new_child.next_sibling = None
334
335 parent = self
336 parents_next_sibling = None
337 while parents_next_sibling is None and parent is not None:
338 parents_next_sibling = parent.next_sibling
339 parent = parent.parent
340 if parents_next_sibling is not None:
341 # We found the element that comes next in the document.
342 break
343 if parents_next_sibling is not None:
344 new_childs_last_element.next_element = parents_next_sibling
345 else:
346 # The last element of this tag is the last element in
347 # the document.
348 new_childs_last_element.next_element = None
349 else:
350 next_child = self.contents[position]
351 new_child.next_sibling = next_child
352 if new_child.next_sibling is not None:
353 new_child.next_sibling.previous_sibling = new_child

Callers 15

appendMethod · 0.95
axisFunction · 0.45
d3_voronoi_tessellateFunction · 0.45
replace_withMethod · 0.45
unwrapMethod · 0.45
insert_beforeMethod · 0.45
insert_afterMethod · 0.45
test_new_tag_creationMethod · 0.45
test_insert_stringMethod · 0.45
test_insert_tagMethod · 0.45

Calls 5

minFunction · 0.85
NavigableStringClass · 0.70
indexMethod · 0.45
extractMethod · 0.45
_last_descendantMethod · 0.45