def remove_element(element: etree._Element, keep_children=False) -> None: """ Removes the given element from its tree. Unless ``keep_children`` is passed as ``True``, its children vanish with it into void. """ if keep_children: for child in element: element.addprevious(child) element.getparent().remove(element)
def _copy_root_siblings(source: etree._Element, target: etree._Element): stack = [] current_element = source.getprevious() while current_element is not None: stack.append(current_element) current_element = current_element.getprevious() while stack: target.addprevious(copy(stack.pop())) stack = [] current_element = source.getnext() while current_element is not None: stack.append(current_element) current_element = current_element.getnext() while stack: target.addnext(copy(stack.pop()))
def move_elements( to_move_list: Iterable[_Element], adjacent_el: _Element, put_after_adjacent: bool = False, ) -> None: """ Move elements inside or into an element after or before specified element in the element. to_move_list -- elements to be moved adjacent_el -- the element next to which the moved elements will be put put_after_adjacent -- put elements after (True) or before (False) the adjacent element """ for el in to_move_list: if put_after_adjacent: adjacent_el.addnext(el) adjacent_el = el else: adjacent_el.addprevious(el)