/
filter.rb
55 lines (45 loc) · 1002 Bytes
/
filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Capybara
class Filter
attr_reader :name
class << self
def all
@all ||= {}
end
def add(name, &block)
all[name.to_sym] = new(name.to_sym, &block)
end
def remove(name)
all.delete(name.to_sym)
end
end
def initialize(name, &block)
@name = name
instance_eval(&block) if block
end
def match(&block)
@match = block if block
@match
end
def compile(&block)
@compile = block if block
@compile
end
def run_compile(xpath, value)
@compile and XPath.instance_exec(xpath, value, &@compile)
end
def match?(node, value)
@match and @match.call(node, value)
end
end
end
Capybara::Filter.add :text do
match do |node, value|
regexp = value.is_a?(Regexp) ? value : Regexp.escape(value)
node.text.match(regexp)
end
end
Capybara::Filter.add :visible do
match do |node, value|
if value then node.visible? else true end
end
end