Skip to content

Commit

Permalink
prefactor: extract XPathContext#register_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Dec 14, 2024
1 parent a21aad5 commit 5822532
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/nokogiri/xml/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,9 @@ def xpath_internal(node, paths, handler, ns, binds)
def xpath_impl(node, path, handler, ns, binds)
ctx = XPathContext.new(node)
ctx.register_namespaces(ns)
path = path.gsub("xmlns:", " :") unless Nokogiri.uses_libxml?
ctx.register_variables(binds)

binds&.each do |key, value|
ctx.register_variable(key.to_s, value)
end
path = path.gsub("xmlns:", " :") unless Nokogiri.uses_libxml?

ctx.evaluate(path, handler)
end
Expand Down
17 changes: 14 additions & 3 deletions lib/nokogiri/xml/xpath_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@ class XPathContext
###
# Register namespaces in +namespaces+
def register_namespaces(namespaces)
namespaces.each do |k, v|
k = k.to_s.gsub(/.*:/, "") # strip off 'xmlns:' or 'xml:'
register_ns(k, v)
namespaces.each do |key, value|
key = key.to_s.gsub(/.*:/, "") # strip off 'xmlns:' or 'xml:'

register_ns(key, value)
end
end

def register_variables(binds)
return if binds.nil?

binds.each do |key, value|
key = key.to_s

register_variable(key, value)
end
end
end
Expand Down

0 comments on commit 5822532

Please sign in to comment.