]> granicus.if.org Git - python/commitdiff
#4147: minidom's toprettyxml no longer adds whitespace to text nodes.
authorR David Murray <rdmurray@bitdance.com>
Sat, 1 Oct 2011 20:49:25 +0000 (16:49 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 1 Oct 2011 20:49:25 +0000 (16:49 -0400)
Patch by Dan Kenigsberg.

Lib/test/test_minidom.py
Lib/xml/dom/minidom.py

index 7b7524a1225ca463db6ee95de43ca78f43d0a463..ecf05dd2e226504382a425f01a094a70c2dd4c7b 100644 (file)
@@ -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 = '<A>B</A>'
+        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('<e><?mypi \t\n data \t\n ?></e>')
         pi = dom.documentElement.firstChild
index 7518852798ba4a72021f97f56973a07b1dd7114b..a1f6f63a5d8ce821da7fab4c60b8a88d107f1f9c 100644 (file)
@@ -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>%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)