Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-37374: Do not escape quotes in minidom inside text segments #14312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 10 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
from test import support
import unittest
from xml.etree import ElementTree

import xml.dom.minidom

Expand Down Expand Up @@ -1663,5 +1664,14 @@ def test_cdata_parsing(self):
dom2 = parseString(dom1.toprettyxml())
self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '</data>')

def testQuoteEscape(self):
text = ElementTree.Element('text')
text.text = 'f&oo"b<a>r'
xml_string = ElementTree.tostring(text)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not rely on an unrelated library providing the same text string serialisation. Instead, the test should use a plain string to compare against. Otherwise, changes in ElementTree could make this test fail without need.

xml_tree = parseString(xml_string)
output = xml_tree.toprettyxml(indent=' ')
self.assertEqual(output.splitlines()[1], xml_string.decode('utf8'))


if __name__ == "__main__":
unittest.main()
9 changes: 8 additions & 1 deletion Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ def _in_document(node):
node = node.parentNode
return False

def _write_text_data(writer, data):
"Writes datachars of text node to writer."
if data:
data = data.replace("&", "&amp;").replace("<", "&lt;"). \
replace(">", "&gt;")
writer.write(data)

def _write_data(writer, data):
"Writes datachars to writer."
if data:
Expand Down Expand Up @@ -1110,7 +1117,7 @@ def splitText(self, offset):
return newText

def writexml(self, writer, indent="", addindent="", newl=""):
_write_data(writer, "%s%s%s" % (indent, self.data, newl))
_write_text_data(writer, "%s%s%s" % (indent, self.data, newl))
Comment on lines 1119 to +1120
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't checked deeply, but my gut feeling would prefer a special case for attribute values, not for text content. Can't say if that's similarly easy to achieve.


# DOM Level 3 (WD 9 April 2002)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`minidom` does not escape quotes anymore inside text nodes.