From 2e5ec15f3b72317e33d7d053e5d83598d9411e7d Mon Sep 17 00:00:00 2001 From: Vladimir Surjaninov Date: Wed, 27 Mar 2019 08:58:49 +0300 Subject: [PATCH] bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (GH-12514) (cherry picked from commit 384b81d923addd52125e94470b11d2574ca266a9) Co-authored-by: Vladimir Surjaninov --- Lib/test/test_minidom.py | 16 ++++++++++++++++ Lib/xml/dom/minidom.py | 3 ++- .../2019-03-23-17-16-15.bpo-36407.LG3aC4.rst | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 2eb642395bbb6b..021147402b1a59 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1499,5 +1499,21 @@ def testEmptyXMLNSValue(self): def test_main(): support.run_unittest(MinidomTest) + 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__": test_main() diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 05649d620fac53..0e5a0fb379d525 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -806,7 +806,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) 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 00000000000000..3873329a51e11e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst @@ -0,0 +1,2 @@ +Fixed wrong indentation writing for CDATA section in xml.dom.minidom. +Patch by Vladimir Surjaninov.