[Python-checkins] bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (GH-12514)

Serhiy Storchaka webhook-mailer at python.org
Wed Mar 27 01:59:03 EDT 2019


https://github.com/python/cpython/commit/384b81d923addd52125e94470b11d2574ca266a9
commit: 384b81d923addd52125e94470b11d2574ca266a9
branch: master
author: Vladimir Surjaninov <vsurjaninov at gmail.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2019年03月27日T07:58:49+02:00
summary:
bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (GH-12514)
files:
A Misc/NEWS.d/next/Library/2019-03-23-17-16-15.bpo-36407.LG3aC4.rst
M Lib/test/test_minidom.py
M Lib/xml/dom/minidom.py
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index f3ef958b5353..70965854ed1b 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -1631,5 +1631,21 @@ def test_toprettyxml_with_attributes_ordered(self):
 '<?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()
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 43569ddcbeac..464420b76598 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)
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 000000000000..3873329a51e1
--- /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.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /