Project

General

Profile

« Previous | Next » 

Revision 4331

xml_dom.py: replace(): Support a list of new nodes to replace the old node with

View differences:

lib/xml_dom.py
7 7
from xml.dom import Node
8 8
import xml.dom.minidom as minidom
9 9

  
10
import lists
10 11
import strings
11 12
import util
12 13

  
......
36 37
def replace(old, new):
37 38
    '''@param new Node|None'''
38 39
    assert old.parentNode != None # not removed from parent tree
40
    if new == None: new = []
41
    else: new = lists.mk_seq(new)
39 42
    
40
    if new == None: old.parentNode.removeChild(old)
41
    else: old.parentNode.replaceChild(new, old) # note order reversed
43
    olds_parent = old.parentNode
44
    if not new: olds_parent.removeChild(old)
45
    else: # at least one node
46
        last = new.pop(-1)
47
        olds_parent.replaceChild(last, old) # note arg order reversed
48
        for node in reversed(new):
49
            olds_parent.insertBefore(node, last) # there is no insertAfter()
42 50

  
43 51
def bool2str(val):
44 52
    '''For use with replace_with_text()'''

Also available in: Unified diff