Skip to content

Commit

Permalink
xpath abbreviate: add a special case for only "//"
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed May 28, 2023
1 parent 3ddbdfc commit 957e50e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def abbreviate(path_or_parsed)
end
components = []
component = nil
previous_op = nil
while parsed.size > 0
op = parsed.shift
case op
Expand Down Expand Up @@ -119,10 +118,12 @@ def abbreviate(path_or_parsed)
component << op.inspect
component << ")"
end
previous_op = op
end
if components == [""]
case components
when [""]
"/"
when ["", ""]
"//"
else
components.join("/")
end
Expand Down
5 changes: 5 additions & 0 deletions test/parser/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def test_document
abbreviate("/"))
end

def test_descendant_or_self_only
assert_equal("//",
abbreviate("/descendant-or-self::node()/"))
end

def test_descendant_or_self_absolute
assert_equal("//a/b",
abbreviate("/descendant-or-self::node()/a/b"))
Expand Down

0 comments on commit 957e50e

Please sign in to comment.