Skip to content
Merged
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
23 changes: 12 additions & 11 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def LocationPath path, parsed
path = path[1..-1]
end
end
RelativeLocationPath( path, parsed ) if path.size > 0
RelativeLocationPath( path, parsed )
end

#RelativeLocationPath
Expand Down Expand Up @@ -564,19 +564,18 @@ def MultiplicativeExpr path, parsed
#| '-' UnaryExpr
#| UnionExpr
def UnaryExpr path, parsed
path =~ /^(\-*)/
path = $'
if $1 and (($1.size % 2) != 0)
mult = -1
else
mult = 1
end
parsed << :neg if mult < 0
original_path = path
path = path.lstrip
match_data = /\A((?:-\s*)*)/.match(path)
negative = match_data[1].count("-").odd?
path = match_data.post_match

n = []
path = UnionExpr( path, n )
rest = UnionExpr( path, n )
return original_path if rest == path
parsed << :neg if negative
parsed.concat( n )
path
rest
end

#| UnionExpr '|' PathExpr
Expand All @@ -601,11 +600,13 @@ def UnionExpr path, parsed
#| LocationPath
#| FilterExpr ('/' | '//') RelativeLocationPath
def PathExpr path, parsed
original_path = path
path = path.lstrip
n = []
rest = FilterExpr( path, n )
if rest == path
rest = LocationPath(rest, n)
return original_path if rest == path
else
if /\A\s*\//.match?(rest)
rest = rest.lstrip
Expand Down
34 changes: 34 additions & 0 deletions test/parser/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ def test_spaces_between_tokens
)
end

def test_unary_minus_with_spaces
parser = REXML::Parsers::XPathParser.new
assert_equal(
parser.parse('(-6) div -3'),
parser.parse(' ( - 6 ) div - 3 '),
)
assert_equal(
parser.parse('--6'),
parser.parse(' - - 6 '),
)
assert_equal(
parser.parse('-6'),
parser.parse(' -6'),
)
assert_equal(
parser.parse('//v[-1]'),
parser.parse('//v[ -1 ]'),
)
assert_equal(
parser.parse('//v[1] - --1'),
parser.parse('//v[1] - - - 1'),
)
end

def test_unary_minus_without_operand
parser = REXML::Parsers::XPathParser.new
assert_raise(REXML::ParseException) do
parser.parse('-')
end
assert_raise(REXML::ParseException) do
parser.parse(' - - ')
end
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"))
Expand Down
11 changes: 11 additions & 0 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def test_word_operators_after_predicates
assert_equal([], XPath.match(doc, "/r/v[../v[1] div 3 = 3]"))
end

def test_unary_minus_with_spaces
doc = Document.new("<r><v>6</v><v>2</v></r>")
assert_equal([-6.0], XPath.match(doc, " -6"))
assert_equal([-6.0], XPath.match(doc, "- 6"))
assert_equal([6], XPath.match(doc, "- - 6"))
assert_equal([-6.0], XPath.match(doc, "( -6 )"))
assert_equal([2.0], XPath.match(doc, "( - 6 ) div - 3"))
assert_equal([], XPath.match(doc, "//v[ -1 ]"))
assert_equal([5.0], XPath.match(doc, "//v[1] - - - 1"))
end

def test_node_type
doc = Document.new "<a><?foo bar?><!--comment-->text</a>"
#res = XPath::first(doc.root, "text()")
Expand Down
Loading