#4147: minidom's toprettyxml no longer adds whitespace to text nodes.

Patch by Dan Kenigsberg.
This commit is contained in:
R David Murray 2011-10-01 16:49:25 -04:00
parent f06eb46918
commit c8faf9bb0c
2 changed files with 11 additions and 2 deletions

View File

@ -439,6 +439,13 @@ class MinidomTest(unittest.TestCase):
dom.unlink() dom.unlink()
self.confirm(domstr == str.replace("\n", "\r\n")) 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): def testProcessingInstruction(self):
dom = parseString('<e><?mypi \t\n data \t\n ?></e>') dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
pi = dom.documentElement.firstChild pi = dom.documentElement.firstChild

View File

@ -806,7 +806,9 @@ class Element(Node):
_write_data(writer, attrs[a_name].value) _write_data(writer, attrs[a_name].value)
writer.write("\"") writer.write("\"")
if self.childNodes: 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: for node in self.childNodes:
node.writexml(writer,indent+addindent,addindent,newl) node.writexml(writer,indent+addindent,addindent,newl)
writer.write("%s</%s>%s" % (indent,self.tagName,newl)) writer.write("%s</%s>%s" % (indent,self.tagName,newl))
@ -1031,7 +1033,7 @@ class Text(CharacterData):
return newText return newText
def writexml(self, writer, indent="", addindent="", newl=""): 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) # DOM Level 3 (WD 9 April 2002)