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)
def fix_lists(dom): # <ins> and <del> tags are not allowed within <ul> or <ol> tags. # Move them to the nearest li, so that the numbering isn't interrupted. # Find all del > li and ins > li sets. del_tags = set() ins_tags = set() for node in list(dom.getElementsByTagName('li')): parent = node.parentNode if parent.tagName == 'del': del_tags.add(parent) elif parent.tagName == 'ins': ins_tags.add(parent) # Change ins > li into li > ins. for ins_tag in ins_tags: distribute(ins_tag) # Change del > li into li.del-li > del. for del_tag in del_tags: children = list(del_tag.childNodes) unwrap(del_tag) for c in children: if c.nodeName == 'li': c.setAttribute('class', 'del-li') wrap_inner(c, 'del')