Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
gh-142754: Ensure that Element & Attr instances have the ownerDocumen…
…t attribute (GH-142794)

(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
  • Loading branch information
2 people authored and miss-islington committed Dec 16, 2025
commit 484fe636eb6c1108ba25d8276a06002e406b9b88
10 changes: 9 additions & 1 deletion Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import xml.dom.minidom

from xml.dom.minidom import parse, Attr, Node, Document, parseString
from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
from xml.dom.minidom import getDOMImplementation
from xml.parsers.expat import ExpatError

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

def testSetAttributeNodeWithoutOwnerDocument(self):
# regression test for gh-142754
elem = Element("test")
attr = Attr("id")
attr.value = "test-id"
elem.setAttributeNode(attr)
self.assertEqual(elem.getAttribute("id"), "test-id")

def testAppendChildFragment(self):
dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
dom.documentElement.appendChild(frag)
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 @@ -364,6 +364,7 @@ class Attr(Node):
def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None,
prefix=None):
self.ownerElement = None
self.ownerDocument = None
self._name = qName
self.namespaceURI = namespaceURI
self._prefix = prefix
Expand Down Expand Up @@ -689,6 +690,7 @@ class Element(Node):

def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,
localName=None):
self.ownerDocument = None
self.parentNode = None
self.tagName = self.nodeName = tagName
self.prefix = prefix
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add the *ownerDocument* attribute to :mod:`xml.dom.minidom` elements and attributes
created by directly instantiating the ``Element`` or ``Attr`` class. Note that
this way of creating nodes is not supported; creator functions like
:py:meth:`xml.dom.Document.documentElement` should be used instead.
Loading