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-17 14:19:36 +02:00
|
|
|
require "./form/hidden"
|
2021-06-18 13:20:00 +02:00
|
|
|
require "./form/button"
|
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-06-27 13:13:43 +02:00
|
|
|
getter node : Node | Myhtml::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-06-18 13:20:00 +02:00
|
|
|
getter buttons : Array(FormContent::Button)
|
|
|
|
getter enctype : String
|
|
|
|
getter method : String
|
|
|
|
getter name : String
|
|
|
|
property action : String
|
2021-05-05 13:52:06 +02:00
|
|
|
|
2021-05-26 06:49:37 +02:00
|
|
|
def initialize(node : Node | Myhtml::Node)
|
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-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-05-05 13:52:06 +02:00
|
|
|
#@clicked_buttons = []
|
|
|
|
#@page = page
|
|
|
|
#@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-05-18 15:00:04 +02:00
|
|
|
|
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|
|
|
|
|
html_node = html_node.as(Myhtml::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"
|
2021-06-18 23:16:36 +02:00
|
|
|
buttons << FormContent::Button.new(html_node)
|
2021-06-18 13:20:00 +02:00
|
|
|
when "submit"
|
2021-06-18 23:16:36 +02:00
|
|
|
buttons << FormContent::SubmitButton.new(html_node)
|
2021-06-18 13:20:00 +02:00
|
|
|
when"reset"
|
2021-06-18 23:16:36 +02:00
|
|
|
buttons << FormContent::ResetButton.new(html_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-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-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-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
|
|
|
|
query
|
2021-05-27 00:40:25 +02:00
|
|
|
end
|
2021-05-05 13:52:06 +02:00
|
|
|
end
|