From 6ccb70dcb73813d872fc842e5203553886db2cf8 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 17 Jun 2021 00:11:04 +0900 Subject: [PATCH] wip add radiobuttons --- src/mechanize/form.cr | 3 ++ src/mechanize/form/check_box.cr | 25 +----------- src/mechanize/form/radio_button.cr | 62 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 src/mechanize/form/radio_button.cr diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 1bfcd2f..46d525b 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -1,9 +1,11 @@ require "./form/field" require "./form/check_box" +require "./form/radio_button" class MechanizeCr::Form getter fields : Array(FormContent::Field) getter checkboxes : Array(FormContent::CheckBox) + getter radiobuttons : Array(FormContent::RadioButton) getter enctype : String getter method : String getter name : String @@ -14,6 +16,7 @@ class MechanizeCr::Form @node = node @fields = Array(FormContent::Field).new @checkboxes = Array(FormContent::CheckBox).new + @radiobuttons = Array(FormContent::RadioButton).new @action = node.fetch("action", "") @method = node.fetch("method", "GET").upcase @name = node.fetch("name", "") diff --git a/src/mechanize/form/check_box.cr b/src/mechanize/form/check_box.cr index 0cf336b..98c0251 100644 --- a/src/mechanize/form/check_box.cr +++ b/src/mechanize/form/check_box.cr @@ -1,29 +1,8 @@ -class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::Field - property :checked, :form - - def initialize(node : Node | Myhtml::Node, form : Form) - @checked = !!node["checked"] - @form = form - super(node) - end - +require "./radio_button" +class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::RadioButton def check - #uncheck_peers @checked = true end - - def uncheck - @checked = false - end - - def checked? - checked - end - - def click - checked ? uncheck : check - end - def query_value [@name, @value || "on"] end diff --git a/src/mechanize/form/radio_button.cr b/src/mechanize/form/radio_button.cr new file mode 100644 index 0000000..4480af6 --- /dev/null +++ b/src/mechanize/form/radio_button.cr @@ -0,0 +1,62 @@ +class MechanizeCr::FormContent::RadioButton < MechanizeCr::FormContent::Field + property :checked, :form + + def initialize(node : Node | Myhtml::Node, form : Form) + @checked = !!node["checked"] + @form = form + super(node) + end + + def check + #uncheck_peers + @checked = true + end + + def uncheck + @checked = false + end + + def click + checked ? uncheck : check + end + + def checked? + checked + end + + #def hash # :nodoc: + # @form.hash ^ @name.hash ^ @value.hash + #end +# + #def label + # (id = self['id']) && @form.page.labels_hash[id] || nil + #end +# + #def text + # label.text rescue nil + #end +# + #def [](key) + # @node[key] + #end + + # alias checked? checked + + #def == other # :nodoc: + # self.class === other and + # other.form == @form and + # other.name == @name and + # other.value == @value + #end +# + #alias eql? == # :nodoc: + + + #private def uncheck_peers + # @form.radiobuttons_with(:name => name).each do |b| + # next if b.value == value + # b.uncheck + # end + #end + +end