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

This commit is contained in:
Vladimir Surjaninov 2019-03-27 08:58:49 +03:00 committed by Serhiy Storchaka
parent f760610bdd
commit 384b81d923
3 changed files with 20 additions and 1 deletions

View 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()

View 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)

View File

@ -0,0 +1,2 @@
Fixed wrong indentation writing for CDATA section in xml.dom.minidom.
Patch by Vladimir Surjaninov.