/
searchable.rb
270 lines (240 loc) · 9.47 KB
/
searchable.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# coding: utf-8
# frozen_string_literal: true
module Nokogiri
module XML
#
# The Searchable module declares the interface used for searching your DOM.
#
# It implements the public methods #search, #css, and #xpath,
# as well as allowing specific implementations to specialize some
# of the important behaviors.
#
module Searchable
# Regular expression used by Searchable#search to determine if a query
# string is CSS or XPath
LOOKS_LIKE_XPATH = %r{^(\./|/|\.\.|\.$)}
# :section: Searching via XPath or CSS Queries
###
# call-seq:
# search(*paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class])
#
# Search this object for +paths+. +paths+ must be one or more XPath or CSS queries:
#
# node.search("div.employee", ".//title")
#
# A hash of namespace bindings may be appended:
#
# node.search('.//bike:tire', {'bike' => 'http://schwinn.com/'})
# node.search('bike|tire', {'bike' => 'http://schwinn.com/'})
#
# For XPath queries, a hash of variable bindings may also be appended to the namespace
# bindings. For example:
#
# node.search('.//address[@domestic=$value]', nil, {:value => 'Yes'})
#
# 💡 Custom XPath functions and CSS pseudo-selectors may also be defined. To define custom
# functions create a class and implement the function you want to define. The first argument
# to the method will be the current matching NodeSet. Any other arguments are ones that you
# pass in. Note that this class may appear anywhere in the argument list. For example:
#
# handler = Class.new {
# def regex node_set, regex
# node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
# end
# }.new
# node.search('.//title[regex(., "\w+")]', 'div.employee:regex("[0-9]+")', handler)
#
# See Searchable#xpath and Searchable#css for further usage help.
def search(*args)
paths, handler, ns, binds = extract_params(args)
xpaths = paths.map(&:to_s).map do |path|
LOOKS_LIKE_XPATH.match?(path) ? path : xpath_query_from_css_rule(path, ns)
end.flatten.uniq
xpath(*(xpaths + [ns, handler, binds].compact))
end
alias_method :/, :search
###
# call-seq:
# at(*paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class])
#
# Search this object for +paths+, and return only the first
# result. +paths+ must be one or more XPath or CSS queries.
#
# See Searchable#search for more information.
def at(*args)
search(*args).first
end
alias_method :%, :at
###
# call-seq:
# css(*rules, [namespace-bindings, custom-pseudo-class])
#
# Search this object for CSS +rules+. +rules+ must be one or more CSS
# selectors. For example:
#
# node.css('title')
# node.css('body h1.bold')
# node.css('div + p.green', 'div#one')
#
# A hash of namespace bindings may be appended. For example:
#
# node.css('bike|tire', {'bike' => 'http://schwinn.com/'})
#
# 💡 Custom CSS pseudo classes may also be defined which are mapped to a custom XPath
# function. To define custom pseudo classes, create a class and implement the custom pseudo
# class you want defined. The first argument to the method will be the matching context
# NodeSet. Any other arguments are ones that you pass in. For example:
#
# handler = Class.new {
# def regex(node_set, regex)
# node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
# end
# }.new
# node.css('title:regex("\w+")', handler)
#
# 💡 Some XPath syntax is supported in CSS queries. For example, to query for an attribute:
#
# node.css('img > @href') # returns all +href+ attributes on an +img+ element
# node.css('img / @href') # same
#
# # ⚠ this returns +class+ attributes from all +div+ elements AND THEIR CHILDREN!
# node.css('div @class')
#
# node.css
#
# 💡 Array-like syntax is supported in CSS queries as an alternative to using +:nth-child()+.
#
# ⚠ NOTE that indices are 1-based like +:nth-child+ and not 0-based like Ruby Arrays. For
# example:
#
# # equivalent to 'li:nth-child(2)'
# node.css('li[2]') # retrieve the second li element in a list
#
# ⚠ NOTE that the CSS query string is case-sensitive with regards to your document type. HTML
# tags will match only lowercase CSS queries, so if you search for "H1" in an HTML document,
# you'll never find anything. However, "H1" might be found in an XML document, where tags
# names are case-sensitive (e.g., "H1" is distinct from "h1").
def css(*args)
rules, handler, ns, _ = extract_params(args)
css_internal(self, rules, handler, ns)
end
##
# call-seq:
# at_css(*rules, [namespace-bindings, custom-pseudo-class])
#
# Search this object for CSS +rules+, and return only the first
# match. +rules+ must be one or more CSS selectors.
#
# See Searchable#css for more information.
def at_css(*args)
css(*args).first
end
###
# call-seq:
# xpath(*paths, [namespace-bindings, variable-bindings, custom-handler-class])
#
# Search this node for XPath +paths+. +paths+ must be one or more XPath
# queries.
#
# node.xpath('.//title')
#
# A hash of namespace bindings may be appended. For example:
#
# node.xpath('.//foo:name', {'foo' => 'http://example.org/'})
# node.xpath('.//xmlns:name', node.root.namespaces)
#
# A hash of variable bindings may also be appended to the namespace bindings. For example:
#
# node.xpath('.//address[@domestic=$value]', nil, {:value => 'Yes'})
#
# 💡 Custom XPath functions may also be defined. To define custom functions create a class and
# implement the function you want to define. The first argument to the method will be the
# current matching NodeSet. Any other arguments are ones that you pass in. Note that this
# class may appear anywhere in the argument list. For example:
#
# handler = Class.new {
# def regex(node_set, regex)
# node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
# end
# }.new
# node.xpath('.//title[regex(., "\w+")]', handler)
#
def xpath(*args)
paths, handler, ns, binds = extract_params(args)
xpath_internal(self, paths, handler, ns, binds)
end
##
# call-seq:
# at_xpath(*paths, [namespace-bindings, variable-bindings, custom-handler-class])
#
# Search this node for XPath +paths+, and return only the first
# match. +paths+ must be one or more XPath queries.
#
# See Searchable#xpath for more information.
def at_xpath(*args)
xpath(*args).first
end
# :call-seq:
# >(selector) → NodeSet
#
# Search this node's immediate children using CSS selector +selector+
def >(selector) # rubocop:disable Naming/BinaryOperatorParameterName
ns = (document.root&.namespaces || {})
xpath(CSS.xpath_for(selector, prefix: "./", ns: ns).first)
end
# :section:
private
def css_internal(node, rules, handler, ns)
xpath_internal(node, css_rules_to_xpath(rules, ns), handler, ns, nil)
end
def xpath_internal(node, paths, handler, ns, binds)
document = node.document
return NodeSet.new(document) unless document
if paths.length == 1
return xpath_impl(node, paths.first, handler, ns, binds)
end
NodeSet.new(document) do |combined|
paths.each do |path|
xpath_impl(node, path, handler, ns, binds).each { |set| combined << set }
end
end
end
def xpath_impl(node, path, handler, ns, binds)
ctx = XPathContext.new(node)
ctx.register_namespaces(ns)
path = path.gsub(/xmlns:/, " :") unless Nokogiri.uses_libxml?
binds&.each do |key, value|
ctx.register_variable(key.to_s, value)
end
ctx.evaluate(path, handler)
end
def css_rules_to_xpath(rules, ns)
rules.map { |rule| xpath_query_from_css_rule(rule, ns) }
end
def xpath_query_from_css_rule(rule, ns)
visitor = Nokogiri::CSS::XPathVisitor.new(
builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::OPTIMAL,
doctype: document.xpath_doctype,
)
self.class::IMPLIED_XPATH_CONTEXTS.map do |implied_xpath_context|
CSS.xpath_for(rule.to_s, { prefix: implied_xpath_context, ns: ns,
visitor: visitor, })
end.join(" | ")
end
def extract_params(params) # :nodoc:
handler = params.find do |param|
![Hash, String, Symbol].include?(param.class)
end
params -= [handler] if handler
hashes = []
while Hash === params.last || params.last.nil?
hashes << params.pop
break if params.empty?
end
ns, binds = hashes.reverse
ns ||= (document.root&.namespaces || {})
[params, handler, ns, binds]
end
end
end
end