Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ac6a09

Browse files
committed
For Python 2.2 and newer, actually support the full NodeList interface by
subclassing list to add the length and item() attributes.
1 parent 2ed6bf8 commit 3ac6a09

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/xml/dom/minidom.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@
3232
import xml.dom
3333
_Node = xml.dom.Node
3434

35+
36+
if list is type([]):
37+
class NodeList(list):
38+
def item(self, index):
39+
if 0 <= index < len(self):
40+
return self[index]
41+
42+
def __getattr__(self, name):
43+
if name == "length":
44+
return len(self)
45+
raise AttributeError, name
46+
47+
else:
48+
def NodeList():
49+
return []
50+
51+
3552
class Node(_Node):
3653
allnodes = {}
3754
_debug = 0
@@ -41,7 +58,7 @@ class Node(_Node):
4158
namespaceURI = None # this is non-null only for elements and attributes
4259

4360
def __init__(self):
44-
self.childNodes = []
61+
self.childNodes = NodeList()
4562
self.parentNode = self.ownerDocument = None
4663
if Node._debug:
4764
index = repr(id(self)) + repr(self.__class__)
@@ -234,7 +251,7 @@ def cloneNode(self, deep):
234251
clone = new.instance(self.__class__, self.__dict__.copy())
235252
if self._makeParentNodes:
236253
clone.parentNode = None
237-
clone.childNodes = []
254+
clone.childNodes = NodeList()
238255
if deep:
239256
for child in self.childNodes:
240257
clone.appendChild(child.cloneNode(1))

0 commit comments

Comments
 (0)