Attribute nodes did not always get their ownerDocument and ownerElement

properly set.  This fixes that.
This commit is contained in:
Fred Drake 2001-12-06 18:27:48 +00:00
parent 6b04ffe9e5
commit 2998a55f2d
3 changed files with 41 additions and 4 deletions

View File

@ -1,5 +1,7 @@
test_minidom
Passed testAAA
Passed setAttribute() sets ownerDocument
Passed setAttribute() sets ownerElement
Test Succeeded testAAA
Passed assertion: len(Node.allnodes) == 0
Passed testAAB
@ -131,6 +133,12 @@ Test Succeeded testInsertBeforeFragment
Passed assertion: len(Node.allnodes) == 0
Test Succeeded testLegalChildren
Passed assertion: len(Node.allnodes) == 0
Passed NamedNodeMap.__setitem__() sets ownerDocument
Passed NamedNodeMap.__setitem__() sets ownerElement
Passed NamedNodeMap.__setitem__() sets value
Passed NamedNodeMap.__setitem__() sets nodeValue
Test Succeeded testNamedNodeMapSetItem
Passed assertion: len(Node.allnodes) == 0
Passed test NodeList.item()
Test Succeeded testNodeListItem
Passed assertion: len(Node.allnodes) == 0

View File

@ -166,6 +166,23 @@ def testLegalChildren():
elem.appendChild(text)
dom.unlink()
def testNamedNodeMapSetItem():
dom = Document()
elem = dom.createElement('element')
attrs = elem.attributes
attrs["foo"] = "bar"
a = attrs.item(0)
confirm(a.ownerDocument is dom,
"NamedNodeMap.__setitem__() sets ownerDocument")
confirm(a.ownerElement is elem,
"NamedNodeMap.__setitem__() sets ownerElement")
confirm(a.value == "bar",
"NamedNodeMap.__setitem__() sets value")
confirm(a.nodeValue == "bar",
"NamedNodeMap.__setitem__() sets nodeValue")
elem.unlink()
dom.unlink()
def testNonZero():
dom = parse(tstfile)
confirm(dom)# should not be zero
@ -188,6 +205,11 @@ def testAAA():
el = dom.documentElement
el.setAttribute("spam", "jam2")
confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
a = el.getAttributeNode("spam")
confirm(a.ownerDocument is dom,
"setAttribute() sets ownerDocument")
confirm(a.ownerElement is dom.documentElement,
"setAttribute() sets ownerElement")
dom.unlink()
def testAAB():

View File

@ -364,9 +364,10 @@ class NamedNodeMap:
attributes as found in an input document.
"""
def __init__(self, attrs, attrsNS):
def __init__(self, attrs, attrsNS, ownerElement):
self._attrs = attrs
self._attrsNS = attrsNS
self._ownerElement = ownerElement
try:
property
@ -430,6 +431,7 @@ class NamedNodeMap:
if type(value) in _StringTypes:
node = Attr(attname)
node.value = value
node.ownerDocument = self._ownerElement.ownerDocument
else:
if not isinstance(value, Attr):
raise TypeError, "value must be a string or Attr object"
@ -445,6 +447,7 @@ class NamedNodeMap:
old.unlink()
self._attrs[node.name] = node
self._attrsNS[(node.namespaceURI, node.localName)] = node
node.ownerElement = self._ownerElement
return old
def setNamedItemNS(self, node):
@ -518,14 +521,18 @@ class Element(Node):
def setAttribute(self, attname, value):
attr = Attr(attname)
# for performance
attr.__dict__["value"] = attr.__dict__["nodeValue"] = value
d = attr.__dict__
d["value"] = d["nodeValue"] = value
d["ownerDocument"] = self.ownerDocument
self.setAttributeNode(attr)
def setAttributeNS(self, namespaceURI, qualifiedName, value):
prefix, localname = _nssplit(qualifiedName)
# for performance
attr = Attr(qualifiedName, namespaceURI, localname, prefix)
attr.__dict__["value"] = attr.__dict__["nodeValue"] = value
d = attr.__dict__
d["value"] = d["nodeValue"] = value
d["ownerDocument"] = self.ownerDocument
self.setAttributeNode(attr)
def getAttributeNode(self, attrname):
@ -608,7 +615,7 @@ class Element(Node):
writer.write("/>%s"%(newl))
def _get_attributes(self):
return AttributeList(self._attrs, self._attrsNS)
return NamedNodeMap(self._attrs, self._attrsNS, self)
try:
property