Skip to content

Commit 2e5ec15

Browse files
vsurjaninovmiss-islington
authored andcommitted
bpo-36407: Fix writing indentations of CDATA section (xml.dom.minidom). (pythonGH-12514)
(cherry picked from commit 384b81d) Co-authored-by: Vladimir Surjaninov <[email protected]>
1 parent 6cbb4c0 commit 2e5ec15

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_minidom.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,5 +1499,21 @@ def testEmptyXMLNSValue(self):
14991499
def test_main():
15001500
support.run_unittest(MinidomTest)
15011501

1502+
def test_toprettyxml_with_cdata(self):
1503+
xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1504+
doc = parseString(xml_str)
1505+
self.assertEqual(doc.toprettyxml(),
1506+
'<?xml version="1.0" ?>\n'
1507+
'<root>\n'
1508+
'\t<node><![CDATA[</data>]]></node>\n'
1509+
'</root>\n')
1510+
1511+
def test_cdata_parsing(self):
1512+
xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1513+
dom1 = parseString(xml_str)
1514+
self.checkWholeText(dom1.getElementsByTagName('node')[0].firstChild, '</data>')
1515+
dom2 = parseString(dom1.toprettyxml())
1516+
self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '</data>')
1517+
15021518
if __name__ == "__main__":
15031519
test_main()

Lib/xml/dom/minidom.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,8 @@ def writexml(self, writer, indent="", addindent="", newl=""):
806806
if self.childNodes:
807807
writer.write(">")
808808
if (len(self.childNodes) == 1 and
809-
self.childNodes[0].nodeType == Node.TEXT_NODE):
809+
self.childNodes[0].nodeType in (
810+
Node.TEXT_NODE, Node.CDATA_SECTION_NODE)):
810811
self.childNodes[0].writexml(writer, '', '', '')
811812
else:
812813
writer.write(newl)
Lines changed: 2 additions & 0 deletions
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)