From 3b73f3c310017150f3a018ea2ca3f28cab6eb320 Mon Sep 17 00:00:00 2001 From: Mitar Date: Sat, 22 Jun 2019 14:08:18 -0700 Subject: [PATCH 1/3] bpo-37374: Do not escape quotes in minidom inside text segments. --- Lib/xml/dom/minidom.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index d09ef5e7d0371a..09bea38cd65973 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -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("&", "&").replace("<", "<"). \ + replace(">", ">") + writer.write(data) + def _write_data(writer, data): "Writes datachars to writer." if data: @@ -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)) # DOM Level 3 (WD 9 April 2002) From 60ad399a95142a84980d421f453c29e21e4907ac Mon Sep 17 00:00:00 2001 From: Mitar Date: Sat, 22 Jun 2019 14:18:59 -0700 Subject: [PATCH 2/3] Adding NEWS entry. --- .../NEWS.d/next/Library/2019-06-22-14-18-39.bpo-14312.3SBbwB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-22-14-18-39.bpo-14312.3SBbwB.rst diff --git a/Misc/NEWS.d/next/Library/2019-06-22-14-18-39.bpo-14312.3SBbwB.rst b/Misc/NEWS.d/next/Library/2019-06-22-14-18-39.bpo-14312.3SBbwB.rst new file mode 100644 index 00000000000000..111f1acee82da0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-22-14-18-39.bpo-14312.3SBbwB.rst @@ -0,0 +1 @@ +:mod:`minidom` does not escape quotes anymore inside text nodes. From 393923fed1a605bc2bac196566a0adf3b58f44b6 Mon Sep 17 00:00:00 2001 From: Mitar Date: Fri, 22 Oct 2021 00:14:25 +0200 Subject: [PATCH 3/3] Adding tests. --- Lib/test/test_minidom.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 1663b1f1143ddc..630eceae953911 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -5,6 +5,7 @@ import io from test import support import unittest +from xml.etree import ElementTree import xml.dom.minidom @@ -1663,5 +1664,14 @@ def test_cdata_parsing(self): dom2 = parseString(dom1.toprettyxml()) self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '') + def testQuoteEscape(self): + text = ElementTree.Element('text') + text.text = 'f&oo"br' + xml_string = ElementTree.tostring(text) + xml_tree = parseString(xml_string) + output = xml_tree.toprettyxml(indent=' ') + self.assertEqual(output.splitlines()[1], xml_string.decode('utf8')) + + if __name__ == "__main__": unittest.main()