@@ -977,11 +977,35 @@ def __init__(self, parser, tree):
977
977
])
978
978
self .endTagHandler .default = self .endTagOther
979
979
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
+
980
993
# helper
981
994
def addFormattingElement (self , token ):
982
995
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 )
985
1009
986
1010
# the real deal
987
1011
def processEOF (self ):
@@ -2422,6 +2446,7 @@ def processEndTag(self, token):
2422
2446
2423
2447
while True :
2424
2448
if node .name .translate (asciiUpper2Lower ) == token ["name" ]:
2449
+ #XXX this isn't in the spec but it seems necessary
2425
2450
if self .parser .phase == self .parser .phases ["inTableText" ]:
2426
2451
self .parser .phase .flushCharacters ()
2427
2452
self .parser .phase = self .parser .phase .originalPhase
0 commit comments