[Python-checkins] cpython (3.2): #4147: minidom's toprettyxml no longer adds whitespace to text nodes.

r.david.murray python-checkins at python.org
Sat Oct 1 22:49:14 CEST 2011


http://hg.python.org/cpython/rev/086ca132e161
changeset:   72570:086ca132e161
branch:      3.2
parent:      72567:65e7f40fefd4
user:        R David Murray <rdmurray at bitdance.com>
date:        Sat Oct 01 16:19:51 2011 -0400
summary:
  #4147: minidom's toprettyxml no longer adds whitespace to text nodes.

Patch by Dan Kenigsberg.

files:
  Lib/test/test_minidom.py |  7 +++++++
  Lib/xml/dom/minidom.py   |  6 ++++--
  Misc/ACKS                |  1 +
  Misc/NEWS                |  2 ++
  4 files changed, 14 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -446,6 +446,13 @@
         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
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -836,7 +836,9 @@
             _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))
@@ -1061,7 +1063,7 @@
         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)
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -468,6 +468,7 @@
 Hiroaki Kawai
 Sebastien Keim
 Ryan Kelly
+Dan Kenigsberg
 Robert Kern
 Randall Kern
 Magnus Kessler
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@
 Library
 -------
 
+- Issue #4147: minidom's toprettyxml no longer adds whitespace to text nodes.
+
 - Issue #13034: When decoding some SSL certificates, the subjectAltName
   extension could be unreported.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list