@@ -467,12 +467,39 @@ def testAltNewline(self):
467467 dom .unlink ()
468468 self .confirm (domstr == str .replace ("\n " , "\r \n " ))
469469
470+ def test_toprettyxml_with_text_nodes (self ):
471+ # see issue #4147, text nodes are not indented
472+ decl = '<?xml version="1.0" ?>\n '
473+ self .assertEqual (parseString ('<B>A</B>' ).toprettyxml (),
474+ decl + '<B>A</B>\n ' )
475+ self .assertEqual (parseString ('<C>A<B>A</B></C>' ).toprettyxml (),
476+ decl + '<C>\n \t A\n \t <B>A</B>\n </C>\n ' )
477+ self .assertEqual (parseString ('<C><B>A</B>A</C>' ).toprettyxml (),
478+ decl + '<C>\n \t <B>A</B>\n \t A\n </C>\n ' )
479+ self .assertEqual (parseString ('<C><B>A</B><B>A</B></C>' ).toprettyxml (),
480+ decl + '<C>\n \t <B>A</B>\n \t <B>A</B>\n </C>\n ' )
481+ self .assertEqual (parseString ('<C><B>A</B>A<B>A</B></C>' ).toprettyxml (),
482+ decl + '<C>\n \t <B>A</B>\n \t A\n \t <B>A</B>\n </C>\n ' )
483+
484+ def test_toprettyxml_with_adjacent_text_nodes (self ):
485+ # see issue #4147, adjacent text nodes are indented normally
486+ dom = Document ()
487+ elem = dom .createElement ('elem' )
488+ elem .appendChild (dom .createTextNode ('TEXT' ))
489+ elem .appendChild (dom .createTextNode ('TEXT' ))
490+ dom .appendChild (elem )
491+ decl = '<?xml version="1.0" ?>\n '
492+ self .assertEqual (dom .toprettyxml (),
493+ decl + '<elem>\n \t TEXT\n \t TEXT\n </elem>\n ' )
494+
470495 def test_toprettyxml_preserves_content_of_text_node (self ):
471- str = '<A>B</A>'
472- dom = parseString (str )
473- dom2 = parseString (dom .toprettyxml ())
474- self .assertEqual (dom .childNodes [0 ].childNodes [0 ].toxml (),
475- dom2 .childNodes [0 ].childNodes [0 ].toxml ())
496+ # see issue #4147
497+ for str in ('<B>A</B>' , '<A><B>C</B></A>' ):
498+ dom = parseString (str )
499+ dom2 = parseString (dom .toprettyxml ())
500+ self .assertEqual (
501+ dom .getElementsByTagName ('B' )[0 ].childNodes [0 ].toxml (),
502+ dom2 .getElementsByTagName ('B' )[0 ].childNodes [0 ].toxml ())
476503
477504 def testProcessingInstruction (self ):
478505 dom = parseString ('<e><?mypi \t \n data \t \n ?></e>' )
0 commit comments