# File lib/action_controller/assertions/selector_assertions.rb, line 408
      def assert_select_rjs(*args, &block)
        rjs_type = args.first.is_a?(Symbol) ? args.shift : nil
        id       = args.first.is_a?(String) ? args.shift : nil

        # If the first argument is a symbol, it's the type of RJS statement we're looking
        # for (update, replace, insertion, etc). Otherwise, we're looking for just about
        # any RJS statement.
        if rjs_type
          if rjs_type == :insert
            position  = args.shift
            id = args.shift
            insertion = "insert_#{position}".to_sym
            raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion]
            statement = "(#{RJS_STATEMENTS[insertion]})"
          else
            raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type]
            statement = "(#{RJS_STATEMENTS[rjs_type]})"
          end
        else
          statement = "#{RJS_STATEMENTS[:any]}"
        end

        # Next argument we're looking for is the element identifier. If missing, we pick
        # any element, otherwise we replace it in the statement.
        pattern = Regexp.new(
          id ? statement.gsub(RJS_ANY_ID, "\"#{id}\"") : statement
        )

        # Duplicate the body since the next step involves destroying it.
        matches = nil
        case rjs_type
          when :remove, :show, :hide, :toggle
            matches = @response.body.match(pattern)
          else
            @response.body.gsub(pattern) do |match|
              html = unescape_rjs(match)
              matches ||= []
              matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? }
              ""
            end
        end

        if matches
          assert_block("") { true } # to count the assertion
          if block_given? && !([:remove, :show, :hide, :toggle].include? rjs_type)
            begin
              in_scope, @selected = @selected, matches
              yield matches
            ensure
              @selected = in_scope
            end
          end
          matches
        else
          # RJS statement not found.
          case rjs_type
            when :remove, :show, :hide, :toggle
              flunk_message = "No RJS statement that #{rjs_type.to_s}s '#{id}' was rendered."
            else
              flunk_message = "No RJS statement that replaces or inserts HTML content."
          end
          flunk args.shift || flunk_message
        end
      end