def _is_junk(hashable_node): if isinstance(hashable_node, basestring): return is_text_junk(hashable_node) # Nodes with no text or just whitespace are junk. for descendant in walk_dom(hashable_node.node): if is_text(descendant): if not is_text_junk(descendant.nodeValue): return False return True
def sort_nodes(dom, cmp_func): """ Sort the nodes of the dom in-place, based on a comparison function. """ dom.normalize() for node in list(walk_dom(dom, elements_only=True)): prev_sib = node.previousSibling while prev_sib and cmp_func(prev_sib, node) == 1: node.parentNode.insertBefore(node, prev_sib) prev_sib = node.previousSibling
def test_node_compare(): del_node = list(walk_dom(parse_minidom('<del/>')))[-1] ins_node = list(walk_dom(parse_minidom('<ins/>')))[-1] assert -1 == node_compare(del_node, ins_node) assert 1 == node_compare(ins_node, del_node)
def split_text_nodes(dom): for text_node in list(walk_dom(dom)): if not is_text(text_node): continue split_node(text_node)