From 4ccbb38f4d4491bea9485e4dc4495eff2f059744 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 17 Jun 2021 14:10:53 +0900 Subject: [PATCH] include radiobutton to request data --- spec/form_spec.cr | 8 +++++++- src/mechanize/form.cr | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/spec/form_spec.cr b/spec/form_spec.cr index d111058..caaaad3 100644 --- a/spec/form_spec.cr +++ b/spec/form_spec.cr @@ -62,7 +62,7 @@ describe "Mechanize Form test" do checkbox.click checkbox.checked?.should eq true end - it "doesn't include request data if checkbox isn't checked" do + it "doesn't included in request data if checkbox isn't checked" do form.request_data.should contain("remember_me=on") checkbox.uncheck form.request_data.should_not contain("remember_me=") @@ -96,5 +96,11 @@ describe "Mechanize Form test" do radiobuttons[1].checked.should eq true radiobuttons[0].checked.should eq false end + it "doesn't included in request data if checkbox isn't checked" do + radiobuttons[0].check + form.request_data.should contain "contact=email" + radiobuttons[0].uncheck + form.request_data.should_not contain "contact" + end end end diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 9503972..bb71b3b 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -105,6 +105,27 @@ class MechanizeCr::Form 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 MechanizeCr::Error.new + else + successful_controls << checked.first unless checked.empty? + end + end + successful_controls.each do |ctrl| value = ctrl.query_value next if value[0] == ""