Skip to content

Commit a84ad7f

Browse files
committed
tidy: CSS.xpath_for can take keyword options
Related to #3200
1 parent fb19c55 commit a84ad7f

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

lib/nokogiri/css.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,24 @@ def parse(selector) # :nodoc:
7070
#
7171
# Nokogiri::CSS.xpath_for("h1, h2, h3") # => ["//h1", "//h2", "//h3"]
7272
#
73-
def xpath_for(selector, options = {})
74-
raise TypeError, "no implicit conversion of #{selector.inspect} to String" unless selector.respond_to?(:to_str)
73+
def xpath_for(
74+
selector, options = nil,
75+
prefix: options&.delete(:prefix), visitor: options&.delete(:visitor), ns: options&.delete(:ns)
76+
)
77+
unless options.nil?
78+
warn("Passing options as an explicit hash is deprecated. Use keyword arguments instead. This will become an error in a future release.", uplevel: 1, category: :deprecated)
79+
end
7580

76-
selector = selector.to_str
77-
raise Nokogiri::CSS::SyntaxError, "empty CSS selector" if selector.empty?
81+
raise(TypeError, "no implicit conversion of #{selector.inspect} to String") unless selector.respond_to?(:to_str)
7882

79-
if options.key?(:prefix) && options.key?(:visitor)
80-
raise ArgumentError, "cannot provide both :prefix and :visitor"
81-
end
83+
selector = selector.to_str
84+
raise(Nokogiri::CSS::SyntaxError, "empty CSS selector") if selector.empty?
8285

83-
ns = options.fetch(:ns, {})
86+
raise ArgumentError, "cannot provide both :prefix and :visitor" if prefix && visitor
8487

85-
visitor = if options.key?(:prefix)
86-
Nokogiri::CSS::XPathVisitor.new(prefix: options.fetch(:prefix))
87-
elsif options.key?(:visitor)
88-
options.fetch(:visitor)
88+
ns ||= {}
89+
visitor ||= if prefix
90+
Nokogiri::CSS::XPathVisitor.new(prefix: prefix)
8991
else
9092
Nokogiri::CSS::XPathVisitor.new
9193
end

lib/nokogiri/xml/searchable.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,7 @@ def xpath_query_from_css_rule(rule, ns)
249249
doctype: document.xpath_doctype,
250250
prefix: implied_xpath_context,
251251
)
252-
CSS.xpath_for(rule.to_s, {
253-
ns: ns,
254-
visitor: visitor,
255-
})
252+
CSS.xpath_for(rule.to_s, ns: ns, visitor: visitor)
256253
end.join(" | ")
257254
end
258255

test/css/test_css.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
end
2323

2424
it "accepts an options hash" do
25-
assert_equal(
26-
["./foo"],
27-
Nokogiri::CSS.xpath_for("foo", { prefix: "./" }),
28-
)
29-
assert_equal(
30-
["./foo"],
31-
Nokogiri::CSS.xpath_for("foo", { visitor: Nokogiri::CSS::XPathVisitor.new(prefix: "./") }),
32-
)
25+
assert_output(nil, /Passing options as an explicit hash is deprecated/) do
26+
assert_equal(
27+
["./foo"],
28+
Nokogiri::CSS.xpath_for("foo", { prefix: "./" }),
29+
)
30+
end
31+
32+
assert_output(nil, /Passing options as an explicit hash is deprecated/) do
33+
assert_equal(
34+
["./foo"],
35+
Nokogiri::CSS.xpath_for("foo", { visitor: Nokogiri::CSS::XPathVisitor.new(prefix: "./") }),
36+
)
37+
end
3338
end
3439

3540
it "accepts keyword arguments" do

0 commit comments

Comments
 (0)