From 563197134781e6111e1e342d0f557967e4386e54 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 29 Jun 2021 20:21:44 +0900 Subject: [PATCH] raise error when submit with invalid button --- src/mechanize/form.cr | 20 ++++++++++---------- src/mechanize/form/button.cr | 5 +++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 7bfe6e1..1e5d795 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -61,11 +61,11 @@ class MechanizeCr::Form when "radio" radiobuttons << FormContent::RadioButton.new(html_node, self) when "button" - buttons << FormContent::Button.new(html_node) + buttons << FormContent::Button.new(html_node, @node) when "submit" - buttons << FormContent::SubmitButton.new(html_node) + buttons << FormContent::SubmitButton.new(html_node, @node) when"reset" - buttons << FormContent::ResetButton.new(html_node) + buttons << FormContent::ResetButton.new(html_node, @node) when "text" fields << FormContent::Text.new(html_node) when "hidden" @@ -130,13 +130,13 @@ class MechanizeCr::Form # 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.node == @node - # message = "" - # "#{button.inspect} does not belong to the same page as " \ - # "the form #{@name.inspect} in #{@page.uri}" -# - # raise ArgumentError, message - #end + unless button.form_node == @node + #message = "" + # "#{button.inspect} does not belong to the same page as " \ + # "the form #{@name.inspect} in #{@page.uri}" + message = "not a valid button" + raise ArgumentError.new(message) + end @clicked_buttons << button end diff --git a/src/mechanize/form/button.cr b/src/mechanize/form/button.cr index f932aa7..5a52bbe 100644 --- a/src/mechanize/form/button.cr +++ b/src/mechanize/form/button.cr @@ -1,4 +1,9 @@ class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field + getter form_node : Node | Myhtml::Node + def initialize(node : Node | Myhtml::Node, form_node : Node | Myhtml::Node, value=nil) + @form_node = form_node + super(node, value) + end end require "./reset_button" require "./submit_button"