diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 69aaef9..a353221 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -9,6 +9,7 @@ require "./utils/element_matcher" class MechanizeCr::Form include MechanzeCr::ElementMatcher + getter node : Node | Myhtml::Node getter fields : Array(FormContent::Field) getter checkboxes : Array(FormContent::CheckBox) getter radiobuttons : Array(FormContent::RadioButton) diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index d6cdb93..be8d3a5 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -1,6 +1,8 @@ require "./file" +require "./utils/element_matcher" class MechanizeCr::Page < MechanizeCr::File + include MechanzeCr::ElementMatcher delegate :css, to: parser def initialize(uri, response, body, code) @@ -27,4 +29,9 @@ class MechanizeCr::Page < MechanizeCr::File form end.to_a end + + # generate form_with, forms_with methods + # ex) form_with({:name => "login_form"}) + # it detects form(s) which match conditions. + elements_with "form" end diff --git a/src/mechanize/utils/element_matcher.cr b/src/mechanize/utils/element_matcher.cr index 8036f78..c4f20f4 100644 --- a/src/mechanize/utils/element_matcher.cr +++ b/src/mechanize/utils/element_matcher.cr @@ -10,9 +10,9 @@ module MechanzeCr::ElementMatcher if criteria.is_a?(String) criteria = {"name" => criteria} else - # now, criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| h[k] = v + # TODO: to deal with when key is "text" #case k = k.to_s #when "id" # h["id"] = v @@ -25,7 +25,7 @@ module MechanzeCr::ElementMatcher end f = {{plural.id}}.select do |elm| criteria.all? do |k,v| - v === elm.node.fetch(k,"x") + v === elm.node.fetch(k,"") end end yield f