mirror of
https://github.com/python/cpython.git
synced 2025-09-25 17:59:57 +00:00
For Python 2.2 and newer, actually support the full NodeList interface by
subclassing list to add the length and item() attributes.
This commit is contained in:
parent
2ed6bf87c9
commit
3ac6a09eed
1 changed files with 19 additions and 2 deletions
|
@ -32,6 +32,23 @@ del types
|
||||||
import xml.dom
|
import xml.dom
|
||||||
_Node = xml.dom.Node
|
_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):
|
class Node(_Node):
|
||||||
allnodes = {}
|
allnodes = {}
|
||||||
_debug = 0
|
_debug = 0
|
||||||
|
@ -41,7 +58,7 @@ class Node(_Node):
|
||||||
namespaceURI = None # this is non-null only for elements and attributes
|
namespaceURI = None # this is non-null only for elements and attributes
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.childNodes = []
|
self.childNodes = NodeList()
|
||||||
self.parentNode = self.ownerDocument = None
|
self.parentNode = self.ownerDocument = None
|
||||||
if Node._debug:
|
if Node._debug:
|
||||||
index = repr(id(self)) + repr(self.__class__)
|
index = repr(id(self)) + repr(self.__class__)
|
||||||
|
@ -234,7 +251,7 @@ class Node(_Node):
|
||||||
clone = new.instance(self.__class__, self.__dict__.copy())
|
clone = new.instance(self.__class__, self.__dict__.copy())
|
||||||
if self._makeParentNodes:
|
if self._makeParentNodes:
|
||||||
clone.parentNode = None
|
clone.parentNode = None
|
||||||
clone.childNodes = []
|
clone.childNodes = NodeList()
|
||||||
if deep:
|
if deep:
|
||||||
for child in self.childNodes:
|
for child in self.childNodes:
|
||||||
clone.appendChild(child.cloneNode(1))
|
clone.appendChild(child.cloneNode(1))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue