# File lib/html5/html5parser/in_body_phase.rb, line 424
    def endTagFormatting(name)
      # http://www.whatwg.org/specs/web-apps/current-work/#adoptionAgency
      # XXX Better parse_error messages appreciated.
      while true
        # Step 1 paragraph 1
        afeElement = @tree.elementInActiveFormattingElements(name)
        if !afeElement or (@tree.open_elements.include?(afeElement) && !in_scope?(afeElement.name))
          parse_error("adoption-agency-1.1", {"name" => name})
          return
        # Step 1 paragraph 2
        elsif not @tree.open_elements.include?(afeElement)
          parse_error("adoption-agency-1.2", {"name" => name})
          @tree.activeFormattingElements.delete(afeElement)
          return
        end

        # Step 1 paragraph 3
        if afeElement != @tree.open_elements.last
          parse_error("adoption-agency-1.3", {"name" => name})
        end

        # Step 2
        # Start of the adoption agency algorithm proper
        afeIndex = @tree.open_elements.index(afeElement)
        furthestBlock = nil
        @tree.open_elements[afeIndex..-1].each do |element|
          if (SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(element.name)
            furthestBlock = element
            break
          end
        end

        # Step 3
        if furthestBlock.nil?
          element = remove_open_elements_until {|element| element == afeElement }
          @tree.activeFormattingElements.delete(element)
          return
        end
        commonAncestor = @tree.open_elements[afeIndex - 1]

        # Step 5
        furthestBlock.parent.removeChild(furthestBlock) if furthestBlock.parent

        # Step 6
        # The bookmark is supposed to help us identify where to reinsert
        # nodes in step 12. We have to ensure that we reinsert nodes after
        # the node before the active formatting element. Note the bookmark
        # can move in step 7.4
        bookmark = @tree.activeFormattingElements.index(afeElement)

        # Step 7
        lastNode = node = furthestBlock
        while true
          # AT replace this with a function and recursion?
          # Node is element before node in open elements
          node = @tree.open_elements[@tree.open_elements.index(node) - 1]
          until @tree.activeFormattingElements.include?(node)
            tmpNode = node
            node = @tree.open_elements[@tree.open_elements.index(node) - 1]
            @tree.open_elements.delete(tmpNode)
          end
          # Step 7.3
          break if node == afeElement
          # Step 7.4
          if lastNode == furthestBlock
            # XXX should this be index(node) or index(node)+1
            # Anne: I think +1 is ok. Given x = [2,3,4,5]
            # x.index(3) gives 1 and then x[1 +1] gives 4...
            bookmark = @tree.activeFormattingElements.index(node) + 1
          end
          # Step 7.5
          cite = node.parent
          if node.hasContent
            clone = node.cloneNode
            # Replace node with clone
            @tree.activeFormattingElements[@tree.activeFormattingElements.index(node)] = clone
            @tree.open_elements[@tree.open_elements.index(node)] = clone
            node = clone
          end
          # Step 7.6
          # Remove lastNode from its parents, if any
          lastNode.parent.removeChild(lastNode) if lastNode.parent
          node.appendChild(lastNode)
          # Step 7.7
          lastNode = node
          # End of inner loop
        end

        # Step 8
        lastNode.parent.removeChild(lastNode) if lastNode.parent
        commonAncestor.appendChild(lastNode)

        # Step 9
        clone = afeElement.cloneNode

        # Step 10
        furthestBlock.reparentChildren(clone)

        # Step 11
        furthestBlock.appendChild(clone)

        # Step 12
        @tree.activeFormattingElements.delete(afeElement)
        @tree.activeFormattingElements.insert([bookmark,@tree.activeFormattingElements.length].min, clone)

        # Step 13
        @tree.open_elements.delete(afeElement)
        @tree.open_elements.insert(@tree.open_elements.index(furthestBlock) + 1, clone)
      end
    end