123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # HG changeset patch
  2. # User Dan Kenigsberg <danken@redhat.com>
  3. # Date 1317492169 -10800
  4. # Node ID a9a87f0e7c509ec6768379c08a0cf56f43d71b4a
  5. # Parent b0ef6a5a6dccab0089d287bf6b9bcb8132bdbd0d
  6. xml.dom.minidom toprettyxml: omit whitespace for Text nodes
  7. 
  8. http://bugs.python.org/issue4147
  9. 
  10. This patch was very lightly tested, but I think it is nicer than the former one,
  11. as Text.writexml() should better know not to wrap its data with whitespace.
  12. Ever.
  13. 
  14. diff -r b0ef6a5a6dcc -r a9a87f0e7c50 Lib/test/test_minidom.py
  15. --- a/Lib/test/test_minidom.py Fri Sep 30 08:46:25 2011 +0300
  16. +++ b/Lib/test/test_minidom.py Sat Oct 01 21:02:49 2011 +0300
  17. @@ -467,6 +467,13 @@
  18.  dom.unlink()
  19.  self.confirm(domstr == str.replace("\n", "\r\n"))
  20. +
  21. + def testPrettyTextNode(self):
  22. + str = '<A>B</A>'
  23. + dom = parseString(str)
  24. + dom2 = parseString(dom.toprettyxml())
  25. + self.confirm(dom.childNodes[0].childNodes[0].toxml()==
  26. + dom2.childNodes[0].childNodes[0].toxml())
  27. 
  28.  def testProcessingInstruction(self):
  29.  dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
  30.  pi = dom.documentElement.firstChild
  31. diff -r b0ef6a5a6dcc -r a9a87f0e7c50 Lib/xml/dom/minidom.py
  32. --- a/Lib/xml/dom/minidom.py Fri Sep 30 08:46:25 2011 +0300
  33. +++ b/Lib/xml/dom/minidom.py Sat Oct 01 21:02:49 2011 +0300
  34. @@ -836,7 +836,9 @@
  35.  _write_data(writer, attrs[a_name].value)
  36.  writer.write("\"")
  37.  if self.childNodes:
  38. - writer.write(">%s"%(newl))
  39. + writer.write(">")
  40. + if self.childNodes[0].nodeType != Node.TEXT_NODE:
  41. + writer.write(newl)
  42.  for node in self.childNodes:
  43.  node.writexml(writer,indent+addindent,addindent,newl)
  44.  writer.write("%s</%s>%s" % (indent,self.tagName,newl))
  45. @@ -1061,7 +1063,7 @@
  46.  return newText
  47. 
  48.  def writexml(self, writer, indent="", addindent="", newl=""):
  49. - _write_data(writer, "%s%s%s"%(indent, self.data, newl))
  50. + _write_data(writer, self.data)
  51. 
  52.  # DOM Level 3 (WD 9 April 2002)
  53. 
  54.