Skip to content

Commit c3b3b59

Browse files
gh-44871: Document the exceptions raised by the DOM interface
The DOM requires InvalidCharacterErr for an invalid XML name, WrongDocumentErr for a node created by another document, and HierarchyRequestErr for making a node a descendant of itself. None of this was documented.
1 parent 9349d7b commit c3b3b59

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Doc/library/xml.dom.minidom.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ rules apply:
266266
.. versionchanged:: next
267267
Previously, :attr:`~xml.dom.Attr.specified` was always ``False``.
268268

269+
* The constraints of the DOM are now enforced,
270+
and the corresponding exceptions are raised.
271+
272+
.. versionchanged:: next
273+
Previously, many invalid operations silently succeeded
274+
and produced an invalid document.
275+
269276
The following interfaces have no implementation in :mod:`!xml.dom.minidom`:
270277

271278
* :class:`DOMTimeStamp`

Doc/library/xml.dom.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ DOM Level 2 added the ability to create new :class:`Document` and
232232
*qualifiedName*, *publicId*, and *systemId* strings, representing the
233233
information contained in an XML document type declaration.
234234

235+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
236+
235237

236238
.. _dom-node-objects:
237239

@@ -457,6 +459,10 @@ for each node type are:
457459
children, returning *newChild*. If the node was already in
458460
the tree, it is removed first.
459461

462+
Raise :exc:`WrongDocumentErr` if *newChild* was created by another
463+
document, and :exc:`HierarchyRequestErr` if it is this node itself or
464+
its ancestor.
465+
460466

461467
.. method:: Node.insertBefore(newChild, refChild)
462468

@@ -465,6 +471,10 @@ for each node type are:
465471
*newChild* is returned. If *refChild* is ``None``, it inserts *newChild* at the
466472
end of the children's list.
467473

474+
Raise :exc:`WrongDocumentErr` if *newChild* was created by another
475+
document, and :exc:`HierarchyRequestErr` if it is this node itself or
476+
its ancestor.
477+
468478

469479
.. method:: Node.removeChild(oldChild)
470480

@@ -479,6 +489,10 @@ for each node type are:
479489
Replace an existing node with a new node. It must be the case that *oldChild*
480490
is a child of this node; if not, :exc:`ValueError` is raised.
481491

492+
Raise :exc:`WrongDocumentErr` if *newChild* was created by another
493+
document, and :exc:`HierarchyRequestErr` if it is this node itself or
494+
its ancestor.
495+
482496

483497
.. method:: Node.normalize()
484498

@@ -665,6 +679,8 @@ inherits properties from :class:`Node`.
665679
document when it is created. You need to explicitly insert it with one of the
666680
other methods such as :meth:`~Node.insertBefore` or :meth:`~Node.appendChild`.
667681

682+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
683+
668684

669685
.. method:: Document.createElementNS(namespaceURI, tagName)
670686

@@ -673,6 +689,8 @@ inherits properties from :class:`Node`.
673689
need to explicitly insert it with one of the other methods such as
674690
:meth:`~Node.insertBefore` or :meth:`~Node.appendChild`.
675691

692+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
693+
676694

677695
.. method:: Document.createTextNode(data)
678696

@@ -686,6 +704,8 @@ inherits properties from :class:`Node`.
686704
Create and return a new entity reference node.
687705
The node is not inserted into the document when it is created.
688706

707+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
708+
689709
.. versionadded:: next
690710

691711

@@ -702,6 +722,8 @@ inherits properties from :class:`Node`.
702722
*data* passed as parameters. As with the other creation methods, this one does
703723
not insert the node into the tree.
704724

725+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
726+
705727

706728
.. method:: Document.createAttribute(name)
707729

@@ -710,6 +732,8 @@ inherits properties from :class:`Node`.
710732
:meth:`~Element.setAttributeNode` on the appropriate :class:`Element` object
711733
to use the newly created attribute instance.
712734

735+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
736+
713737

714738
.. method:: Document.createAttributeNS(namespaceURI, qualifiedName)
715739

@@ -718,6 +742,8 @@ inherits properties from :class:`Node`.
718742
element. You must use :meth:`~Element.setAttributeNode` on the appropriate
719743
:class:`Element` object to use the newly created attribute instance.
720744

745+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
746+
721747

722748
.. method:: Document.getElementById(id)
723749

@@ -854,6 +880,8 @@ of that class.
854880

855881
Set an attribute value from a string.
856882

883+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
884+
857885

858886
.. method:: Element.setAttributeNode(newAttr)
859887

@@ -862,6 +890,8 @@ of that class.
862890
occurs, the old attribute node will be returned. If *newAttr* is already in use,
863891
:exc:`InuseAttributeErr` will be raised.
864892

893+
Raise :exc:`WrongDocumentErr` if *newAttr* was created by another document.
894+
865895

866896
.. method:: Element.setAttributeNodeNS(newAttr)
867897

@@ -871,12 +901,16 @@ of that class.
871901
returned. If *newAttr* is already in use, :exc:`InuseAttributeErr` will be
872902
raised.
873903

904+
Raise :exc:`WrongDocumentErr` if *newAttr* was created by another document.
905+
874906

875907
.. method:: Element.setAttributeNS(namespaceURI, qname, value)
876908

877909
Set an attribute value from a string, given a *namespaceURI* and a *qname*.
878910
Note that a qname is the whole attribute name. This is different than above.
879911

912+
Raise :exc:`InvalidCharacterErr` if the name is not a valid XML name.
913+
880914

881915
.. _dom-attr-objects:
882916

@@ -982,13 +1016,19 @@ NamedNodeMap Objects
9821016
Add *node* to the map, using its :attr:`~Attr.name` as the key.
9831017
Return the node which it replaces, or ``None`` if it replaces no node.
9841018

1019+
Raise :exc:`WrongDocumentErr` if *node* was created by another document,
1020+
and :exc:`InuseAttributeErr` if it belongs to another element.
1021+
9851022

9861023
.. method:: NamedNodeMap.setNamedItemNS(node)
9871024

9881025
Add *node* to the map,
9891026
using its namespace URI and local name as the key.
9901027
Return the node which it replaces, or ``None`` if it replaces no node.
9911028

1029+
Raise :exc:`WrongDocumentErr` if *node* was created by another document,
1030+
and :exc:`InuseAttributeErr` if it belongs to another element.
1031+
9921032

9931033
.. method:: NamedNodeMap.removeNamedItem(name)
9941034

0 commit comments

Comments
 (0)