Skip to content

Commit 9de89b7

Browse files
gh-44871: Only walk ancestors when the new child has children
A node without children cannot be an ancestor, so testing this first keeps appending leaf nodes linear in the depth of the tree. Also fix references which do not resolve on main.
1 parent f59bfab commit 9de89b7

3 files changed

Lines changed: 6 additions & 4 deletions

File tree

Doc/library/xml.dom.minidom.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ This reflects information in the XML document that is not of general
259259
utility to most DOM users.
260260

261261
.. versionchanged:: next
262-
:class:`EntityReference` is now implemented.
262+
:class:`~xml.dom.EntityReference` is now implemented.
263263
Note that the parser expands entity references,
264264
so they only occur in a document if created explicitly.
265265

Doc/library/xml.dom.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,8 @@ EntityReference Objects
816816
.. class:: EntityReference
817817

818818
Represents an entity reference in the XML document.
819-
It is a subclass of :class:`Node`.
820-
The name of the referenced entity is its :attr:`nodeName`.
819+
It is a subclass of :class:`!Node`.
820+
The name of the referenced entity is its :attr:`Node.nodeName`.
821821
Its children are the replacement text of the entity,
822822
and are read-only.
823823

Lib/xml/dom/minidom.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def _check_new_child(self, newChild):
9393
if newChild is self:
9494
raise xml.dom.HierarchyRequestErr(
9595
"%s cannot be child of itself" % repr(self))
96-
if _is_ancestor(newChild, self):
96+
# A node without children cannot be an ancestor, and testing this
97+
# first keeps appending leaf nodes linear in the depth of the tree.
98+
if newChild.childNodes and _is_ancestor(newChild, self):
9799
raise xml.dom.HierarchyRequestErr(
98100
"%s is an ancestor of %s" % (repr(newChild), repr(self)))
99101

0 commit comments

Comments
 (0)