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
2 changes: 2 additions & 0 deletions Doc/library/xml.dom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ inherits properties from :class:`Node`.

Raise :exc:`WrongDocumentErr` if *n* was created by another document,
and :exc:`NotSupportedErr` if it is neither an element nor an attribute.
Raise :exc:`InvalidCharacterErr` if *name* is not a valid XML name,
and :exc:`NamespaceErr` if it is not valid for *namespaceURI*.


.. _dom-element-objects:
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,11 @@ def testNamespaceErr(self):
("http://xml.python.org/ns", "p:1e"),
("http://xml.python.org/ns", "xml:e"), # the xml prefix
]:
for node in (doc.createElement("e"), doc.createAttribute("a")):
with self.subTest(namespaceURI=namespaceURI, qname=qname,
nodeType=node.nodeType):
self.assertRaises(xml.dom.NamespaceErr,
doc.renameNode, node, namespaceURI, qname)
with self.subTest(namespaceURI=namespaceURI, qname=qname):
self.assertRaises(xml.dom.NamespaceErr,
doc.createElementNS, namespaceURI, qname)
Expand All @@ -2433,6 +2438,10 @@ def testNamespaceErr(self):
(XMLNS_NS, "p:a"), # and it allows nothing else
(XMLNS_NS, "a"),
]:
with self.subTest(namespaceURI=namespaceURI, qname=qname,
method="renameNode"):
self.assertRaises(xml.dom.NamespaceErr, doc.renameNode,
doc.createAttribute("a"), namespaceURI, qname)
with self.subTest(namespaceURI=namespaceURI, qname=qname):
self.assertRaises(xml.dom.NamespaceErr,
doc.createAttributeNS, namespaceURI, qname)
Expand Down Expand Up @@ -2471,6 +2480,10 @@ def testInvalidCharacterErr(self):
doc = parseString("<doc/>")
impl = getDOMImplementation()
for name in ("", "bad name", "1st", "-x", ".x", "a<b", "a&b", "a\tb"):
for node in (doc.createElement("e"), doc.createAttribute("a")):
with self.subTest(name=name, nodeType=node.nodeType):
self.assertRaises(xml.dom.InvalidCharacterErr,
doc.renameNode, node, None, name)
with self.subTest(name=name):
self.assertRaises(xml.dom.InvalidCharacterErr,
doc.createElement, name)
Expand Down
2 changes: 2 additions & 0 deletions Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,8 @@ def renameNode(self, n, namespaceURI, name):
if n.nodeType not in (Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE):
raise xml.dom.NotSupportedErr(
"renameNode() only applies to element and attribute nodes")
_check_qualified_name(
namespaceURI, name, n.nodeType == Node.ATTRIBUTE_NODE)
if namespaceURI != EMPTY_NAMESPACE:
if ':' in name:
prefix, localName = name.split(':', 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make :meth:`~xml.dom.Document.renameNode` validate qualified names against
their namespace URI, raising :exc:`~xml.dom.InvalidCharacterErr` or
:exc:`~xml.dom.NamespaceErr` for invalid input. Patch by tonghuaroot.
Loading