Skip to content

Commit

Permalink
tidy: CSS.xpath_for can take keyword options
Browse files Browse the repository at this point in the history
Related to #3200
  • Loading branch information
flavorjones committed Jun 11, 2024
1 parent 0bf11c1 commit a8e6957
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
26 changes: 14 additions & 12 deletions lib/nokogiri/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ def parse(selector) # :nodoc:
#
# 💡 Note that translated queries are cached for performance concerns.
#
def xpath_for(selector, options = {})
raise TypeError, "no implicit conversion of #{selector.inspect} to String" unless selector.respond_to?(:to_str)
def xpath_for(
selector, options = nil,
prefix: options&.delete(:prefix), visitor: options&.delete(:visitor), ns: options&.delete(:ns)
)
unless options.nil?
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)
end

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

if options.key?(:prefix) && options.key?(:visitor)
raise ArgumentError, "cannot provide both :prefix and :visitor"
end
selector = selector.to_str
raise(Nokogiri::CSS::SyntaxError, "empty CSS selector") if selector.empty?

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

visitor = if options.key?(:prefix)
Nokogiri::CSS::XPathVisitor.new(prefix: options.fetch(:prefix))
elsif options.key?(:visitor)
options.fetch(:visitor)
ns ||= {}
visitor ||= if prefix
Nokogiri::CSS::XPathVisitor.new(prefix: prefix)
else
Nokogiri::CSS::XPathVisitor.new
end
Expand Down
5 changes: 1 addition & 4 deletions lib/nokogiri/xml/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ def xpath_query_from_css_rule(rule, ns)
doctype: document.xpath_doctype,
prefix: implied_xpath_context,
)
CSS.xpath_for(rule.to_s, {
ns: ns,
visitor: visitor,
})
CSS.xpath_for(rule.to_s, ns: ns, visitor: visitor)
end.join(" | ")
end

Expand Down
21 changes: 13 additions & 8 deletions test/css/test_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
end

it "accepts an options hash" do
assert_equal(
["./foo"],
Nokogiri::CSS.xpath_for("foo", { prefix: "./" }),
)
assert_equal(
["./foo"],
Nokogiri::CSS.xpath_for("foo", { visitor: Nokogiri::CSS::XPathVisitor.new(prefix: "./") }),
)
assert_output(nil, /Passing options as an explicit hash is deprecated/) do
assert_equal(
["./foo"],
Nokogiri::CSS.xpath_for("foo", { prefix: "./" }),
)
end

assert_output(nil, /Passing options as an explicit hash is deprecated/) do
assert_equal(
["./foo"],
Nokogiri::CSS.xpath_for("foo", { visitor: Nokogiri::CSS::XPathVisitor.new(prefix: "./") }),
)
end
end

it "accepts keyword arguments" do
Expand Down

0 comments on commit a8e6957

Please sign in to comment.