diff --git a/ext/java/nokogiri/XmlXpathContext.java b/ext/java/nokogiri/XmlXpathContext.java index 433717492fa..dc476d2eea1 100644 --- a/ext/java/nokogiri/XmlXpathContext.java +++ b/ext/java/nokogiri/XmlXpathContext.java @@ -106,7 +106,7 @@ public class XmlXpathContext extends RubyObject { String query = rbQuery.convertToString().asJavaString(); - if (!handler.isNil() && !isContainsPrefix(query)) { + if (!isContainsPrefix(query)) { // // The user has passed in a handler, but isn't using the `nokogiri:` prefix as // instructed in JRuby land, so let's try to be clever and rewrite the query, inserting @@ -131,9 +131,10 @@ public class XmlXpathContext extends RubyObject jchar = xpathFunctionCalls.end(); } - if (jchar < query.length() - 1) { + if (jchar < query.length()) { namespacedQuery.append(query.subSequence(jchar, query.length())); } + query = namespacedQuery.toString(); } diff --git a/lib/nokogiri/xml/searchable.rb b/lib/nokogiri/xml/searchable.rb index 5f0ed24792e..639f9770f2e 100644 --- a/lib/nokogiri/xml/searchable.rb +++ b/lib/nokogiri/xml/searchable.rb @@ -253,7 +253,7 @@ def extract_params(params) # :nodoc: ![Hash, String, Symbol].include?(param.class) end params -= [handler] if handler - handler = XPathFunctions.new(handler) if handler + handler = XPathFunctions.wrap(handler) hashes = [] while Hash === params.last || params.last.nil? diff --git a/lib/nokogiri/xml/xpath_functions.rb b/lib/nokogiri/xml/xpath_functions.rb index 26c44a63ad8..2ad5f2c5e67 100644 --- a/lib/nokogiri/xml/xpath_functions.rb +++ b/lib/nokogiri/xml/xpath_functions.rb @@ -5,6 +5,15 @@ module Nokogiri module XML class XPathFunctions < SimpleDelegator + class << self + def wrap(handler) + if handler.nil? + @wrap_nil ||= new(Object.new) + else + new(handler) + end + end + end end end end