@@ -446,12 +446,39 @@ def testAltNewline(self):
446446 dom .unlink ()
447447 self .confirm (domstr == str .replace ("\n " , "\r \n " ))
448448
449+ def test_toprettyxml_with_text_nodes (self ):
450+ # see issue #4147, text nodes are not indented
451+ decl = '<?xml version="1.0" ?>\n '
452+ self .assertEqual (parseString ('<B>A</B>' ).toprettyxml (),
453+ decl + '<B>A</B>\n ' )
454+ self .assertEqual (parseString ('<C>A<B>A</B></C>' ).toprettyxml (),
455+ decl + '<C>\n \t A\n \t <B>A</B>\n </C>\n ' )
456+ self .assertEqual (parseString ('<C><B>A</B>A</C>' ).toprettyxml (),
457+ decl + '<C>\n \t <B>A</B>\n \t A\n </C>\n ' )
458+ self .assertEqual (parseString ('<C><B>A</B><B>A</B></C>' ).toprettyxml (),
459+ decl + '<C>\n \t <B>A</B>\n \t <B>A</B>\n </C>\n ' )
460+ self .assertEqual (parseString ('<C><B>A</B>A<B>A</B></C>' ).toprettyxml (),
461+ decl + '<C>\n \t <B>A</B>\n \t A\n \t <B>A</B>\n </C>\n ' )
462+
463+ def test_toprettyxml_with_adjacent_text_nodes (self ):
464+ # see issue #4147, adjacent text nodes are indented normally
465+ dom = Document ()
466+ elem = dom .createElement ('elem' )
467+ elem .appendChild (dom .createTextNode ('TEXT' ))
468+ elem .appendChild (dom .createTextNode ('TEXT' ))
469+ dom .appendChild (elem )
470+ decl = '<?xml version="1.0" ?>\n '
471+ self .assertEqual (dom .toprettyxml (),
472+ decl + '<elem>\n \t TEXT\n \t TEXT\n </elem>\n ' )
473+
449474 def test_toprettyxml_preserves_content_of_text_node (self ):
450- str = '<A>B</A>'
451- dom = parseString (str )
452- dom2 = parseString (dom .toprettyxml ())
453- self .assertEqual (dom .childNodes [0 ].childNodes [0 ].toxml (),
454- dom2 .childNodes [0 ].childNodes [0 ].toxml ())
475+ # see issue #4147
476+ for str in ('<B>A</B>' , '<A><B>C</B></A>' ):
477+ dom = parseString (str )
478+ dom2 = parseString (dom .toprettyxml ())
479+ self .assertEqual (
480+ dom .getElementsByTagName ('B' )[0 ].childNodes [0 ].toxml (),
481+ dom2 .getElementsByTagName ('B' )[0 ].childNodes [0 ].toxml ())
455482
456483 def testProcessingInstruction (self ):
457484 dom = parseString ('<e><?mypi \t \n data \t \n ?></e>' )
0 commit comments