-
-
Notifications
You must be signed in to change notification settings - Fork 903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add global XPath functions handler #1894
Conversation
Code Climate has analyzed commit c3376dd and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 100.0% (80% is the threshold). This pull request will bring the total coverage in the repository to 93.5% (0.0% change). View more on Code Climate. |
28ddb33
to
c3376dd
Compare
@@ -114,9 +118,15 @@ public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject | |||
if (!handler.isNil()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition should now always be true (i.e. handler will always be an instance of XPathFunctions
). Should I remove this if
?
Thank you for submitting this PR! I'll take a look as soon as I can, this is a super-busy week for me. |
Sorry this has gone so long without review; I'm tagging it for v1.11.0 which I'm hoping to release at the end of 2019, so will review in that timeframe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
เล่นไรกันวะ
c3376dd
to
d5adb55
Compare
I've rebased this onto current |
d5adb55
to
abc9765
Compare
I've added a |
OK, the method lookup in the Here's the failing test that needs to be added: def test_search_with_xpath_query_uses_global_custom_selectors_with_arguments_without_namespace
skip("Testing fallback behavior in JRuby") unless Nokogiri.jruby?
XPathFunctions.class_eval do
def our_filter(*args)
my_filter(*args)
end
end
set = @xml.search('//employee/address[our_filter(., "domestic", "Yes")]', @handler)
refute_empty(set)
set.each do |node|
assert_equal("Yes", node["domestic"])
end
end I also want to point out that the intention is to deprecate use of custom XPath functions without the |
abc9765
to
25e6ac7
Compare
25e6ac7
to
2a2f9f9
Compare
@flavorjones Oh wow, I forgot about this one! I am fine with waiting until after #2147 — no rush. Still, I was curious to see if I could get it working, so I pushed a commit to fix any failing tests, including the one you mentioned ( |
Thank you! I'll take another look |
OK, I'm sorry this has been open for so long. After a lot of thought, I'm leaning towards not accepting this feature into Nokogiri. I think, over the last decade, Nokogiri has moved towards the bottom of most people's stack -- that is, the majority of dependencies on Nokogiri are indirect (via rails-html-sanitizer, for example) and not direct. As a result, I've grown less enthusiastic about introducing global behavior changes, simply because I want to reduce the blast radius should there be some sort of unexpected conflict between two libraries both trying to use this functionality. (I'm imagining two libraries each trying to implement a function with a common name, and a user having no way to diagnose or work around the problem.) I think that @mbklein's suggestion of making this into a document-specific I'm really sorry that this decision took so long to make, and I want to say thanks very much for putting in the work on this PR to try to improve Nokogiri! I'd be happy to keep discussing this, but I'm going to close this in the meantime. Thank you again! Small edits for clarity made on 2026-06-27. |
@flavorjones Thank you for explaining your thought process! 😄 |
What problem is this PR intended to solve?
Allow global definition of XPath functions, e.g.
This is a revival of #464 with a different approach. Although that PR is shown as merged, I think that's a GitHub bug. The original implementation was mbklein/nokogiri@8fb3da3 which has not been merged.
Have you included adequate test coverage?
I think so. I refrained from adding tests which would overlap with existing tests, but I would be happy to add more, if you have suggestions.
Does this change affect the C or the Java implementations?
No.Apparently, yes. The Java implementation injects XML namespaces into XPath queries based on the Ruby methods the function handler defines. Previously, that list of methods was derived from the function handler class, which omits methods delegated bySimpleDelegator
. Now the list of methods is derived directly from the function handler object.