Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,21 @@ def PathExpr path, parsed
path = path.lstrip
n = []
rest = FilterExpr( path, n )
if rest != path
if rest and rest[0] == ?/
if rest == path
rest = LocationPath(rest, n)
else
Comment on lines +590 to +592
if /\A\s*\//.match?(rest)
rest = rest.lstrip
if rest.start_with?('//')
n << :descendant_or_self
n << :node
rest = rest[2..-1]
else # starts with '/'
rest = rest[1..-1]
end
Comment on lines +593 to +601
rest = RelativeLocationPath(rest, n)
Comment on lines +593 to 602
parsed.concat(n)
return rest
end
end
Comment thread
tompng marked this conversation as resolved.
rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w*]/
parsed.concat(n)
rest
end
Expand Down
13 changes: 13 additions & 0 deletions test/parser/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,18 @@ def test_spaces_between_tokens
parser.parse('//processing-instruction( "a" )'),
)
end

def test_mult_asterisk_without_surrounding_spaces
parser = REXML::Parsers::XPathParser.new
assert_equal(parser.parse("1 * 2 * 3"), parser.parse("1*2*3"))
assert_equal(parser.parse("a[( ( 1 + 2 ) * 3 + 4 * ( 5 + 6 ) ) * 7 < 8]"), parser.parse("a[((1+2)*3+4*(5+6))*7<8]"))
# number(a/b) * 2
assert_equal(parser.parse("(a/b) * 2"), parser.parse("(a/b)*2"))
assert_equal(parser.parse("a/b * 2"), parser.parse("a/b*2"))
# number(a/b/*) * 2
assert_equal(parser.parse("a/b/* * 2"), parser.parse("a/b/**2"))
# number(*) * number(*/*) * number(*)
assert_equal(parser.parse("* * */* * *"), parser.parse("***/***"))
end
end
end
27 changes: 27 additions & 0 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,33 @@ def test_nested_predicates
assert_equal without_parentheses, with_parentheses
end

def test_parenthesized_xpath
doc = Document.new <<-EOF
<div>
<div id='1'>
<test>ab</test>
<div><test>cd</test></div>
</div>
<div id='2'>
<test>ef</test>
<div><test>gh</test></div>
</div>
<div>
<test>hi</test>
</div>
</div>
EOF
parenthesized_xpath = '(//div[@id="1"] | //div[@id="2"])'
assert_equal(%w[ab ef], XPath.match(doc, "#{parenthesized_xpath}/test").map(&:text))
assert_equal(%w[ab ef], XPath.match(doc, "#{parenthesized_xpath} / test").map(&:text))
assert_equal(%w[ab cd ef gh], XPath.match(doc, "#{parenthesized_xpath}//test").map(&:text))
assert_equal(%w[ab cd ef gh], XPath.match(doc, "#{parenthesized_xpath} // test").map(&:text))
assert_equal(%w[ab], XPath.match(doc, "#{parenthesized_xpath}[@id='1']/test").map(&:text))
assert_equal(%w[ef gh], XPath.match(doc, "#{parenthesized_xpath}[@id='2']//test").map(&:text))
assert_equal(%w[ef], XPath.match(doc, "#{parenthesized_xpath}[2] / test").map(&:text))
assert_equal(%w[ab cd], XPath.match(doc, "#{parenthesized_xpath}[1] // test").map(&:text))
end

# Contributed by Mike Stok
def test_starts_with
source = <<-EOF
Expand Down
Loading