*_with method deals :text key

master
Kanezoh 2021-06-28 16:36:24 +09:00
parent 3fc6227712
commit 8fa89fab2d
2 changed files with 10 additions and 11 deletions

View File

@ -4,6 +4,10 @@ class Node < Hash(String,String)
def css(str) def css(str)
[] of Hash(String,String) [] of Hash(String,String)
end end
def inner_text
""
end
end end
# This is a real Node got from html. # This is a real Node got from html.

View File

@ -6,26 +6,21 @@ module MechanzeCr::ElementMatcher
end end
def {{plural.id}}_with(criteria, &block) def {{plural.id}}_with(criteria, &block)
value = Hash(String,String).new
if criteria.is_a?(String) if criteria.is_a?(String)
criteria = {"name" => criteria} criteria = {"name" => criteria}
else else
criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h|
k = k.to_s
h[k] = v 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
end end
f = {{plural.id}}.select do |elm| f = {{plural.id}}.select do |elm|
criteria.all? do |k,v| 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
end end
yield f yield f