Skip to content

Commit 384b81d

Browse files
vsurjaninovserhiy-storchaka
authored andcommitted
bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (GH-12514)
1 parent f760610 commit 384b81d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_minidom.py

+16
Original file line numberDiff line numberDiff line change
@@ -1631,5 +1631,21 @@ def test_toprettyxml_with_attributes_ordered(self):
16311631
'<?xml version="1.0" ?>\n'
16321632
'<curriculum status="public" company="example"/>\n')
16331633

1634+
def test_toprettyxml_with_cdata(self):
1635+
xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1636+
doc = parseString(xml_str)
1637+
self.assertEqual(doc.toprettyxml(),
1638+
'<?xml version="1.0" ?>\n'
1639+
'<root>\n'
1640+
'\t<node><![CDATA[</data>]]></node>\n'
1641+
'</root>\n')
1642+
1643+
def test_cdata_parsing(self):
1644+
xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1645+
dom1 = parseString(xml_str)
1646+
self.checkWholeText(dom1.getElementsByTagName('node')[0].firstChild, '</data>')
1647+
dom2 = parseString(dom1.toprettyxml())
1648+
self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '</data>')
1649+
16341650
if __name__ == "__main__":
16351651
unittest.main()

Lib/xml/dom/minidom.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,8 @@ def writexml(self, writer, indent="", addindent="", newl=""):
862862
if self.childNodes:
863863
writer.write(">")
864864
if (len(self.childNodes) == 1 and
865-
self.childNodes[0].nodeType == Node.TEXT_NODE):
865+
self.childNodes[0].nodeType in (
866+
Node.TEXT_NODE, Node.CDATA_SECTION_NODE)):
866867
self.childNodes[0].writexml(writer, '', '', '')
867868
else:
868869
writer.write(newl)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed wrong indentation writing for CDATA section in xml.dom.minidom.
2+
Patch by Vladimir Surjaninov.

0 commit comments

Comments
 (0)