Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 13 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,19 @@ def testEncodings(self):

doc.unlink()

def testStandalone(self):
doc = parseString('<foo>&#x20ac;</foo>')
self.assertEqual(doc.toxml(),
'<?xml version="1.0" ?><foo>\u20ac</foo>')
self.assertEqual(doc.toxml(standalone=None),
'<?xml version="1.0" ?><foo>\u20ac</foo>')
self.assertEqual(doc.toxml(standalone=True),
'<?xml version="1.0" standalone=\'yes\'?><foo>\u20ac</foo>')
self.assertEqual(doc.toxml(standalone=False),
'<?xml version="1.0" standalone=\'no\'?><foo>\u20ac</foo>')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both values should use the same quotes here. Since both the version and the encoding used double quotes, please also use double quotes for standalone.

Please also add a test that declares an encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, missed the test case with an encoding.
Added and the quote styles are made consistent either.


doc.unlink()

class UserDataHandler:
called = 0
def handle(self, operation, key, data, src, dst):
Expand Down
28 changes: 19 additions & 9 deletions Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -56,7 +57,7 @@ def toprettyxml(self, indent="\t", newl="\n", encoding=None):
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:
Expand Down Expand Up @@ -1787,12 +1788,21 @@ def importNode(self, node, deep):
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)
def writexml(self, writer, indent="", addindent="", newl="", encoding=None,
standalone=None):
# In case standalone declaration is set
if standalone is not None:
standalone = "standalone='{}'".format('yes' if standalone else 'no')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put a space before standalone=… here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. Added.

else:
writer.write('<?xml version="1.0" encoding="%s"?>%s' % (
encoding, newl))
standalone = ''

writer.write(
'<?xml version="1.0" {encoding}{standalone}?>{newline}'.format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to use f-strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Done as well.

encoding="encoding=\"{}\"".format(encoding) if encoding else '',
standalone=standalone,
newline=newl)
)

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

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1883,3 +1883,4 @@ Robert Leenders
Tim Hopper
Dan Lidral-Porter
Ngalim Siregar
Henrik Harutyunyan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intended name sorting is alphabetically by last name. Please try to adhere to that.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When using minidom module to generate XML documents the ability to add Standalone Document Declaration is added.
All the changes are made to generate a document in compliance with Extensible Markup Language (XML) 1.0 (Fifth Edition) W3C Recommendation (available here: https://www.w3.org/TR/xml/#sec-prolog-dtd).