class Capybara::RackTest::Form

def add_input_param(field, params)

def add_input_param(field, params)
  if %w[radio checkbox].include? field['type']
    if field['checked']
      node = Capybara::RackTest::Node.new(driver, field)
      merge_param!(params, field['name'], node.value.to_s)
    end
  elsif %w[submit image].include? field['type']
    # TODO: identify the click button here (in document order, rather
    # than leaving until the end of the params)
  elsif field['type'] == 'file'
    if multipart?
      file = if (value = field['value']).to_s.empty?
        NilUploadedFile.new
      else
        mime_info = MiniMime.lookup_by_filename(value)
        Rack::Test::UploadedFile.new(value, mime_info&.content_type&.to_s)
      end
      merge_param!(params, field['name'], file)
    else
      merge_param!(params, field['name'], File.basename(field['value'].to_s))
    end
  else
    merge_param!(params, field['name'], field['value'].to_s)
  end
end

def add_select_param(field, params)

def add_select_param(field, params)
  if field.has_attribute?('multiple')
    field.xpath('.//option[@selected]').each do |option|
      merge_param!(params, field['name'], (option['value'] || option.text).to_s)
    end
  else
    option = field.xpath('.//option[@selected]').first || field.xpath('.//option').first
    merge_param!(params, field['name'], (option['value'] || option.text).to_s) if option
  end
end

def add_textarea_param(field, params)

def add_textarea_param(field, params)
  merge_param!(params, field['name'], field['_capybara_raw_value'].to_s.gsub(/\n/, "\r\n"))
end

def make_params

def make_params
  if Rack::Utils.respond_to?(:default_query_parser)
    Rack::Utils.default_query_parser.make_params
  else
    ParamsHash.new
  end
end

def merge_param!(params, key, value)

def merge_param!(params, key, value)
  key = key.to_s
  if Rack::Utils.respond_to?(:default_query_parser)
    Rack::Utils.default_query_parser.normalize_params(params, key, value, Rack::Utils.param_depth_limit)
  else
    Rack::Utils.normalize_params(params, key, value)
  end
end

def multipart?

def multipart?
  self[:enctype] == 'multipart/form-data'
end

def params(button)

def params(button)
  params = make_params
  form_element_types = %i[input select textarea]
  form_elements_xpath = XPath.generate do |xp|
    xpath = xp.descendant(*form_element_types).where(!xp.attr(:form))
    xpath += xp.anywhere(*form_element_types).where(xp.attr(:form) == native[:id]) if native[:id]
    xpath.where(!xp.attr(:disabled))
  end.to_s
  native.xpath(form_elements_xpath).map do |field|
    case field.name
    when 'input' then add_input_param(field, params)
    when 'select' then add_select_param(field, params)
    when 'textarea' then add_textarea_param(field, params)
    end
  end
  merge_param!(params, button[:name], button[:value] || '') if button[:name]
  params.to_params_hash
end

def request_method

def request_method
  self[:method] =~ /post/i ? :post : :get
end

def submit(button)

def submit(button)
  action = button&.[]('formaction') || native['action']
  method = button&.[]('formmethod') || request_method
  driver.submit(method, action.to_s, params(button))
end