]> granicus.if.org Git - python/commitdiff
#4147: minidom's toprettyxml no longer adds whitespace around a text node when it...
authorEzio Melotti <ezio.melotti@gmail.com>
Fri, 18 Nov 2011 15:34:26 +0000 (17:34 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Fri, 18 Nov 2011 15:34:26 +0000 (17:34 +0200)
Lib/test/test_minidom.py
Lib/xml/dom/minidom.py
Misc/NEWS

index 126bdb15d251e57d494371ee4a511b14781b72f9..f3fa1b8c5815c65985c377a1d366e9957d75ed24 100644 (file)
@@ -446,12 +446,39 @@ class MinidomTest(unittest.TestCase):
         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('elem')
+        elem.appendChild(dom.createTextNode('TEXT'))
+        elem.appendChild(dom.createTextNode('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>')
index 386494d288ee900ca4e020f39d20a95e7779575f..f23ad053333b53005612cbf88fbefc1b5a0e2954 100644 (file)
@@ -837,11 +837,15 @@ class Element(Node):
             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))
 
@@ -1063,7 +1067,7 @@ class Text(CharacterData):
         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)
 
index 4fb9ff6305a135d9074696e768545442d14ae73e..8a82b099ff85d09e1b0344a0da5128e071c8ed85 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -76,6 +76,10 @@ Core and Builtins
 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.
+
 - Issues #1745761, #755670, #13357, #12629, #1200313: HTMLParser now correctly
   handles non-valid attributes, including adjacent and unquoted attributes.