Skip to content

Commit dc86e6d

Browse files
gh-54092: Implement writexml() for DocumentFragment in xml.dom.minidom
toxml() and toprettyxml() now work for document fragments. They write the children of the fragment, without adding a level of indentation.
1 parent d636e3c commit dc86e6d

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

Doc/library/xml.dom.minidom.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ module documentation. This section lists the differences between the API and
154154
.. versionchanged:: 3.9
155155
The *standalone* parameter was added.
156156

157+
.. versionchanged:: next
158+
It now works for :class:`!DocumentFragment` nodes.
159+
157160
.. method:: Node.toxml(encoding=None, standalone=None)
158161

159162
Return a string or byte string containing the XML represented by
@@ -175,6 +178,9 @@ module documentation. This section lists the differences between the API and
175178
.. versionchanged:: 3.9
176179
The *standalone* parameter was added.
177180

181+
.. versionchanged:: next
182+
It now works for :class:`!DocumentFragment` nodes.
183+
178184
.. method:: Node.toprettyxml(indent="\t", newl="\n", encoding=None, \
179185
standalone=None)
180186

@@ -194,6 +200,9 @@ module documentation. This section lists the differences between the API and
194200
.. versionchanged:: 3.9
195201
The *standalone* parameter was added.
196202

203+
.. versionchanged:: next
204+
It now works for :class:`!DocumentFragment` nodes.
205+
197206
.. _dom-example:
198207

199208
DOM Example

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,12 @@ xml
648648
rather than defaulted from the DTD.
649649
(Contributed by Jason Orendorff and Serhiy Storchaka in :gh:`44871`.)
650650

651+
* The :meth:`~xml.dom.minidom.Node.writexml`,
652+
:meth:`~xml.dom.minidom.Node.toxml` and
653+
:meth:`~xml.dom.minidom.Node.toprettyxml` methods
654+
now work for :class:`!DocumentFragment` nodes in :mod:`xml.dom.minidom`.
655+
(Contributed by Serhiy Storchaka in :gh:`54092`.)
656+
651657
zipfile
652658
-------
653659

Lib/test/test_minidom.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,22 @@ def testWriteXML(self):
561561
dom.unlink()
562562
self.assertEqual(str, domstr)
563563

564+
def testWriteXMLDocumentFragment(self):
565+
dom = parseString('<doc><a b="c"/>text<!--comment--></doc>')
566+
frag = dom.createDocumentFragment()
567+
for node in list(dom.documentElement.childNodes):
568+
frag.appendChild(node)
569+
self.assertEqual(frag.toxml(), '<a b="c"/>text<!--comment-->')
570+
self.assertEqual(frag.toprettyxml(),
571+
'<a b="c"/>\ntext\n<!--comment-->\n')
572+
# the fragment itself does not add a level of indentation
573+
writer = io.StringIO()
574+
frag.writexml(writer, " ", " ", "\n")
575+
self.assertEqual(writer.getvalue(),
576+
' <a b="c"/>\n text\n <!--comment-->\n')
577+
self.assertEqual(dom.createDocumentFragment().toxml(), '')
578+
dom.unlink()
579+
564580
def test_toxml_quote_text(self):
565581
dom = Document()
566582
elem = dom.appendChild(dom.createElement('elem'))

Lib/xml/dom/minidom.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ class DocumentFragment(Node):
379379
def __init__(self):
380380
self.childNodes = NodeList()
381381

382+
def writexml(self, writer, indent="", addindent="", newl=""):
383+
for node in self.childNodes:
384+
node.writexml(writer, indent, addindent, newl)
385+
382386

383387
class Attr(Node):
384388
__slots__=('_name', '_value', 'namespaceURI',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Implement :meth:`~xml.dom.minidom.Node.writexml` for document fragments
2+
in :mod:`xml.dom.minidom`,
3+
so that :meth:`~xml.dom.minidom.Node.toxml` and
4+
:meth:`~xml.dom.minidom.Node.toprettyxml` now work for them.
5+
They write the children of the fragment,
6+
without adding a level of indentation.

0 commit comments

Comments
 (0)