From c8faf9bb0cdd5faa957fbd3aa38b637c68cdee34 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Sat, 1 Oct 2011 16:49:25 -0400 Subject: [PATCH] #4147: minidom's toprettyxml no longer adds whitespace to text nodes. Patch by Dan Kenigsberg. --- Lib/test/test_minidom.py | 7 +++++++ Lib/xml/dom/minidom.py | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 7b7524a1225..ecf05dd2e22 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -439,6 +439,13 @@ class MinidomTest(unittest.TestCase): dom.unlink() self.confirm(domstr == str.replace("\n", "\r\n")) + def test_toPrettyXML_perserves_content_of_text_node(self): + str = 'B' + dom = parseString(str) + dom2 = parseString(dom.toprettyxml()) + self.assertEqual(dom.childNodes[0].childNodes[0].toxml(), + dom2.childNodes[0].childNodes[0].toxml()) + def testProcessingInstruction(self): dom = parseString('') pi = dom.documentElement.firstChild diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 7518852798b..a1f6f63a5d8 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -806,7 +806,9 @@ class Element(Node): _write_data(writer, attrs[a_name].value) writer.write("\"") if self.childNodes: - writer.write(">%s"%(newl)) + writer.write(">") + if self.childNodes[0].nodeType != Node.TEXT_NODE: + writer.write(newl) for node in self.childNodes: node.writexml(writer,indent+addindent,addindent,newl) writer.write("%s%s" % (indent,self.tagName,newl)) @@ -1031,7 +1033,7 @@ class Text(CharacterData): return newText def writexml(self, writer, indent="", addindent="", newl=""): - _write_data(writer, "%s%s%s"%(indent, self.data, newl)) + _write_data(writer, self.data) # DOM Level 3 (WD 9 April 2002)