Skip to content

Commit 335454d

Browse files
miss-islingtonencukouhugovk
authored andcommitted
[3.14] gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (GH-142794) (#142818)
gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (GH-142794) (cherry picked from commit 1cc7551) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent f4eb9ab commit 335454d

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
@@ -10,7 +10,7 @@
1010
import pyexpat
1111
import xml.dom.minidom
1212

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

@@ -195,6 +195,14 @@ def testAppendChildNoQuadraticComplexity(self):
195195
# This example used to take at least 30 seconds.
196196
self.assertLess(end - start, 1)
197197

198+
def testSetAttributeNodeWithoutOwnerDocument(self):
199+
# regression test for gh-142754
200+
elem = Element("test")
201+
attr = Attr("id")
202+
attr.value = "test-id"
203+
elem.setAttributeNode(attr)
204+
self.assertEqual(elem.getAttribute("id"), "test-id")
205+
198206
def testAppendChildFragment(self):
199207
dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
200208
dom.documentElement.appendChild(frag)

Lib/xml/dom/minidom.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ class Attr(Node):
348348
def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None,
349349
prefix=None):
350350
self.ownerElement = None
351+
self.ownerDocument = None
351352
self._name = qName
352353
self.namespaceURI = namespaceURI
353354
self._prefix = prefix
@@ -673,6 +674,7 @@ class Element(Node):
673674

674675
def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,
675676
localName=None):
677+
self.ownerDocument = None
676678
self.parentNode = None
677679
self.tagName = self.nodeName = tagName
678680
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)