Project

General

Profile

« Previous | Next » 

Revision 1017

xml_dom.py: Fixed bug in parent() where it didn't account for NodeParentIter's first element returned being the current node, not its parent. Refactored parent() to use parentNode directly, and NodeParentIter to use parent(), instead of the other way around.

View differences:

xml_dom.py
103 103

  
104 104
##### Parent nodes
105 105

  
106
class NodeParentIter:
106
def parent(node):
107 107
    '''Does not treat the document object as the root node's parent, since it's
108 108
    not a true element node'''
109
    parent_ = node.parentNode
110
    if parent_ != None and is_elem(parent_): return parent_
111
    else: return None
112

  
113
class NodeParentIter:
114
    '''See parent() for special treatment of root node.
115
    Note that the first element returned is the current node, not its parent.'''
109 116
    def __init__(self, node): self.node = node
110 117
    
111 118
    def __iter__(self): return self
112 119
    
113 120
    def curr(self):
114
        if self.node != None and is_elem(self.node): return self.node
115
        raise StopIteration
121
        if self.node != None: return self.node
122
        else: raise StopIteration
116 123
    
117 124
    def next(self):
118 125
        node = self.curr()
119
        self.node = self.node.parentNode
126
        self.node = parent(self.node)
120 127
        return node
121 128

  
122
def parent(node):
123
    '''See NodeParentIter for special treatment of root node'''
124
    try: return NodeParentIter(node).next()
125
    except StopIteration: return None
126

  
127 129
##### Element nodes containing text
128 130

  
129 131
def is_text_node(node): return node.nodeType == Node.TEXT_NODE

Also available in: Unified diff