Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,28 @@ def testRenameAttribute(self):
self.checkRenameNodeSharedConstraints(doc, attr)
doc.unlink()

def testRenameAttributeToPrefixedName(self):
doc = parseString("<doc a='v' b='w'/>")
elem = doc.documentElement
attr = elem.getAttributeNode("a")
attr = doc.renameNode(attr, "http://xml.python.org/ns", "p:c")
self.assertEqual(attr.name, "p:c")
self.assertEqual(attr.prefix, "p")
self.assertEqual(attr.localName, "c")
self.assertEqual(attr.namespaceURI, "http://xml.python.org/ns")
self.assertIsNone(elem.getAttributeNode("a"))
self.assertIs(elem.getAttributeNodeNS("http://xml.python.org/ns", "c"),
attr)

# A rejected rename leaves the attribute on its element.
attr = elem.getAttributeNode("b")
self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, attr,
"http://xml.python.org/ns", "xml:lang")
self.assertEqual(attr.name, "b")
self.assertIsNone(attr.namespaceURI)
self.assertIs(elem.getAttributeNode("b"), attr)
doc.unlink()

def testRenameElement(self):
doc = parseString("<doc/>")
elem = doc.documentElement
Expand Down
4 changes: 3 additions & 1 deletion Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,15 +2129,17 @@ def renameNode(self, n, namespaceURI, name):
prefix = None
localName = None
if n.nodeType == Node.ATTRIBUTE_NODE:
if prefix is not None:
_check_prefix(prefix, namespaceURI, True)
element = n.ownerElement
if element is not None:
is_id = n._is_id
element.removeAttributeNode(n)
else:
element = None
n.namespaceURI = namespaceURI
n.prefix = prefix
n._localName = localName
n.namespaceURI = namespaceURI
n.nodeName = name
if n.nodeType == Node.ELEMENT_NODE:
n.tagName = name
Expand Down
Loading