mechanize.cr/src/mechanize/form.cr

194 lines
5.9 KiB
Crystal
Raw Normal View History

2021-05-05 13:52:06 +02:00
require "./form/field"
2021-06-16 17:11:04 +02:00
require "./form/radio_button"
2021-06-17 08:49:46 +02:00
require "./form/check_box"
require "./form/text"
2021-06-29 14:07:15 +02:00
require "./form/textarea"
2021-06-17 14:19:36 +02:00
require "./form/hidden"
2021-06-18 13:20:00 +02:00
require "./form/button"
2021-07-02 02:59:33 +02:00
require "./form/select_list"
2021-06-25 02:15:43 +02:00
require "./utils/element_matcher"
2021-05-18 15:00:04 +02:00
2021-05-05 13:52:06 +02:00
class MechanizeCr::Form
2021-06-25 02:15:43 +02:00
include MechanzeCr::ElementMatcher
2021-08-14 11:20:15 +02:00
getter node : Node | Lexbor::Node
2021-06-18 13:20:00 +02:00
getter fields : Array(FormContent::Field)
getter checkboxes : Array(FormContent::CheckBox)
2021-06-16 17:11:04 +02:00
getter radiobuttons : Array(FormContent::RadioButton)
2021-07-01 12:02:57 +02:00
getter selectboxes : Array(FormContent::MultiSelectList)
2021-06-18 13:20:00 +02:00
getter buttons : Array(FormContent::Button)
getter enctype : String
getter method : String
getter name : String
2021-08-09 01:34:46 +02:00
getter page : Page?
2021-06-18 13:20:00 +02:00
property action : String
2021-05-05 13:52:06 +02:00
2021-08-14 11:20:15 +02:00
def initialize(node : Node | Lexbor::Node, page : Page? = nil)
2021-06-11 02:18:29 +02:00
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
2021-05-05 13:52:06 +02:00
@node = node
2021-06-16 16:35:14 +02:00
@fields = Array(FormContent::Field).new
@checkboxes = Array(FormContent::CheckBox).new
2021-06-16 17:11:04 +02:00
@radiobuttons = Array(FormContent::RadioButton).new
2021-07-01 12:02:57 +02:00
@selectboxes = Array(FormContent::MultiSelectList).new
2021-06-18 13:20:00 +02:00
@buttons = Array(FormContent::Button).new
2021-06-11 02:18:29 +02:00
@action = node.fetch("action", "")
@method = node.fetch("method", "GET").upcase
2021-06-11 03:52:34 +02:00
@name = node.fetch("name", "")
2021-06-29 10:46:52 +02:00
@clicked_buttons = Array(FormContent::Button).new
@page = page
2021-05-05 13:52:06 +02:00
#@mech = mech
2021-06-11 02:18:29 +02:00
2021-05-05 13:52:06 +02:00
#@encoding = node['accept-charset'] || (page && page.encoding) || nil
#@ignore_encoding_error = false
2021-05-18 15:00:04 +02:00
parse
end
def request_data
query_params = build_query
2021-05-20 11:00:39 +02:00
build_query_string(query_params)
2021-05-18 15:00:04 +02:00
end
2021-06-17 01:30:32 +02:00
# generate fields_with and field_with methods.
# These methods are used for finding nodes that matches conditions.
2021-06-17 01:33:23 +02:00
# ex.) field_with("email") finds <input name="email">
2021-06-17 01:30:32 +02:00
2021-06-25 02:15:43 +02:00
elements_with "field"
elements_with "radiobutton"
2021-06-28 09:51:04 +02:00
elements_with "checkbox", "checkboxes"
2021-07-01 00:30:34 +02:00
elements_with "button"
2021-05-18 15:00:04 +02:00
2021-06-29 14:41:04 +02:00
# Returns all fields of type Textarea
def textareas
fields.select { |f| f.class == FormContent::Textarea }.map &.as(FormContent::Textarea)
end
2021-06-11 03:21:12 +02:00
private def parse
2021-05-27 00:40:25 +02:00
@node.css("input").not_nil!.each do |html_node|
2021-08-14 11:20:15 +02:00
html_node = html_node.as(Lexbor::Node)
2021-06-16 07:58:49 +02:00
type = (html_node["type"] || "text").downcase
case type
when "checkbox"
2021-06-17 08:43:00 +02:00
checkboxes << FormContent::CheckBox.new(html_node, self)
2021-06-17 05:04:45 +02:00
when "radio"
2021-06-17 08:43:00 +02:00
radiobuttons << FormContent::RadioButton.new(html_node, self)
2021-06-18 13:20:00 +02:00
when "button"
buttons << FormContent::Button.new(html_node, @node)
2021-06-18 13:20:00 +02:00
when "submit"
buttons << FormContent::SubmitButton.new(html_node, @node)
2021-06-18 13:20:00 +02:00
when"reset"
buttons << FormContent::ResetButton.new(html_node, @node)
2021-06-30 07:03:35 +02:00
when "image"
buttons << FormContent::ImageButton.new(html_node, @node)
2021-06-17 08:49:46 +02:00
when "text"
fields << FormContent::Text.new(html_node)
2021-06-17 14:19:36 +02:00
when "hidden"
fields << FormContent::Hidden.new(html_node)
2021-06-29 14:07:15 +02:00
when "textarea"
2021-06-29 14:41:04 +02:00
fields << FormContent::Textarea.new(html_node)
2021-06-16 07:58:49 +02:00
else
2021-06-17 08:43:00 +02:00
fields << FormContent::Field.new(html_node)
2021-06-16 07:58:49 +02:00
end
2021-05-18 15:00:04 +02:00
end
2021-06-29 14:07:15 +02:00
# Find all textarea tags
@node.css("textarea").each do |node|
2021-08-14 11:20:15 +02:00
node = node.as(Lexbor::Node)
2021-06-29 14:07:15 +02:00
next if node["name"].empty?
@fields << FormContent::Textarea.new(node, node.inner_text)
end
2021-06-30 07:27:34 +02:00
@node.css("button").each do |node|
2021-08-14 11:20:15 +02:00
node = node.as(Lexbor::Node)
2021-06-30 07:27:34 +02:00
type = node.fetch("type", "submit").downcase
next if type == "reset"
@buttons << FormContent::Button.new(node, @node)
end
2021-07-01 12:02:57 +02:00
# Find all select tags
@node.css("select").each do |node|
2021-08-14 11:20:15 +02:00
node = node.as(Lexbor::Node)
2021-07-01 12:02:57 +02:00
next if node["name"].empty?
if node.has_key?("multiple")
@selectboxes << FormContent::MultiSelectList.new(node)
else
2021-07-02 02:59:33 +02:00
@selectboxes << FormContent::SelectList.new(node)
2021-07-01 12:02:57 +02:00
end
end
2021-05-05 13:52:06 +02:00
end
2021-05-20 11:00:39 +02:00
2021-06-11 03:21:12 +02:00
private def build_query_string(params : Array(Array(String)))
2021-05-20 11:00:39 +02:00
params.reduce("") do |acc, arr|
hash = { arr[0] => arr[1] }
acc + URI::Params.encode(hash) + '&'
end.rchop
end
2021-05-27 00:40:25 +02:00
2021-06-11 03:21:12 +02:00
private def build_query
query = [] of Array(String)
2021-06-16 16:35:14 +02:00
successful_controls = Array(FormContent::Field | FormContent::CheckBox).new
2021-06-11 03:21:12 +02:00
fields.each do |elm|
successful_controls << elm
2021-05-27 00:40:25 +02:00
end
2021-06-11 03:21:12 +02:00
checkboxes.each do |elm|
if elm.checked
successful_controls << elm
2021-05-27 00:40:25 +02:00
end
end
2021-06-17 07:10:53 +02:00
radio_groups = Hash(String, Array(FormContent::RadioButton)).new
radiobuttons.each do |radio|
name = radio.name
radio_groups[name] = Array(FormContent::RadioButton).new unless radio_groups.has_key?(name)
radio_groups[name] << radio
end
radio_groups.each_value do |g|
checked = g.select(&.checked)
if checked.uniq.size > 1
#values = checked.map(&.value).join(', ').inspect
#name = checked.first.name.inspect
#raise Mechanize::Error,
# "radiobuttons #{values} are checked in the #{name} group, " \
# "only one is allowed"
raise MechanizeCr::Error.new
else
successful_controls << checked.first unless checked.empty?
end
end
2021-06-29 10:46:52 +02:00
@clicked_buttons.each do |b|
successful_controls << b
end
2021-06-11 03:21:12 +02:00
successful_controls.each do |ctrl|
value = ctrl.query_value
2021-06-12 16:10:56 +02:00
next if value[0] == ""
2021-06-11 03:21:12 +02:00
query.push(value)
end
2021-07-01 13:31:22 +02:00
@selectboxes.each do |s|
value = s.query_value
if value
value.each do |v|
query.push(v)
end
end
end
2021-06-11 03:21:12 +02:00
query
2021-05-27 00:40:25 +02:00
end
2021-06-29 10:46:52 +02:00
# This method adds a button to the query. If the form needs to be
# submitted with multiple buttons, pass each button to this method.
2021-06-29 12:14:30 +02:00
def add_button_to_query(button)
unless button.form_node == @node
message = ""
"#{button.inspect} does not belong to the same page as " \
"the form #{@name.inspect} in #{@page.try &.uri}"
message = "not a valid button"
raise ArgumentError.new(message)
end
2021-06-29 10:46:52 +02:00
@clicked_buttons << button
end
2021-05-05 13:52:06 +02:00
end