123456789_123456789_123456789_123456789_123456789_

Class: Capybara::RackTest::Form

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Capybara::RackTest::Node
Defined in: lib/capybara/rack_test/form.rb

Constant Summary

Node::WhitespaceNormalizer - Included

BREAKING_SPACES, EMPTY_LINES, LEADING_SPACES, LEFT_TO_RIGHT_MARK, LINE_SEPERATOR, NON_BREAKING_SPACE, PARAGRAPH_SEPERATOR, REMOVED_CHARACTERS, RIGHT_TO_LEFT_MARK, SQUEEZED_SPACES, TRAILING_SPACES, ZERO_WIDTH_SPACE

Node - Inherited

BLOCK_ELEMENTS, DISABLED_BY_FIELDSET_XPATH, OPTION_OWNER_XPATH

Class Method Summary

Driver::Node - Inherited

Instance Attribute Summary

Instance Method Summary

Constructor Details

This class inherits a constructor from Capybara::Driver::Node

Instance Attribute Details

#multipart?Boolean (readonly)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 60

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

Instance Method Details

#add_input_param(field, params)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 93

def add_input_param(field, params)
  name, value = field['name'].to_s, field['value'].to_s
  return if name.empty?

  value = case field['type']
  when 'radio', 'checkbox'
    return unless field['checked']

    Capybara::RackTest::Node.new(driver, field).value.to_s
  when 'file'
    return if value.empty? && params.keys.include?(name) && Rack::Test::VERSION.to_f >= 2.0 # rubocop:disable Performance/InefficientHashSearch

    if multipart?
      file_to_upload(value)
    else
      File.basename(value)
    end
  else
    value
  end
  # merge_param!(params, name, value)
  params[name] = value
end

#add_select_param(field, params)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 126

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

#add_textarea_param(field, params)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 141

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

#file_to_upload(filename)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 117

def file_to_upload(filename)
  if filename.empty?
    NilUploadedFile.new
  else
    mime_info = MiniMime.lookup_by_filename(filename)
    Rack::Test::UploadedFile.new(filename, mime_info&.content_type&.to_s)
  end
end

#make_params

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 85

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

#merge_param!(params, key, value)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 76

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

#params(button)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 23

def params(button)
  form_element_types = %i[input select textarea button]
  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

  form_elements = native.xpath(form_elements_xpath).reject { |el| submitter?(el) && (el != button.native) }

  form_params = form_elements.each_with_object({}.compare_by_identity) do |field, params|
    case field.name
    when 'input', 'button' then add_input_param(field, params)
    when 'select' then add_select_param(field, params)
    when 'textarea' then add_textarea_param(field, params)
    end
  end

  form_params.each_with_object(make_params) do |(name, value), params|
    merge_param!(params, name, value)
  end.to_params_hash

  # form_elements.each_with_object(make_params) do |field, params|
  #   case field.name
  #   when 'input', 'button' then add_input_param(field, params)
  #   when 'select' then add_select_param(field, params)
  #   when 'textarea' then add_textarea_param(field, params)
  #   end
  # end.to_params_hash
end

#request_method

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 72

def request_method
  /post/i.match?(self[:method] || '') ? :post : :get
end

#submit(button)

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 54

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

#submitter?(el) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/rack_test/form.rb', line 146

def submitter?(el)
  (%w[submit image].include? el['type']) || (el.name == 'button')
end