# File lib/html5/html5parser.rb, line 207
    def reset_insertion_mode
      # The name of this method is mostly historical. (It's also used in the
      # specification.)
      last = false

      @tree.open_elements.reverse.each do |node|
        node_name = node.name

        if node == @tree.open_elements.first
          last = true
          unless ['td', 'th'].include?(node_name)
            # XXX
            # assert @inner_html
            node_name = @inner_html
          end
        end

        # Check for conditions that should only happen in the inner_html
        # case
        if ['select', 'colgroup', 'head', 'frameset'].include?(node_name)
          # XXX
          # assert @inner_html
        end

        if @@new_modes.has_key?(node_name)
          @phase = @phases[@@new_modes[node_name]]
        elsif node_name == 'html'
          @phase = @phases[@tree.head_pointer.nil?? :beforeHead : :afterHead]
        elsif last
          @phase = @phases[:inBody]
        else
          next
        end

        break
      end
    end