[Python-checkins] cpython (2.7): #4147: minidom's toprettyxml no longer adds whitespace around a text node when

ezio.melotti python-checkins at python.org
Fri Nov 18 16:36:19 CET 2011


http://hg.python.org/cpython/rev/7262f8f276ff
changeset:   73605:7262f8f276ff
branch:      2.7
parent:      73602:ce34e9223450
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Fri Nov 18 17:30:28 2011 +0200
summary:
  #4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element.  Initial patch by Dan Kenigsberg.

files:
  Lib/test/test_minidom.py |  37 ++++++++++++++++++++++++---
  Lib/xml/dom/minidom.py   |  14 ++++++---
  Misc/NEWS                |   4 +++
  3 files changed, 45 insertions(+), 10 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
@@ -439,12 +439,39 @@
         dom.unlink()
         self.confirm(domstr == str.replace("\n", "\r\n"))
 
+    def test_toprettyxml_with_text_nodes(self):
+        # see issue #4147, text nodes are not indented
+        decl = '<?xml version="1.0" ?>\n'
+        self.assertEqual(parseString('<B>A</B>').toprettyxml(),
+                         decl + '<B>A</B>\n')
+        self.assertEqual(parseString('<C>A<B>A</B></C>').toprettyxml(),
+                         decl + '<C>\n\tA\n\t<B>A</B>\n</C>\n')
+        self.assertEqual(parseString('<C><B>A</B>A</C>').toprettyxml(),
+                         decl + '<C>\n\t<B>A</B>\n\tA\n</C>\n')
+        self.assertEqual(parseString('<C><B>A</B><B>A</B></C>').toprettyxml(),
+                         decl + '<C>\n\t<B>A</B>\n\t<B>A</B>\n</C>\n')
+        self.assertEqual(parseString('<C><B>A</B>A<B>A</B></C>').toprettyxml(),
+                         decl + '<C>\n\t<B>A</B>\n\tA\n\t<B>A</B>\n</C>\n')
+
+    def test_toprettyxml_with_adjacent_text_nodes(self):
+        # see issue #4147, adjacent text nodes are indented normally
+        dom = Document()
+        elem = dom.createElement(u'elem')
+        elem.appendChild(dom.createTextNode(u'TEXT'))
+        elem.appendChild(dom.createTextNode(u'TEXT'))
+        dom.appendChild(elem)
+        decl = '<?xml version="1.0" ?>\n'
+        self.assertEqual(dom.toprettyxml(),
+                         decl + '<elem>\n\tTEXT\n\tTEXT\n</elem>\n')
+
     def test_toprettyxml_preserves_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())
+        # see issue #4147
+        for str in ('<B>A</B>', '<A><B>C</B></A>'):
+            dom = parseString(str)
+            dom2 = parseString(dom.toprettyxml())
+            self.assertEqual(
+                dom.getElementsByTagName('B')[0].childNodes[0].toxml(),
+                dom2.getElementsByTagName('B')[0].childNodes[0].toxml())
 
     def testProcessingInstruction(self):
         dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
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
@@ -807,11 +807,15 @@
             writer.write("\"")
         if self.childNodes:
             writer.write(">")
-            if self.childNodes[0].nodeType != Node.TEXT_NODE:
+            if (len(self.childNodes) == 1 and
+                self.childNodes[0].nodeType == Node.TEXT_NODE):
+                self.childNodes[0].writexml(writer, '', '', '')
+            else:
                 writer.write(newl)
-            for node in self.childNodes:
-                node.writexml(writer,indent+addindent,addindent,newl)
-            writer.write("%s</%s>%s" % (indent,self.tagName,newl))
+                for node in self.childNodes:
+                    node.writexml(writer, indent+addindent, addindent, newl)
+                writer.write(indent)
+            writer.write("</%s>%s" % (self.tagName, newl))
         else:
             writer.write("/>%s"%(newl))
 
@@ -1033,7 +1037,7 @@
         return newText
 
     def writexml(self, writer, indent="", addindent="", newl=""):
-        _write_data(writer, self.data)
+        _write_data(writer, "%s%s%s" % (indent, self.data, newl))
 
     # DOM Level 3 (WD 9 April 2002)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -79,6 +79,10 @@
 Library
 -------
 
+- Issue #4147: minidom's toprettyxml no longer adds whitespace around a text
+  node when it is the only child of an element.  Initial patch by Dan
+  Kenigsberg.
+
 - Issue #8793: Prevent IDLE crash when given strings with invalid hex escape
   sequences.
 

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


More information about the Python-checkins mailing list