improve add_button_to_query error message

master
Kanezoh 2021-07-01 07:54:27 +09:00
parent fb94479166
commit 5a98037965
3 changed files with 13 additions and 6 deletions

View File

@ -20,7 +20,7 @@ class MechanizeCr::Form
getter name : String
property action : String
def initialize(node : Node | Myhtml::Node)
def initialize(node : Node | Myhtml::Node, page : Page? = nil)
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
@node = node
@fields = Array(FormContent::Field).new
@ -31,7 +31,7 @@ class MechanizeCr::Form
@method = node.fetch("method", "GET").upcase
@name = node.fetch("name", "")
@clicked_buttons = Array(FormContent::Button).new
#@page = page
@page = page
#@mech = mech
#@encoding = node['accept-charset'] || (page && page.encoding) || nil
@ -156,9 +156,9 @@ class MechanizeCr::Form
# 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.uri}"
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

View File

@ -26,4 +26,11 @@ class MechanizeCr::FormContent::Field
def dom_class
node.fetch("class", "")
end
def inspect # :nodoc:
"[%s:0x%x type: %s name: %s value: %s]" % [
self.class.name.sub(/Mechanize::FormContent::/, "").downcase,
object_id, type, name, value
]
end
end

View File

@ -24,7 +24,7 @@ class MechanizeCr::Page < MechanizeCr::File
def forms
forms = css("form").map do |html_form|
form = Form.new(html_form)
form = Form.new(html_form, self)
form.action ||= @uri.to_s
form
end.to_a