diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb
index cb2f5c67..ccdc1639 100644
--- a/lib/rexml/parsers/xpathparser.rb
+++ b/lib/rexml/parsers/xpathparser.rb
@@ -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
@@ -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
@@ -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
diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb
index bb20c1fd..ffef7bb6 100644
--- a/test/parser/test_xpath.rb
+++ b/test/parser/test_xpath.rb
@@ -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"))
diff --git a/test/xpath/test_base.rb b/test/xpath/test_base.rb
index 2d022aa4..820e5c45 100644
--- a/test/xpath/test_base.rb
+++ b/test/xpath/test_base.rb
@@ -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("62")
+ 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 "text"
#res = XPath::first(doc.root, "text()")