From 8fa89fab2d1a7e01b506fb7c54bea7d82a7ee81e Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Mon, 28 Jun 2021 16:36:24 +0900 Subject: [PATCH] *_with method deals :text key --- src/mechanize/node.cr | 4 ++++ src/mechanize/utils/element_matcher.cr | 17 ++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mechanize/node.cr b/src/mechanize/node.cr index b464b0b..f142efc 100644 --- a/src/mechanize/node.cr +++ b/src/mechanize/node.cr @@ -4,6 +4,10 @@ class Node < Hash(String,String) def css(str) [] of Hash(String,String) end + + def inner_text + "" + end end # This is a real Node got from html. diff --git a/src/mechanize/utils/element_matcher.cr b/src/mechanize/utils/element_matcher.cr index c4f20f4..c2d1582 100644 --- a/src/mechanize/utils/element_matcher.cr +++ b/src/mechanize/utils/element_matcher.cr @@ -6,26 +6,21 @@ module MechanzeCr::ElementMatcher end def {{plural.id}}_with(criteria, &block) - value = Hash(String,String).new if criteria.is_a?(String) criteria = {"name" => criteria} else criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| + k = k.to_s h[k] = v - # TODO: to deal with when key is "text" - #case k = k.to_s - #when "id" - # h["id"] = v - #when "class" - # h["class"] = v - #else - # h[k] = v - #end end end f = {{plural.id}}.select do |elm| criteria.all? do |k,v| - v === elm.node.fetch(k,"") + if k == "text" + v == elm.node.inner_text + else + v == elm.node.fetch(k,"") + end end end yield f