From a6111294adf425c349b8b031e8637dae59c5633b Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 1 Jul 2021 19:08:28 +0900 Subject: [PATCH] add select list options --- src/mechanize/form/option.cr | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/mechanize/form/option.cr diff --git a/src/mechanize/form/option.cr b/src/mechanize/form/option.cr new file mode 100644 index 0000000..7f423c5 --- /dev/null +++ b/src/mechanize/form/option.cr @@ -0,0 +1,41 @@ +class MechanizeCr::FormContent::Option + getter select_list : FormContent::MultiSelectList + getter node : Myhtml::Node + getter text : String + getter value : String + getter selected : Bool + def initialize(node, select_list) + @node = node + @text = node.inner_text + @value = (node["value"] || node.inner_text) + @selected = node.has_key?("selected") + @select_list = select_list # The select list this option belongs to + end + + # Select this option + def select + unselect_peers + @selected = true + end + + # Unselect this option + def unselect + @selected = false + end + + # Toggle the selection value of this option + def click + unselect_peers + @selected = !@selected + end + + def selected? + @selected + end + + private def unselect_peers + return unless SelectList === @select_list + + @select_list.select_none + end +end