gh-54092: Implement writexml() for DocumentFragment in xml.dom.minidom by serhiy-storchaka · Pull Request #156646 · python/cpython · GitHub
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
5 changes: 5 additions & 0 deletions Doc/library/xml.dom.minidom.rst
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ xml
rather than defaulted from the DTD.
(Contributed by Jason Orendorff and Serhiy Storchaka in :gh:`44871`.)

* The :meth:`~xml.dom.minidom.Node.writexml`,
:meth:`~xml.dom.minidom.Node.toxml` and
:meth:`~xml.dom.minidom.Node.toprettyxml` methods
now work for :class:`!DocumentFragment` nodes in :mod:`xml.dom.minidom`.
(Contributed by Serhiy Storchaka in :gh:`54092`.)

* :class:`~xml.etree.ElementTree.XMLPullParser` and
:func:`~xml.etree.ElementTree.iterparse` now support the *target* parameter.
The reported object is the value returned by the corresponding method of
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,21 @@ def testWriteXML(self):
dom.unlink()
self.assertEqual(str, domstr)

def testWriteXMLDocumentFragment(self):
dom = parseString('<doc><a b="c"/>text<!--comment--></doc>')
frag = dom.createDocumentFragment()
for node in list(dom.documentElement.childNodes):
frag.appendChild(node)
self.assertEqual(frag.toxml(), '<a b="c"/>text<!--comment-->')
self.assertEqual(frag.toprettyxml(),
'<a b="c"/>\ntext\n<!--comment-->\n')
# the fragment itself does not add a level of indentation
writer = io.StringIO()
frag.writexml(writer, " ", " ", "\n")
self.assertEqual(writer.getvalue(),
' <a b="c"/>\n text\n <!--comment-->\n')
self.assertEqual(dom.createDocumentFragment().toxml(), '')

def testWriteXMLNamespaceDeclarations(self):
dom = Document()
root = dom.appendChild(
Expand Down
4 changes: 4 additions & 0 deletions Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ class DocumentFragment(Node):
def __init__(self):
self.childNodes = NodeList()

def writexml(self, writer, indent="", addindent="", newl=""):
for node in self.childNodes:
node.writexml(writer, indent, addindent, newl)


class Attr(Node):
__slots__=('_name', '_value', 'namespaceURI',
Expand Down
Loading