diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index 2621f72222d..392e40e94fd 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -467,12 +467,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 = '\n'
+ self.assertEqual(parseString('A').toprettyxml(),
+ decl + 'A\n')
+ self.assertEqual(parseString('AA').toprettyxml(),
+ decl + '\n\tA\n\tA\n\n')
+ self.assertEqual(parseString('AA').toprettyxml(),
+ decl + '\n\tA\n\tA\n\n')
+ self.assertEqual(parseString('AA').toprettyxml(),
+ decl + '\n\tA\n\tA\n\n')
+ self.assertEqual(parseString('AAA').toprettyxml(),
+ decl + '\n\tA\n\tA\n\tA\n\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 = '\n'
+ self.assertEqual(dom.toprettyxml(),
+ decl + '\n\tTEXT\n\tTEXT\n\n')
+
def test_toprettyxml_preserves_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())
+ # see issue #4147
+ for str in ('A', 'C'):
+ 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('')
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 386494d288e..f23ad053333 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -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)
diff --git a/Misc/NEWS b/Misc/NEWS
index af8eaf463e0..4bf4666721a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -377,6 +377,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.
+
- Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Unicode filenames instead of bytes filenames to not depend on the ANSI code
page anymore and to support any filename.