@@ -1399,7 +1399,7 @@ def endTagHeading(self, token):
13991399
14001400def endTagFormatting (self ,token ):
14011401"""The much-feared adoption agency algorithm"""
1402- # http://www .whatwg.org/specs/web-apps/current-work/ #adoptionAgency
1402+ # http://svn .whatwg.org/webapps/complete.html #adoptionAgency revision 7867
14031403# XXX Better parseError messages appreciated.
14041404
14051405# Step 1
@@ -1456,35 +1456,45 @@ def endTagFormatting(self, token):
14561456if formattingElement != self .tree .openElements [- 1 ]:
14571457self .parser .parseError ("adoption-agency-1.3" , {"name" :token ["name" ]})
14581458
1459- # Step 2
1460- # Start of the adoption agency algorithm proper
1459+ # Step 5:
1460+
1461+ # Let the furthest block be the topmost node in the
1462+ # stack of open elements that is lower in the stack
1463+ # than the formatting element, and is an element in
1464+ # the special category. There might not be one.
14611465afeIndex = self .tree .openElements .index (formattingElement )
14621466furthestBlock = None
14631467for element in self .tree .openElements [afeIndex :]:
14641468if element .nameTuple in specialElements :
14651469furthestBlock = element
14661470break
1467- # Step 3
1471+
1472+ # Step 6:
1473+
1474+ # If there is no furthest block, then the UA must
1475+ # first pop all the nodes from the bottom of the stack
1476+ # of open elements, from the current node up to and
1477+ # including the formatting element, then remove the
1478+ # formatting element from the list of active
1479+ # formatting elements, and finally abort these steps.
14681480if furthestBlock is None :
14691481element = self .tree .openElements .pop ()
14701482while element != formattingElement :
14711483element = self .tree .openElements .pop ()
14721484self .tree .activeFormattingElements .remove (element )
14731485return
1474- commonAncestor = self .tree .openElements [afeIndex - 1 ]
14751486
1476- # Step 5
1477- # if furthestBlock.parent:
1478- # furthestBlock.parent.removeChild(furthestBlock)
1487+ # Step 7
1488+ commonAncestor = self .tree .openElements [afeIndex - 1 ]
14791489
1480- # Step5
1490+ # Step8:
14811491# The bookmark is supposed to help us identify where to reinsert
1482- # nodes in step12 . We have to ensure that we reinsert nodes after
1492+ # nodes in step15 . We have to ensure that we reinsert nodes after
14831493# the node before the active formatting element. Note the bookmark
1484- # can move in step7.4
1494+ # can move in step9.7
14851495bookmark = self .tree .activeFormattingElements .index (formattingElement )
14861496
1487- # Step6
1497+ # Step9
14881498lastNode = node = furthestBlock
14891499innerLoopCounter = 0
14901500
@@ -1497,36 +1507,32 @@ def endTagFormatting(self, token):
14971507if node not in self .tree .activeFormattingElements :
14981508self .tree .openElements .remove (node )
14991509continue
1500- # Step6.3
1510+ # Step9.6
15011511if node == formattingElement :
15021512break
1503- # Step6.4
1513+ # Step9.7
15041514if lastNode == furthestBlock :
1505- bookmark = (self .tree .activeFormattingElements .index (node )
1506- + 1 )
1507- # Step 6.5
1508- # cite = node.parent
1515+ bookmark = self .tree .activeFormattingElements .index (node )+ 1
1516+ # Step 9.8
15091517clone = node .cloneNode ()
15101518# Replace node with clone
15111519self .tree .activeFormattingElements [
15121520self .tree .activeFormattingElements .index (node )]= clone
15131521self .tree .openElements [
15141522self .tree .openElements .index (node )]= clone
15151523node = clone
1516-
1517- # Step 6.6
1524+ # Step 9.9
15181525# Remove lastNode from its parents, if any
15191526if lastNode .parent :
15201527lastNode .parent .removeChild (lastNode )
15211528node .appendChild (lastNode )
1522- # Step7.7
1529+ # Step9.10
15231530lastNode = node
1524- # End of inner loop
15251531
1526- # Step7
1532+ # Step10
15271533# Foster parent lastNode if commonAncestor is a
1528- # table, tbody, tfoot, thead, or tr we need to foster parent the
1529- # lastNode
1534+ # table, tbody, tfoot, thead, or tr we need to foster
1535+ #parent the lastNode
15301536if lastNode .parent :
15311537lastNode .parent .removeChild (lastNode )
15321538
@@ -1536,20 +1542,20 @@ def endTagFormatting(self, token):
15361542else :
15371543commonAncestor .appendChild (lastNode )
15381544
1539- # Step8
1545+ # Step11
15401546clone = formattingElement .cloneNode ()
15411547
1542- # Step9
1548+ # Step12
15431549furthestBlock .reparentChildren (clone )
15441550
1545- # Step10
1551+ # Step13
15461552furthestBlock .appendChild (clone )
15471553
1548- # Step11
1554+ # Step14
15491555self .tree .activeFormattingElements .remove (formattingElement )
15501556self .tree .activeFormattingElements .insert (bookmark ,clone )
15511557
1552- # Step12
1558+ # Step15
15531559self .tree .openElements .remove (formattingElement )
15541560self .tree .openElements .insert (
15551561self .tree .openElements .index (furthestBlock )+ 1 ,clone )