Beispiel #1
0
def _internalize_changes_markup(dom, child_tag_names):
    # Delete tags are always ordered first.
    for del_tag in list(dom.getElementsByTagName("del")):
        ins_tag = del_tag.nextSibling
        # The one child tag of `del_tag` should be child_tag_names
        if len(del_tag.childNodes) != 1:
            continue
        if ins_tag is None or len(ins_tag.childNodes) != 1:
            continue
        if ins_tag.tagName != "ins":
            continue
        deleted_tag = del_tag.firstChild
        if not is_element(deleted_tag):
            continue
        if deleted_tag.tagName not in child_tag_names:
            continue
        # The one child tag of `ins_tag` should be child_tag_names
        inserted_tag = ins_tag.firstChild
        if not is_element(inserted_tag):
            continue
        if inserted_tag.tagName not in child_tag_names:
            continue

        attributes = dict([key, value] for key, value in inserted_tag.attributes.items())
        nodes_to_unwrap = [deleted_tag, inserted_tag]
        for n in nodes_to_unwrap:
            unwrap(n)
        new_node = wrap_nodes([del_tag, ins_tag], inserted_tag.tagName)
        for key, value in attributes.items():
            new_node.setAttribute(key, value)
def distribute(node):
    """
    Wrap a copy of the given element around the contents of each of its
    children, removing the node in the process.
    """
    children = list(c for c in node.childNodes if is_element(c))
    unwrap(node)
    tag_name = node.tagName
    for c in children:
        wrap_inner(c, tag_name)