Skip to content

Commit de6f40e

Browse files
authored
Fix reverse sort in xpath_parser (#251)
The code below was failing with `REXML::XPathParser#sort': undefined method '-@' for an instance of Array` ```ruby d = REXML::Document.new("<a><b><c/><d/><x/></b><b><e/><x/></b></a>") matches = REXML::XPath.match(d, "a/b/x/preceding-sibling::node()") # Before: error # After: [<e/>, <d/>, <c/>] ``` This pull request will fix it.
1 parent d944fa4 commit de6f40e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/rexml/xpath_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def sort(array_of_nodes, order)
671671
if order == :forward
672672
index
673673
else
674-
-index
674+
index.map(&:-@)
675675
end
676676
end
677677
ordered.collect do |_index, node|

test/xpath/test_base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ def test_preceding
416416
assert_equal( 4, cs.length )
417417
end
418418

419+
def test_preceding_sibling
420+
d = REXML::Document.new("<a><b><c/><d/><x/></b><b><e/><x/></b></a>")
421+
matches = REXML::XPath.match(d, "a/b/x/preceding-sibling::node()")
422+
assert_equal(["e", "d", "c"], matches.map(&:name))
423+
end
424+
419425
def test_following
420426
d = Document.new "<a><b id='0'/><b/><b><c id='1'/><c id='2'/></b><b id='1'/></a>"
421427
start = XPath.first( d, "/a/b[@id='0']" )

0 commit comments

Comments
 (0)