123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. 467  dom.unlink() 467  dom.unlink()
  19. 468  self.confirm(domstr == str.replace("\n", "\r\n")) 468  self.confirm(domstr == str.replace("\n", "\r\n"))
  20.   469 
  21.   470  def testPrettyTextNode(self):
  22.   471  str = '<A>B</A>'
  23.   472  dom = parseString(str)
  24.   473  dom2 = parseString(dom.toprettyxml())
  25.   474  self.confirm(dom.childNodes[0].childNodes[0].toxml()==
  26.   475  dom2.childNodes[0].childNodes[0].toxml())
  27. 469  476 
  28. 470  def testProcessingInstruction(self): 477  def testProcessingInstruction(self):
  29. 471  dom = parseString('<e><?mypi \t\n data \t\n ?></e>') 478  dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
  30. 472  pi = dom.documentElement.firstChild 479  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.  836  _write_data(writer, attrs[a_name].value)  836  _write_data(writer, attrs[a_name].value)
  36.  837  writer.write("\"")  837  writer.write("\"")
  37.  838  if self.childNodes:  838  if self.childNodes:
  38.  839  writer.write(">%s"%(newl))  839  writer.write(">")
  39.    840  if self.childNodes[0].nodeType != Node.TEXT_NODE:
  40.    841  writer.write(newl)
  41.  840  for node in self.childNodes:  842  for node in self.childNodes:
  42.  841  node.writexml(writer,indent+addindent,addindent,newl)  843  node.writexml(writer,indent+addindent,addindent,newl)
  43.  842  writer.write("%s</%s>%s" % (indent,self.tagName,newl))  844  writer.write("%s</%s>%s" % (indent,self.tagName,newl))
  44. @@ -1061,7 +1063,7 @@
  45. 1061  return newText 1063  return newText
  46. 1062  1064 
  47. 1063  def writexml(self, writer, indent="", addindent="", newl=""): 1065  def writexml(self, writer, indent="", addindent="", newl=""):
  48. 1064  _write_data(writer, "%s%s%s"%(indent, self.data, newl)) 1066  _write_data(writer, self.data)
  49. 1065  1067 
  50. 1066  # DOM Level 3 (WD 9 April 2002) 1068  # DOM Level 3 (WD 9 April 2002)
  51. 1067  1069