@@ -789,6 +789,167 @@ def testNormalize(self):
789789 "testNormalize -- single empty node removed" )
790790 doc .unlink ()
791791
792+ def testNormalizeCombineAndNextSibling (self ):
793+ doc = parseString ("<doc/>" )
794+ root = doc .documentElement
795+ root .appendChild (doc .createTextNode ("first" ))
796+ root .appendChild (doc .createTextNode ("second" ))
797+ root .appendChild (doc .createElement ("i" ))
798+ self .confirm (len (root .childNodes ) == 3
799+ and root .childNodes .length == 3 ,
800+ "testNormalizeCombineAndNextSibling -- preparation" )
801+ doc .normalize ()
802+ self .confirm (len (root .childNodes ) == 2
803+ and root .childNodes .length == 2
804+ and root .firstChild .data == "firstsecond"
805+ and root .firstChild is not root .lastChild
806+ and root .firstChild .nextSibling is root .lastChild
807+ and root .firstChild .previousSibling is None
808+ and root .lastChild .previousSibling is root .firstChild
809+ and root .lastChild .nextSibling is None
810+ , "testNormalizeCombinedAndNextSibling -- result" )
811+ doc .unlink ()
812+
813+ def testNormalizeDeleteWithPrevSibling (self ):
814+ doc = parseString ("<doc/>" )
815+ root = doc .documentElement
816+ root .appendChild (doc .createTextNode ("first" ))
817+ root .appendChild (doc .createTextNode ("" ))
818+ self .confirm (len (root .childNodes ) == 2
819+ and root .childNodes .length == 2 ,
820+ "testNormalizeDeleteWithPrevSibling -- preparation" )
821+ doc .normalize ()
822+ self .confirm (len (root .childNodes ) == 1
823+ and root .childNodes .length == 1
824+ and root .firstChild .data == "first"
825+ and root .firstChild is root .lastChild
826+ and root .firstChild .nextSibling is None
827+ and root .firstChild .previousSibling is None
828+ , "testNormalizeDeleteWithPrevSibling -- result" )
829+ doc .unlink ()
830+
831+ def testNormalizeDeleteWithNextSibling (self ):
832+ doc = parseString ("<doc/>" )
833+ root = doc .documentElement
834+ root .appendChild (doc .createTextNode ("" ))
835+ root .appendChild (doc .createTextNode ("second" ))
836+ self .confirm (len (root .childNodes ) == 2
837+ and root .childNodes .length == 2 ,
838+ "testNormalizeDeleteWithNextSibling -- preparation" )
839+ doc .normalize ()
840+ self .confirm (len (root .childNodes ) == 1
841+ and root .childNodes .length == 1
842+ and root .firstChild .data == "second"
843+ and root .firstChild is root .lastChild
844+ and root .firstChild .nextSibling is None
845+ and root .firstChild .previousSibling is None
846+ , "testNormalizeDeleteWithNextSibling -- result" )
847+ doc .unlink ()
848+
849+ def testNormalizeDeleteWithTwoNonTextSiblings (self ):
850+ doc = parseString ("<doc/>" )
851+ root = doc .documentElement
852+ root .appendChild (doc .createElement ("i" ))
853+ root .appendChild (doc .createTextNode ("" ))
854+ root .appendChild (doc .createElement ("i" ))
855+ self .confirm (len (root .childNodes ) == 3
856+ and root .childNodes .length == 3 ,
857+ "testNormalizeDeleteWithTwoSiblings -- preparation" )
858+ doc .normalize ()
859+ self .confirm (len (root .childNodes ) == 2
860+ and root .childNodes .length == 2
861+ and root .firstChild is not root .lastChild
862+ and root .firstChild .nextSibling is root .lastChild
863+ and root .firstChild .previousSibling is None
864+ and root .lastChild .previousSibling is root .firstChild
865+ and root .lastChild .nextSibling is None
866+ , "testNormalizeDeleteWithTwoSiblings -- result" )
867+ doc .unlink ()
868+
869+ def testNormalizeDeleteAndCombine (self ):
870+ doc = parseString ("<doc/>" )
871+ root = doc .documentElement
872+ root .appendChild (doc .createTextNode ("" ))
873+ root .appendChild (doc .createTextNode ("second" ))
874+ root .appendChild (doc .createTextNode ("" ))
875+ root .appendChild (doc .createTextNode ("fourth" ))
876+ root .appendChild (doc .createTextNode ("" ))
877+ self .confirm (len (root .childNodes ) == 5
878+ and root .childNodes .length == 5 ,
879+ "testNormalizeDeleteAndCombine -- preparation" )
880+ doc .normalize ()
881+ self .confirm (len (root .childNodes ) == 1
882+ and root .childNodes .length == 1
883+ and root .firstChild is root .lastChild
884+ and root .firstChild .data == "secondfourth"
885+ and root .firstChild .previousSibling is None
886+ and root .firstChild .nextSibling is None
887+ , "testNormalizeDeleteAndCombine -- result" )
888+ doc .unlink ()
889+
890+ def testNormalizeRecursion (self ):
891+ doc = parseString ("<doc>"
892+ "<o>"
893+ "<i/>"
894+ "t"
895+ #
896+ #x
897+ "</o>"
898+ "<o>"
899+ "<o>"
900+ "t2"
901+ #x2
902+ "</o>"
903+ "t3"
904+ #x3
905+ "</o>"
906+ #
907+ "</doc>" )
908+ root = doc .documentElement
909+ root .childNodes [0 ].appendChild (doc .createTextNode ("" ))
910+ root .childNodes [0 ].appendChild (doc .createTextNode ("x" ))
911+ root .childNodes [1 ].childNodes [0 ].appendChild (doc .createTextNode ("x2" ))
912+ root .childNodes [1 ].appendChild (doc .createTextNode ("x3" ))
913+ root .appendChild (doc .createTextNode ("" ))
914+ self .confirm (len (root .childNodes ) == 3
915+ and root .childNodes .length == 3
916+ and len (root .childNodes [0 ].childNodes ) == 4
917+ and root .childNodes [0 ].childNodes .length == 4
918+ and len (root .childNodes [1 ].childNodes ) == 3
919+ and root .childNodes [1 ].childNodes .length == 3
920+ and len (root .childNodes [1 ].childNodes [0 ].childNodes ) == 2
921+ and root .childNodes [1 ].childNodes [0 ].childNodes .length == 2
922+ , "testNormalize2 -- preparation" )
923+ doc .normalize ()
924+ self .confirm (len (root .childNodes ) == 2
925+ and root .childNodes .length == 2
926+ and len (root .childNodes [0 ].childNodes ) == 2
927+ and root .childNodes [0 ].childNodes .length == 2
928+ and len (root .childNodes [1 ].childNodes ) == 2
929+ and root .childNodes [1 ].childNodes .length == 2
930+ and len (root .childNodes [1 ].childNodes [0 ].childNodes ) == 1
931+ and root .childNodes [1 ].childNodes [0 ].childNodes .length == 1
932+ , "testNormalize2 -- childNodes lengths" )
933+ self .confirm (root .childNodes [0 ].childNodes [1 ].data == "tx"
934+ and root .childNodes [1 ].childNodes [0 ].childNodes [0 ].data == "t2x2"
935+ and root .childNodes [1 ].childNodes [1 ].data == "t3x3"
936+ , "testNormalize2 -- joined text fields" )
937+ self .confirm (root .childNodes [0 ].childNodes [1 ].nextSibling is None
938+ and root .childNodes [0 ].childNodes [1 ].previousSibling
939+ is root .childNodes [0 ].childNodes [0 ]
940+ and root .childNodes [0 ].childNodes [0 ].previousSibling is None
941+ and root .childNodes [0 ].childNodes [0 ].nextSibling
942+ is root .childNodes [0 ].childNodes [1 ]
943+ and root .childNodes [1 ].childNodes [1 ].nextSibling is None
944+ and root .childNodes [1 ].childNodes [1 ].previousSibling
945+ is root .childNodes [1 ].childNodes [0 ]
946+ and root .childNodes [1 ].childNodes [0 ].previousSibling is None
947+ and root .childNodes [1 ].childNodes [0 ].nextSibling
948+ is root .childNodes [1 ].childNodes [1 ]
949+ , "testNormalize2 -- sibling pointers" )
950+ doc .unlink ()
951+
952+
792953 def testBug1433694 (self ):
793954 doc = parseString ("<o><i/>t</o>" )
794955 node = doc .documentElement
0 commit comments