]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (GH-12514)
authorVladimir Surjaninov <vsurjaninov@gmail.com>
Wed, 27 Mar 2019 05:58:49 +0000 (08:58 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 27 Mar 2019 05:58:49 +0000 (07:58 +0200)
Lib/test/test_minidom.py
Lib/xml/dom/minidom.py
Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst [new file with mode: 0644]

index f3ef958b535373ffa1417a7cbf5cfb2dffafe081..70965854ed1b1ccfb87ea6e6a3baf04395268be1 100644 (file)
@@ -1631,5 +1631,21 @@ class MinidomTest(unittest.TestCase):
                          '<?xml version="1.0" ?>\n'
                          '<curriculum status="public" company="example"/>\n')
 
+    def test_toprettyxml_with_cdata(self):
+        xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
+        doc = parseString(xml_str)
+        self.assertEqual(doc.toprettyxml(),
+                         '<?xml version="1.0" ?>\n'
+                         '<root>\n'
+                         '\t<node><![CDATA[</data>]]></node>\n'
+                         '</root>\n')
+
+    def test_cdata_parsing(self):
+        xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
+        dom1 = parseString(xml_str)
+        self.checkWholeText(dom1.getElementsByTagName('node')[0].firstChild, '</data>')
+        dom2 = parseString(dom1.toprettyxml())
+        self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '</data>')
+
 if __name__ == "__main__":
     unittest.main()
index 43569ddcbeacd9d2bca3309bc8df594aa9fe4bd2..464420b76598e07ea38aabc9a22705c0650c7471 100644 (file)
@@ -862,7 +862,8 @@ class Element(Node):
         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 (file)
index 0000000..3873329
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed wrong indentation writing for CDATA section in xml.dom.minidom.
+Patch by Vladimir Surjaninov.