FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-54092: Implement writexml() for DocumentFragment in xml.dom.minidom by serhiy-storchaka · Pull Request #156646 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Doc/library/xml.dom.minidom.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: next
Namespace declarations missing for the serialized element
and its attributes are now written.
It now works for :class:`!DocumentFragment` nodes.

.. method:: Node.toxml(encoding=None, standalone=None)

Expand All @@ -179,6 +180,9 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: 3.9
The *standalone* parameter was added.

.. versionchanged:: next
It now works for :class:`!DocumentFragment` nodes.

.. method:: Node.toprettyxml(indent="\t", newl="\n", encoding=None, \
standalone=None)

Expand Down Expand Up @@ -207,6 +211,7 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: next
Whitespace is no longer added inside an element with mixed content
or marked with ``xml:space="preserve"``.
It now works for :class:`!DocumentFragment` nodes.

.. _dom-example:

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.16.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Implement :meth:`~xml.dom.minidom.Node.writexml` for document fragments
in :mod:`xml.dom.minidom`,
so that :meth:`~xml.dom.minidom.Node.toxml` and
:meth:`~xml.dom.minidom.Node.toprettyxml` now work for them.
They write the children of the fragment,
without adding a level of indentation.
Loading

Back | FazBrowse Home | New Git URL