def endTagFormatting(name)
while true
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
elsif not @tree.open_elements.include?(afeElement)
parse_error("adoption-agency-1.2", {"name" => name})
@tree.activeFormattingElements.delete(afeElement)
return
end
if afeElement != @tree.open_elements.last
parse_error("adoption-agency-1.3", {"name" => name})
end
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
if furthestBlock.nil?
element = remove_open_elements_until {|element| element == afeElement }
@tree.activeFormattingElements.delete(element)
return
end
commonAncestor = @tree.open_elements[afeIndex - 1]
furthestBlock.parent.removeChild(furthestBlock) if furthestBlock.parent
bookmark = @tree.activeFormattingElements.index(afeElement)
lastNode = node = furthestBlock
while true
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
break if node == afeElement
if lastNode == furthestBlock
bookmark = @tree.activeFormattingElements.index(node) + 1
end
cite = node.parent
if node.hasContent
clone = node.cloneNode
@tree.activeFormattingElements[@tree.activeFormattingElements.index(node)] = clone
@tree.open_elements[@tree.open_elements.index(node)] = clone
node = clone
end
lastNode.parent.removeChild(lastNode) if lastNode.parent
node.appendChild(lastNode)
lastNode = node
end
lastNode.parent.removeChild(lastNode) if lastNode.parent
commonAncestor.appendChild(lastNode)
clone = afeElement.cloneNode
furthestBlock.reparentChildren(clone)
furthestBlock.appendChild(clone)
@tree.activeFormattingElements.delete(afeElement)
@tree.activeFormattingElements.insert([bookmark,@tree.activeFormattingElements.length].min, clone)
@tree.open_elements.delete(afeElement)
@tree.open_elements.insert(@tree.open_elements.index(furthestBlock) + 1, clone)
end
end