@@ -1404,28 +1404,60 @@ def endTagFormatting(self, token):
14041404# XXX Better parseError messages appreciated.
14051405name = token ["name" ]
14061406
1407+ # Step 1
14071408outerLoopCounter = 0
1409+
1410+ # Step 2
14081411while outerLoopCounter < 8 :
1412+
1413+ # Step 3
14091414outerLoopCounter += 1
14101415
1411- # Step 1 paragraph 1
1416+ # Step 4:
1417+
1418+ # Let the formatting element be the last element in
1419+ # the list of active formatting elements that:
1420+ # - is between the end of the list and the last scope
1421+ # marker in the list, if any, or the start of the list
1422+ # otherwise, and
1423+ # - has the same tag name as the token.
14121424formattingElement = self .tree .elementInActiveFormattingElements (
14131425token ["name" ])
14141426if (not formattingElement or
14151427 (formattingElement in self .tree .openElements and
14161428not self .tree .elementInScope (formattingElement .name ))):
1417- self .parser .parseError ("adoption-agency-1.1" , {"name" :token ["name" ]})
1429+ # If there is no such node, then abort these steps
1430+ # and instead act as described in the "any other
1431+ # end tag" entry below.
1432+ self .endTagOther (token )
14181433return
14191434
1420- # Step 1 paragraph 2
1435+ # Otherwise, if there is such a node, but that node is
1436+ # not in the stack of open elements, then this is a
1437+ # parse error; remove the element from the list, and
1438+ # abort these steps.
14211439elif formattingElement not in self .tree .openElements :
14221440self .parser .parseError ("adoption-agency-1.2" , {"name" :token ["name" ]})
14231441self .tree .activeFormattingElements .remove (formattingElement )
14241442return
1443+
1444+
1445+ # Otherwise, if there is such a node, and that node is
1446+ # also in the stack of open elements, but the element
1447+ # is not in scope, then this is a parse error; ignore
1448+ # the token, and abort these steps.
1449+ elif not self .tree .elementInScope (formattingElement .name ):
1450+ self .parser .parseError ("adoption-agency-4.4" , {"name" :token ["name" ]})
1451+ return
14251452
1426- # Step 1 paragraph 3
1427- if formattingElement != self .tree .openElements [- 1 ]:
1428- self .parser .parseError ("adoption-agency-1.3" , {"name" :token ["name" ]})
1453+ # Otherwise, there is a formatting element and that
1454+ # element is in the stack and is in scope. If the
1455+ # element is not the current node, this is a parse
1456+ # error. In any case, proceed with the algorithm as
1457+ # written in the following steps.
1458+ else :
1459+ if formattingElement != self .tree .openElements [- 1 ]:
1460+ self .parser .parseError ("adoption-agency-1.3" , {"name" :token ["name" ]})
14291461
14301462# Step 2
14311463# Start of the adoption agency algorithm proper