diff options
author | Henry Harutyunyan <henryharutyunyan@gmail.com> | 2020-02-29 08:22:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-29 08:22:19 (GMT) |
commit | dc04a0571e362cd3de040771d7705cb107ae26fc (patch) | |
tree | 0184295482e356caa91427ae37a2541723da0b82 /Lib/xml | |
parent | 02673352b5db6ca4d3dc804965facbedfe66425d (diff) | |
download | cpython-dc04a0571e362cd3de040771d7705cb107ae26fc.zip cpython-dc04a0571e362cd3de040771d7705cb107ae26fc.tar.gz cpython-dc04a0571e362cd3de040771d7705cb107ae26fc.tar.bz2 |
bpo-37534: Allow adding Standalone Document Declaration when generating XML documents (GH-14912)
Diffstat (limited to 'Lib/xml')
-rw-r--r-- | Lib/xml/dom/minidom.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 464420b..1083b48 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -43,10 +43,11 @@ class Node(xml.dom.Node): def __bool__(self): return True - def toxml(self, encoding=None): - return self.toprettyxml("", "", encoding) + def toxml(self, encoding=None, standalone=None): + return self.toprettyxml("", "", encoding, standalone) - def toprettyxml(self, indent="\t", newl="\n", encoding=None): + def toprettyxml(self, indent="\t", newl="\n", encoding=None, + standalone=None): if encoding is None: writer = io.StringIO() else: @@ -56,7 +57,7 @@ class Node(xml.dom.Node): newline='\n') if self.nodeType == Node.DOCUMENT_NODE: # Can pass encoding only to document, to put it into XML header - self.writexml(writer, "", indent, newl, encoding) + self.writexml(writer, "", indent, newl, encoding, standalone) else: self.writexml(writer, "", indent, newl) if encoding is None: @@ -1787,12 +1788,17 @@ class Document(Node, DocumentLS): raise xml.dom.NotSupportedErr("cannot import document type nodes") return _clone_node(node, deep, self) - def writexml(self, writer, indent="", addindent="", newl="", encoding=None): - if encoding is None: - writer.write('<?xml version="1.0" ?>'+newl) - else: - writer.write('<?xml version="1.0" encoding="%s"?>%s' % ( - encoding, newl)) + def writexml(self, writer, indent="", addindent="", newl="", encoding=None, + standalone=None): + declarations = [] + + if encoding: + declarations.append(f'encoding="{encoding}"') + if standalone is not None: + declarations.append(f'standalone="{"yes" if standalone else "no"}"') + + writer.write(f'<?xml version="1.0" {" ".join(declarations)}?>{newl}') + for node in self.childNodes: node.writexml(writer, indent, addindent, newl) |