Skip to content

Commit

Permalink
prefactor: re-order searchable private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Dec 14, 2024
1 parent 749c582 commit a21aad5
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions lib/nokogiri/xml/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,26 @@ def >(selector) # rubocop:disable Naming/BinaryOperatorParameterName

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)
def extract_params(params) # :nodoc:
handler = params.find do |param|
![Hash, String, Symbol].include?(param.class)
end
params -= [handler] if handler

NodeSet.new(document) do |combined|
paths.each do |path|
xpath_impl(node, path, handler, ns, binds).each { |set| combined << set }
end
hashes = []
while Hash === params.last || params.last.nil?
hashes << params.pop
break if params.empty?
end
end
ns, binds = hashes.reverse

def xpath_impl(node, path, handler, ns, binds)
ctx = XPathContext.new(node)
ctx.register_namespaces(ns)
path = path.gsub("xmlns:", " :") unless Nokogiri.uses_libxml?
ns ||= document.root&.namespaces || {}

binds&.each do |key, value|
ctx.register_variable(key.to_s, value)
end
[params, handler, ns, binds]
end

ctx.evaluate(path, handler)
def css_internal(node, rules, handler, ns)
xpath_internal(node, css_rules_to_xpath(rules, ns), handler, ns, nil)
end

def css_rules_to_xpath(rules, ns)
Expand All @@ -254,22 +245,31 @@ def xpath_query_from_css_rule(rule, ns)
end.join(" | ")
end

def extract_params(params) # :nodoc:
handler = params.find do |param|
![Hash, String, Symbol].include?(param.class)
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
params -= [handler] if handler

hashes = []
while Hash === params.last || params.last.nil?
hashes << params.pop
break if params.empty?
NodeSet.new(document) do |combined|
paths.each do |path|
xpath_impl(node, path, handler, ns, binds).each { |set| combined << set }
end
end
ns, binds = hashes.reverse
end

ns ||= document.root&.namespaces || {}
def xpath_impl(node, path, handler, ns, binds)
ctx = XPathContext.new(node)
ctx.register_namespaces(ns)
path = path.gsub("xmlns:", " :") unless Nokogiri.uses_libxml?

[params, handler, ns, binds]
binds&.each do |key, value|
ctx.register_variable(key.to_s, value)
end

ctx.evaluate(path, handler)
end
end
end
Expand Down

0 comments on commit a21aad5

Please sign in to comment.