master
Kanezoh 2021-11-18 14:19:07 +09:00
parent 6865409068
commit ed57c3a696
6 changed files with 203 additions and 199 deletions

View File

@ -8,197 +8,199 @@ require "./form/button"
require "./form/select_list" require "./form/select_list"
require "./utils/element_matcher" require "./utils/element_matcher"
# This class represents the form tag of html. class Mechanize
class Mechanize::Form # This class represents the form tag of html.
include Mechanize::ElementMatcher class Form
include ElementMatcher
getter node : Node | Lexbor::Node getter node : Node | Lexbor::Node
# returns hoge array of `Mechanize::FormContent::Field` in the form. # returns hoge array of `Mechanize::FormContent::Field` in the form.
getter fields : Array(FormContent::Field) getter fields : Array(FormContent::Field)
# returns an array of input tags whose type is checkbox in the form. # returns an array of input tags whose type is checkbox in the form.
getter checkboxes : Array(FormContent::CheckBox) getter checkboxes : Array(FormContent::CheckBox)
# returns an array of input tags whose type is radio in the form. # returns an array of input tags whose type is radio in the form.
getter radiobuttons : Array(FormContent::RadioButton) getter radiobuttons : Array(FormContent::RadioButton)
# returns an array of input tags whose type is select in the form. # returns an array of input tags whose type is select in the form.
getter selectboxes : Array(FormContent::MultiSelectList) getter selectboxes : Array(FormContent::MultiSelectList)
# returns an array of button tags and input tag whose type is button,submit,reset,image. # returns an array of button tags and input tag whose type is button,submit,reset,image.
getter buttons : Array(FormContent::Button) getter buttons : Array(FormContent::Button)
# returns form's 'enctype' attribute. # returns form's 'enctype' attribute.
getter enctype : String getter enctype : String
# returns form's 'method' attribute. # returns form's 'method' attribute.
getter method : String getter method : String
# returns form's 'name' attribute. # returns form's 'name' attribute.
getter name : String getter name : String
# return form's 'action' attribute. # return form's 'action' attribute.
property action : String property action : String
# returns the page which includes the form. # returns the page which includes the form.
getter page : Mechanize::Page? getter page : Page?
def initialize(node : Node | Lexbor::Node, page : Mechanize::Page? = nil) def initialize(node : Node | Lexbor::Node, page : Page? = nil)
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded") @enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
@node = node @node = node
@fields = Array(Mechanize::FormContent::Field).new @fields = Array(FormContent::Field).new
@checkboxes = Array(Mechanize::FormContent::CheckBox).new @checkboxes = Array(FormContent::CheckBox).new
@radiobuttons = Array(Mechanize::FormContent::RadioButton).new @radiobuttons = Array(FormContent::RadioButton).new
@selectboxes = Array(Mechanize::FormContent::MultiSelectList).new @selectboxes = Array(FormContent::MultiSelectList).new
@buttons = Array(Mechanize::FormContent::Button).new @buttons = Array(FormContent::Button).new
@action = node.fetch("action", "") @action = node.fetch("action", "")
@method = node.fetch("method", "GET").upcase @method = node.fetch("method", "GET").upcase
@name = node.fetch("name", "") @name = node.fetch("name", "")
@clicked_buttons = Array(Mechanize::FormContent::Button).new @clicked_buttons = Array(FormContent::Button).new
@page = page @page = page
# @mech = mech # @mech = mech
# @encoding = node['accept-charset'] || (page && page.encoding) || nil # @encoding = node['accept-charset'] || (page && page.encoding) || nil
# @ignore_encoding_error = false # @ignore_encoding_error = false
parse parse
end end
def request_data def request_data
query_params = build_query query_params = build_query
build_query_string(query_params) build_query_string(query_params)
end end
# generate fields_with and field_with methods. # generate fields_with and field_with methods.
# These methods are used for finding nodes that matches conditions. # These methods are used for finding nodes that matches conditions.
# ex.) field_with("email") finds <input name="email"> # ex.) field_with("email") finds <input name="email">
elements_with "field" elements_with "field"
elements_with "radiobutton" elements_with "radiobutton"
elements_with "checkbox", "checkboxes" elements_with "checkbox", "checkboxes"
elements_with "button" elements_with "button"
# Returns all fields of &lt;input type="textarea"&gt; # Returns all fields of &lt;input type="textarea"&gt;
def textareas def textareas
fields.select { |f| f.class == FormContent::Textarea }.map &.as(FormContent::Textarea) fields.select { |f| f.class == FormContent::Textarea }.map &.as(FormContent::Textarea)
end end
private def parse private def parse
@node.css("input").not_nil!.each do |html_node| @node.css("input").not_nil!.each do |html_node|
html_node = html_node.as(Lexbor::Node) html_node = html_node.as(Lexbor::Node)
type = (html_node["type"]? || "text").downcase type = (html_node["type"]? || "text").downcase
case type case type
when "checkbox" when "checkbox"
checkboxes << Mechanize::FormContent::CheckBox.new(html_node, self) checkboxes << FormContent::CheckBox.new(html_node, self)
when "radio" when "radio"
radiobuttons << Mechanize::FormContent::RadioButton.new(html_node, self) radiobuttons << FormContent::RadioButton.new(html_node, self)
when "button" when "button"
buttons << Mechanize::FormContent::Button.new(html_node, @node) buttons << FormContent::Button.new(html_node, @node)
when "submit" when "submit"
buttons << Mechanize::FormContent::SubmitButton.new(html_node, @node) buttons << FormContent::SubmitButton.new(html_node, @node)
when "reset" when "reset"
buttons << Mechanize::FormContent::ResetButton.new(html_node, @node) buttons << FormContent::ResetButton.new(html_node, @node)
when "image" when "image"
buttons << Mechanize::FormContent::ImageButton.new(html_node, @node) buttons << FormContent::ImageButton.new(html_node, @node)
when "text" when "text"
fields << Mechanize::FormContent::Text.new(html_node) fields << FormContent::Text.new(html_node)
when "hidden" when "hidden"
fields << Mechanize::FormContent::Hidden.new(html_node) fields << FormContent::Hidden.new(html_node)
when "textarea" when "textarea"
fields << Mechanize::FormContent::Textarea.new(html_node) fields << FormContent::Textarea.new(html_node)
else else
fields << Mechanize::FormContent::Field.new(html_node) fields << FormContent::Field.new(html_node)
end
end end
end
# Find all textarea tags # Find all textarea tags
@node.css("textarea").each do |node| @node.css("textarea").each do |node|
node = node.as(Lexbor::Node) node = node.as(Lexbor::Node)
next if node["name"].empty? next if node["name"].empty?
@fields << FormContent::Textarea.new(node, node.inner_text) @fields << FormContent::Textarea.new(node, node.inner_text)
end
@node.css("button").each do |node|
node = node.as(Lexbor::Node)
type = node.fetch("type", "submit").downcase
next if type == "reset"
@buttons << FormContent::Button.new(node, @node)
end
# Find all select tags
@node.css("select").each do |node|
node = node.as(Lexbor::Node)
next if node["name"].empty?
if node.has_key?("multiple")
@selectboxes << FormContent::MultiSelectList.new(node)
else
@selectboxes << FormContent::SelectList.new(node)
end end
end
end
private def build_query_string(params : Array(Array(String))) @node.css("button").each do |node|
params.reduce("") do |acc, arr| node = node.as(Lexbor::Node)
hash = {arr[0] => arr[1]} type = node.fetch("type", "submit").downcase
acc + URI::Params.encode(hash) + '&' next if type == "reset"
end.rchop @buttons << FormContent::Button.new(node, @node)
end
private def build_query
query = [] of Array(String)
successful_controls = Array(FormContent::Field | FormContent::CheckBox).new
fields.each do |elm|
successful_controls << elm
end
checkboxes.each do |elm|
if elm.checked
successful_controls << elm
end end
end
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| # Find all select tags
checked = g.select(&.checked) @node.css("select").each do |node|
if checked.uniq.size > 1 node = node.as(Lexbor::Node)
# values = checked.map(&.value).join(', ').inspect next if node["name"].empty?
# name = checked.first.name.inspect if node.has_key?("multiple")
# raise Mechanize::Error, @selectboxes << FormContent::MultiSelectList.new(node)
# "radiobuttons #{values} are checked in the #{name} group, " \ else
# "only one is allowed" @selectboxes << FormContent::SelectList.new(node)
raise Mechanize::Error.new
else
successful_controls << checked.first unless checked.empty?
end
end
@clicked_buttons.each do |b|
successful_controls << b
end
successful_controls.each do |ctrl|
value = ctrl.query_value
next if value[0] == ""
query.push(value)
end
@selectboxes.each do |s|
value = s.query_value
if value
value.each do |v|
query.push(v)
end end
end end
end end
query private def build_query_string(params : Array(Array(String)))
end params.reduce("") do |acc, arr|
hash = {arr[0] => arr[1]}
# This method adds a button to the query. If the form needs to be acc + URI::Params.encode(hash) + '&'
# submitted with multiple buttons, pass each button to this method. end.rchop
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 end
@clicked_buttons << button private def build_query
query = [] of Array(String)
successful_controls = Array(FormContent::Field | FormContent::CheckBox).new
fields.each do |elm|
successful_controls << elm
end
checkboxes.each do |elm|
if elm.checked
successful_controls << elm
end
end
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 Error.new
else
successful_controls << checked.first unless checked.empty?
end
end
@clicked_buttons.each do |b|
successful_controls << b
end
successful_controls.each do |ctrl|
value = ctrl.query_value
next if value[0] == ""
query.push(value)
end
@selectboxes.each do |s|
value = s.query_value
if value
value.each do |v|
query.push(v)
end
end
end
query
end
# This method adds a button to the query. If the form needs to be
# submitted with multiple buttons, pass each button to this method.
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
@clicked_buttons << button
end
end end
end end

View File

@ -7,11 +7,11 @@ class Mechanize
# max page size history can save. default is 100. # max page size history can save. default is 100.
# as same as `agent.max_history`. # as same as `agent.max_history`.
property max_size : Int32 property max_size : Int32
property array : Array(Mechanize::Page) property array : Array(Page)
delegate :size, :empty?, :last, to: array delegate :size, :empty?, :last, to: array
def initialize(max_size = 100, array = Array(Mechanize::Page).new) def initialize(max_size = 100, array = Array(Page).new)
@max_size = max_size @max_size = max_size
@array = array @array = array
end end

View File

@ -9,12 +9,12 @@ class Mechanize
class Agent class Agent
property request_headers : ::HTTP::Headers property request_headers : ::HTTP::Headers
property context : Mechanize? property context : Mechanize?
property history : Mechanize::History property history : History
property user_agent : String property user_agent : String
property request_cookies : ::HTTP::Cookies property request_cookies : ::HTTP::Cookies
def initialize(@context : Mechanize? = nil) def initialize(@context : Mechanize? = nil)
@history = Mechanize::History.new @history = History.new
@request_headers = ::HTTP::Headers.new @request_headers = ::HTTP::Headers.new
@context = context @context = context
@request_cookies = ::HTTP::Cookies.new @request_cookies = ::HTTP::Cookies.new
@ -73,7 +73,7 @@ class Mechanize
# ``` # ```
# agent.current_page # agent.current_page
# ``` # ```
def current_page : Mechanize::Page def current_page : Page
@history.last @history.last
end end
@ -81,7 +81,7 @@ class Mechanize
# ``` # ```
# agent.back # agent.back
# ``` # ```
def back : Mechanize::Page def back : Page
@history.pop @history.pop
end end
@ -116,7 +116,7 @@ class Mechanize
end end
# Sets a Referer header. # Sets a Referer header.
def set_request_referer(referer : Mechanize::Page?) def set_request_referer(referer : Page?)
return unless referer return unless referer
request_headers["Referer"] = referer.uri.to_s request_headers["Referer"] = referer.uri.to_s

View File

@ -7,7 +7,7 @@ class Mechanize
# If you send a request, it returns the instance of `Mechanize::Page`. # If you send a request, it returns the instance of `Mechanize::Page`.
# You can get status code, title, and page body, and search html node using css selector from page instance. # You can get status code, title, and page body, and search html node using css selector from page instance.
class Page < Mechanize::File class Page < Mechanize::File
include Mechanize::ElementMatcher include ElementMatcher
# look at lexbor document.(https://github.com/kostya/lexbor#readme) # look at lexbor document.(https://github.com/kostya/lexbor#readme)
delegate :css, to: parser delegate :css, to: parser
@ -42,7 +42,7 @@ class Mechanize
# ``` # ```
# page.forms # => Array(Mechanize::Form) # page.forms # => Array(Mechanize::Form)
# ``` # ```
def forms : Array(Mechanize::Form) def forms : Array(Form)
forms = css("form").map do |html_form| forms = css("form").map do |html_form|
form = Form.new(html_form, self) form = Form.new(html_form, self)
form.action ||= @uri.to_s form.action ||= @uri.to_s
@ -54,7 +54,7 @@ class Mechanize
# ``` # ```
# page.links # => Array(Mechanize::PageContent::Link) # page.links # => Array(Mechanize::PageContent::Link)
# ``` # ```
def links : Array(Mechanize::PageContent::Link) def links : Array(PageContent::Link)
links = %w{a area}.map do |tag| links = %w{a area}.map do |tag|
css(tag).map do |node| css(tag).map do |node|
PageContent::Link.new(node, @mech, self) PageContent::Link.new(node, @mech, self)

View File

@ -1,22 +1,24 @@
# This class represents link element like <a> and <area>. # This class represents link element like <a> and <area>.
# The instance of this class is clickable. # The instance of this class is clickable.
class Mechanize::PageContent::Link class Mechanize
getter node : Lexbor::Node class PageContent::Link
getter page : Mechanize::Page getter node : Lexbor::Node
getter mech : Mechanize getter page : Page
getter href : String getter mech : Mechanize
getter text : String getter href : String
getter text : String
def initialize(node, mech, page) def initialize(node, mech, page)
@node = node @node = node
@page = page @page = page
@mech = mech @mech = mech
@href = node.fetch("href", "") @href = node.fetch("href", "")
@text = node.inner_text @text = node.inner_text
end end
# click on this link # click on this link
def click def click
@mech.click self @mech.click self
end
end end
end end

View File

@ -65,7 +65,7 @@ class Mechanize
def {{singular.id}}_with(criteria) def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria) f = {{plural.id}}_with(criteria)
# TODO: Write correct error message. # TODO: Write correct error message.
raise Mechanize::ElementNotFoundError.new(:{{singular.id}}, "") if f.empty? raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty?
f.first f.first
end end
end end