summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-09-28 04:33:06 (GMT)
committerFred Drake <fdrake@acm.org>2001-09-28 04:33:06 (GMT)
commit3ac6a09eed70b5ebff9200a22e0ff7f010b36d0d (patch)
tree71eb0a1840cecb28788ebbb0875f7c3f4911d1d2
parent2ed6bf87c9ce5d2e8a0eb33f7aa6503196205a0c (diff)
downloadcpython-3ac6a09eed70b5ebff9200a22e0ff7f010b36d0d.zip
cpython-3ac6a09eed70b5ebff9200a22e0ff7f010b36d0d.tar.gz
cpython-3ac6a09eed70b5ebff9200a22e0ff7f010b36d0d.tar.bz2
For Python 2.2 and newer, actually support the full NodeList interface by
subclassing list to add the length and item() attributes.
-rw-r--r--Lib/xml/dom/minidom.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 9db9ec1..18b0e27 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -32,6 +32,23 @@ del types
import xml.dom
_Node = xml.dom.Node
+
+if list is type([]):
+ class NodeList(list):
+ def item(self, index):
+ if 0 <= index < len(self):
+ return self[index]
+
+ def __getattr__(self, name):
+ if name == "length":
+ return len(self)
+ raise AttributeError, name
+
+else:
+ def NodeList():
+ return []
+
+
class Node(_Node):
allnodes = {}
_debug = 0
@@ -41,7 +58,7 @@ class Node(_Node):
namespaceURI = None # this is non-null only for elements and attributes
def __init__(self):
- self.childNodes = []
+ self.childNodes = NodeList()
self.parentNode = self.ownerDocument = None
if Node._debug:
index = repr(id(self)) + repr(self.__class__)
@@ -234,7 +251,7 @@ class Node(_Node):
clone = new.instance(self.__class__, self.__dict__.copy())
if self._makeParentNodes:
clone.parentNode = None
- clone.childNodes = []
+ clone.childNodes = NodeList()
if deep:
for child in self.childNodes:
clone.appendChild(child.cloneNode(1))