@@ -977,11 +977,35 @@ def __init__(self, parser, tree):
977977 ])
978978 self .endTagHandler .default = self .endTagOther
979979
980+ def isMatchingFormattingElement (self , node1 , node2 ):
981+ if node1 .name != node2 .name or node1 .namespace != node2 .namespace :
982+ return False
983+ elif len (node1 .attributes ) != len (node2 .attributes ):
984+ return False
985+ else :
986+ attributes1 = sorted (node1 .attributes .items ())
987+ attributes2 = sorted (node2 .attributes .items ())
988+ for attr1 , attr2 in zip (attributes1 , attributes2 ):
989+ if attr1 != attr2 :
990+ return False
991+ return True
992+
980993 # helper
981994 def addFormattingElement (self , token ):
982995 self .tree .insertElement (token )
983- self .tree .activeFormattingElements .append (
984- self .tree .openElements [- 1 ])
996+ element = self .tree .openElements [- 1 ]
997+
998+ matchingElements = []
999+ for node in self .tree .activeFormattingElements [::- 1 ]:
1000+ if node is Marker :
1001+ break
1002+ elif self .isMatchingFormattingElement (node , element ):
1003+ matchingElements .append (node )
1004+
1005+ assert len (matchingElements ) <= 3
1006+ if len (matchingElements ) == 3 :
1007+ self .tree .activeFormattingElements .remove (matchingElements [- 1 ])
1008+ self .tree .activeFormattingElements .append (element )
9851009
9861010 # the real deal
9871011 def processEOF (self ):
@@ -2422,6 +2446,7 @@ def processEndTag(self, token):
24222446
24232447 while True :
24242448 if node .name .translate (asciiUpper2Lower ) == token ["name" ]:
2449+ #XXX this isn't in the spec but it seems necessary
24252450 if self .parser .phase == self .parser .phases ["inTableText" ]:
24262451 self .parser .phase .flushCharacters ()
24272452 self .parser .phase = self .parser .phase .originalPhase
0 commit comments