- for elem in doc.iter():
- try:
- if elem.text:
- chunks = split_re.split(elem.text)
- while len(chunks) > 1:
- ins = etree.Element(tagname)
- ins.tail = chunks.pop()
- elem.insert(0, ins)
- elem.text = chunks.pop(0)
- if elem.tail:
- chunks = split_re.split(elem.tail)
- parent = elem.getparent()
- ins_index = parent.index(elem) + 1
- while len(chunks) > 1:
- ins = etree.Element(tagname)
- ins.tail = chunks.pop()
- parent.insert(ins_index, ins)
- elem.tail = chunks.pop(0)
- except TypeError, e:
- # element with no children, like comment
- pass
+ for elem in doc.iter(tag=etree.Element):
+ if exclude and elem.tag in exclude:
+ continue
+ if elem.text:
+ chunks = split_re.split(elem.text)
+ while len(chunks) > 1:
+ ins = etree.Element(tagname)
+ ins.tail = chunks.pop()
+ elem.insert(0, ins)
+ elem.text = chunks.pop(0)
+ if elem.tail:
+ chunks = split_re.split(elem.tail)
+ parent = elem.getparent()
+ ins_index = parent.index(elem) + 1
+ while len(chunks) > 1:
+ ins = etree.Element(tagname)
+ ins.tail = chunks.pop()
+ parent.insert(ins_index, ins)
+ elem.tail = chunks.pop(0)