Skip to content

Commit

Permalink
feat(cruby): custom XPath functions support nokogiri namespace
Browse files Browse the repository at this point in the history
See #2147 for context and deprecation roadmap.
  • Loading branch information
flavorjones committed Aug 29, 2022
1 parent 805e766 commit a55e79e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This version of Nokogiri uses [`jar-dependencies`](https://github.com/mkristian/
### Added

* [CRuby] The HTML5 parser handles the [new `search` element](https://github.com/whatwg/html/pull/7320). [#2566](https://github.com/sparklemotion/nokogiri/issues/2566)
* [CRuby] Invocation of custom XPath or CSS handler functions may now use the `nokogiri` namespace prefix. Historically, the JRuby implementation _required_ this namespace but the CRuby implementation did not support it. It's recommended that all XPath and CSS queries use the `nokogiri` namespace going forward. Invocation without the namespace is planned for deprecation in v1.15.0 and removal in a future release. [[#2147](https://github.com/sparklemotion/nokogiri/issues/2147)]


### Fixed
Expand Down
3 changes: 3 additions & 0 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ VALUE cNokogiriXmlXpathContext;
* these constants have matching declarations in
* ext/java/nokogiri/internals/NokogiriNamespaceContext.java
*/
static const xmlChar *NOKOGIRI_PREFIX = (const xmlChar *)"nokogiri";
static const xmlChar *NOKOGIRI_URI = (const xmlChar *)"http://www.nokogiri.org/default_ns/ruby/extensions_functions";
static const xmlChar *NOKOGIRI_BUILTIN_PREFIX = (const xmlChar *)"nokogiri-builtin";
static const xmlChar *NOKOGIRI_BUILTIN_URI = (const xmlChar *)"https://www.nokogiri.org/default_ns/ruby/builtins";

Expand Down Expand Up @@ -382,6 +384,7 @@ new (VALUE klass, VALUE nodeobj)
ctx = xmlXPathNewContext(node->doc);
ctx->node = node;

xmlXPathRegisterNs(ctx, NOKOGIRI_PREFIX, NOKOGIRI_URI);
xmlXPathRegisterNs(ctx, NOKOGIRI_BUILTIN_PREFIX, NOKOGIRI_BUILTIN_URI);
xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"css-class", NOKOGIRI_BUILTIN_URI,
xpath_builtin_css_class);
Expand Down
10 changes: 10 additions & 0 deletions test/xml/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ def test_search_with_xpath_query_uses_custom_selectors_with_arguments
end
end

def test_search_with_xpath_query_using_namespaced_custom_function
# Note that invocation with namespaces was not historically supported in CRuby.
# see https://github.com/sparklemotion/nokogiri/issues/2147 for more context.
set = @xml.xpath('//employee/address[nokogiri:my_filter(., "domestic", "Yes")]', @handler)
refute_empty(set)
set.each do |node|
assert_equal("Yes", node["domestic"])
end
end

def test_pass_self_to_function
set = if Nokogiri.uses_libxml?
@xml.xpath('//employee/address[my_filter(., "domestic", "Yes")]', @handler)
Expand Down

0 comments on commit a55e79e

Please sign in to comment.