@@ -1399,7 +1399,7 @@ def endTagHeading(self, token):
13991399
14001400 def 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):
14561456 if formattingElement != self .tree .openElements [- 1 ]:
14571457 self .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.
14611465 afeIndex = self .tree .openElements .index (formattingElement )
14621466 furthestBlock = None
14631467 for element in self .tree .openElements [afeIndex :]:
14641468 if element .nameTuple in specialElements :
14651469 furthestBlock = element
14661470 break
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.
14681480 if furthestBlock is None :
14691481 element = self .tree .openElements .pop ()
14701482 while element != formattingElement :
14711483 element = self .tree .openElements .pop ()
14721484 self .tree .activeFormattingElements .remove (element )
14731485 return
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- # Step 5
1490+ # Step 8:
14811491 # The bookmark is supposed to help us identify where to reinsert
1482- # nodes in step 12 . We have to ensure that we reinsert nodes after
1492+ # nodes in step 15 . We have to ensure that we reinsert nodes after
14831493 # the node before the active formatting element. Note the bookmark
1484- # can move in step 7.4
1494+ # can move in step 9.7
14851495 bookmark = self .tree .activeFormattingElements .index (formattingElement )
14861496
1487- # Step 6
1497+ # Step 9
14881498 lastNode = node = furthestBlock
14891499 innerLoopCounter = 0
14901500
@@ -1497,36 +1507,32 @@ def endTagFormatting(self, token):
14971507 if node not in self .tree .activeFormattingElements :
14981508 self .tree .openElements .remove (node )
14991509 continue
1500- # Step 6.3
1510+ # Step 9.6
15011511 if node == formattingElement :
15021512 break
1503- # Step 6.4
1513+ # Step 9.7
15041514 if 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
15091517 clone = node .cloneNode ()
15101518 # Replace node with clone
15111519 self .tree .activeFormattingElements [
15121520 self .tree .activeFormattingElements .index (node )] = clone
15131521 self .tree .openElements [
15141522 self .tree .openElements .index (node )] = clone
15151523 node = clone
1516-
1517- # Step 6.6
1524+ # Step 9.9
15181525 # Remove lastNode from its parents, if any
15191526 if lastNode .parent :
15201527 lastNode .parent .removeChild (lastNode )
15211528 node .appendChild (lastNode )
1522- # Step 7.7
1529+ # Step 9.10
15231530 lastNode = node
1524- # End of inner loop
15251531
1526- # Step 7
1532+ # Step 10
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
15301536 if lastNode .parent :
15311537 lastNode .parent .removeChild (lastNode )
15321538
@@ -1536,20 +1542,20 @@ def endTagFormatting(self, token):
15361542 else :
15371543 commonAncestor .appendChild (lastNode )
15381544
1539- # Step 8
1545+ # Step 11
15401546 clone = formattingElement .cloneNode ()
15411547
1542- # Step 9
1548+ # Step 12
15431549 furthestBlock .reparentChildren (clone )
15441550
1545- # Step 10
1551+ # Step 13
15461552 furthestBlock .appendChild (clone )
15471553
1548- # Step 11
1554+ # Step 14
15491555 self .tree .activeFormattingElements .remove (formattingElement )
15501556 self .tree .activeFormattingElements .insert (bookmark , clone )
15511557
1552- # Step 12
1558+ # Step 15
15531559 self .tree .openElements .remove (formattingElement )
15541560 self .tree .openElements .insert (
15551561 self .tree .openElements .index (furthestBlock ) + 1 , clone )
0 commit comments