Fix xpath functions related to names#343
Open
tompng wants to merge 2 commits into
Open
Conversation
Fix and simplify name(nodesets), local-name(nodesets) and namespace-uri(nodesets) node select logic. All functions that uses a single node should use the first document-ordered node, but these functions were wrongly skipping un-named nodes.
There was a problem hiding this comment.
Pull request overview
This PR fixes XPath name-related functions (name(), local-name(), namespace-uri()) so they correctly use the first document-ordered node (even if it’s not a named node), rather than skipping unnamed nodes and selecting a later named node.
Changes:
- Refactors
local_name,name, andnamespace_urito share a simplified “pick first document-ordered node” helper. - Updates and expands
test_local_nameto avoid whitespace-text-node sensitivity and to cover unnamed-node behavior. - Adds new test cases for attribute and non-named node inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
lib/rexml/functions.rb |
Simplifies node selection logic for name-related XPath functions via a new helper method. |
test/functions/test_local_name.rb |
Adjusts existing node-set construction and adds new cases to validate the corrected behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| result | ||
| elsif node_set.respond_to? :namespace | ||
| yield node_set | ||
| def target_named_node(node_set = nil) |
Comment on lines
+78
to
82
| when nil | ||
| node = @context[:node] | ||
| when Array | ||
| node = XPathParser.sort(node_set).first | ||
| end |
Comment on lines
+19
to
20
| node_set = document.root.elements.to_a | ||
| assert_equal("child", REXML::Functions.local_name(node_set)) |
Comment on lines
+38
to
+42
| def test_attribute | ||
| document = REXML::Document.new("<root xmlns:x='http://example.com/x/' x:attr='value' />") | ||
| node_set = [document.root.attributes.to_a.last] | ||
| assert_equal("attr", REXML::Functions.local_name(node_set)) | ||
| end |
Comment on lines
+47
to
+49
| assert_equal("", REXML::Functions.local_name([children[0]])) | ||
| assert_equal("", REXML::Functions.local_name([children[1]])) | ||
| assert_equal("", REXML::Functions.local_name(children)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix and simplify name(nodesets), local-name(nodesets) and namespace-uri(nodesets) node select logic. All functions that uses a single node should use the first document-ordered node, but these functions were wrongly skipping un-named nodes.