Skip to content

Commit 1cc7551

Browse files
encukouhugovk
andauthored
gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (#142794)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent c10ec48 commit 1cc7551

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_minidom.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import xml.dom.minidom
1111

12-
from xml.dom.minidom import parse, Attr, Node, Document, parseString
12+
from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
1313
from xml.dom.minidom import getDOMImplementation
1414
from xml.parsers.expat import ExpatError
1515

@@ -191,6 +191,14 @@ def testAppendChildNoQuadraticComplexity(self):
191191
# This example used to take at least 30 seconds.
192192
self.assertLess(end - start, 1)
193193

194+
def testSetAttributeNodeWithoutOwnerDocument(self):
195+
# regression test for gh-142754
196+
elem = Element("test")
197+
attr = Attr("id")
198+
attr.value = "test-id"
199+
elem.setAttributeNode(attr)
200+
self.assertEqual(elem.getAttribute("id"), "test-id")
201+
194202
def testAppendChildFragment(self):
195203
dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
196204
dom.documentElement.appendChild(frag)

Lib/xml/dom/minidom.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ class Attr(Node):
364364
def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None,
365365
prefix=None):
366366
self.ownerElement = None
367+
self.ownerDocument = None
367368
self._name = qName
368369
self.namespaceURI = namespaceURI
369370
self._prefix = prefix
@@ -689,6 +690,7 @@ class Element(Node):
689690

690691
def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,
691692
localName=None):
693+
self.ownerDocument = None
692694
self.parentNode = None
693695
self.tagName = self.nodeName = tagName
694696
self.prefix = prefix
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add the *ownerDocument* attribute to :mod:`xml.dom.minidom` elements and attributes
2+
created by directly instantiating the ``Element`` or ``Attr`` class. Note that
3+
this way of creating nodes is not supported; creator functions like
4+
:py:meth:`xml.dom.Document.documentElement` should be used instead.

0 commit comments

Comments
 (0)