From 5a98037965636cacfcce22c9f4a384b51e0557dc Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 1 Jul 2021 07:54:27 +0900 Subject: [PATCH] improve add_button_to_query error message --- src/mechanize/form.cr | 10 +++++----- src/mechanize/form/field.cr | 7 +++++++ src/mechanize/page.cr | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 81f8ed6..7ff677c 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -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 diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr index 7ecd22f..81b970e 100644 --- a/src/mechanize/form/field.cr +++ b/src/mechanize/form/field.cr @@ -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 diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index be8d3a5..5ff6760 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -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