From 159b20b10570e97551c0efde5cb6887f11736b23 Mon Sep 17 00:00:00 2001 From: Vladimir Surjaninov Date: Sat, 23 Mar 2019 18:40:52 +0300 Subject: [PATCH 1/4] bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom) --- Lib/test/test_minidom.py | 16 ++++++++++++++++ Lib/xml/dom/minidom.py | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index f3ef958b535373f..aa430b6f8d5f5d7 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1631,5 +1631,21 @@ def test_toprettyxml_with_attributes_ordered(self): '\n' '\n') + def testCDATAWriting(self): + doc = Document() + root = doc.createElement('root') + doc.appendChild(root) + node = doc.createElement('node') + root.appendChild(node) + data = doc.createCDATASection('') + node.appendChild(data) + + doc1 = parseString(doc.toxml()) + val1 = doc1.getElementsByTagName('node')[0].firstChild.nodeValue + doc2 = parseString(doc.toprettyxml()) + val2 = doc2.getElementsByTagName('node')[0].firstChild.nodeValue + + self.confirm(val1 == val2) + if __name__ == "__main__": unittest.main() diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 43569ddcbeacd9d..464420b76598e07 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -862,7 +862,8 @@ def writexml(self, writer, indent="", addindent="", newl=""): if self.childNodes: writer.write(">") if (len(self.childNodes) == 1 and - self.childNodes[0].nodeType == Node.TEXT_NODE): + self.childNodes[0].nodeType in ( + Node.TEXT_NODE, Node.CDATA_SECTION_NODE)): self.childNodes[0].writexml(writer, '', '', '') else: writer.write(newl) From 70f3c4a46b3850fc424985eda08babe1d39d3ea2 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sat, 23 Mar 2019 17:16:16 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst diff --git a/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst b/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst new file mode 100644 index 000000000000000..937fa988f67add3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst @@ -0,0 +1 @@ +Fixed wrong indentation writing for CDATA section in xml.dom.minidom \ No newline at end of file From 84ff016ccd82fd14d34521615c267e2918062e3d Mon Sep 17 00:00:00 2001 From: Vladimir Surjaninov Date: Sat, 23 Mar 2019 23:54:19 +0300 Subject: [PATCH 3/4] Update test cases --- Lib/test/test_minidom.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index aa430b6f8d5f5d7..70965854ed1b1cc 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1631,21 +1631,21 @@ def test_toprettyxml_with_attributes_ordered(self): '\n' '\n') - def testCDATAWriting(self): - doc = Document() - root = doc.createElement('root') - doc.appendChild(root) - node = doc.createElement('node') - root.appendChild(node) - data = doc.createCDATASection('') - node.appendChild(data) - - doc1 = parseString(doc.toxml()) - val1 = doc1.getElementsByTagName('node')[0].firstChild.nodeValue - doc2 = parseString(doc.toprettyxml()) - val2 = doc2.getElementsByTagName('node')[0].firstChild.nodeValue - - self.confirm(val1 == val2) + def test_toprettyxml_with_cdata(self): + xml_str = ']]>' + doc = parseString(xml_str) + self.assertEqual(doc.toprettyxml(), + '\n' + '\n' + '\t]]>\n' + '\n') + + def test_cdata_parsing(self): + xml_str = ']]>' + dom1 = parseString(xml_str) + self.checkWholeText(dom1.getElementsByTagName('node')[0].firstChild, '') + dom2 = parseString(dom1.toprettyxml()) + self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '') if __name__ == "__main__": unittest.main() From 889677cd402cdfe199730976cb28dd473c036b8d Mon Sep 17 00:00:00 2001 From: Vladimir Surjaninov Date: Mon, 25 Mar 2019 19:45:00 +0300 Subject: [PATCH 4/4] Update news --- .../next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst b/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst index 937fa988f67add3..3873329a51e11e0 100644 --- a/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst +++ b/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst @@ -1 +1,2 @@ -Fixed wrong indentation writing for CDATA section in xml.dom.minidom \ No newline at end of file +Fixed wrong indentation writing for CDATA section in xml.dom.minidom. +Patch by Vladimir Surjaninov.