Skip to content

Commit c8faf9b

Browse files
committed
python#4147: minidom's toprettyxml no longer adds whitespace to text nodes.
Patch by Dan Kenigsberg.
1 parent f06eb46 commit c8faf9b

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎Lib/test/test_minidom.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ def testAltNewline(self):
439439
dom.unlink()
440440
self.confirm(domstr == str.replace("\n", "\r\n"))
441441

442+
def test_toPrettyXML_perserves_content_of_text_node(self):
443+
str = '<A>B</A>'
444+
dom = parseString(str)
445+
dom2 = parseString(dom.toprettyxml())
446+
self.assertEqual(dom.childNodes[0].childNodes[0].toxml(),
447+
dom2.childNodes[0].childNodes[0].toxml())
448+
442449
def testProcessingInstruction(self):
443450
dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
444451
pi = dom.documentElement.firstChild

‎Lib/xml/dom/minidom.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,9 @@ def writexml(self, writer, indent="", addindent="", newl=""):
806806
_write_data(writer, attrs[a_name].value)
807807
writer.write("\"")
808808
if self.childNodes:
809-
writer.write(">%s"%(newl))
809+
writer.write(">")
810+
if self.childNodes[0].nodeType != Node.TEXT_NODE:
811+
writer.write(newl)
810812
for node in self.childNodes:
811813
node.writexml(writer,indent+addindent,addindent,newl)
812814
writer.write("%s</%s>%s" % (indent,self.tagName,newl))
@@ -1031,7 +1033,7 @@ def splitText(self, offset):
10311033
return newText
10321034

10331035
def writexml(self, writer, indent="", addindent="", newl=""):
1034-
_write_data(writer, "%s%s%s"%(indent, self.data, newl))
1036+
_write_data(writer, self.data)
10351037

10361038
# DOM Level 3 (WD 9 April 2002)
10371039

0 commit comments

Comments
 (0)