Skip to content

Commit f762d6c

Browse files
committed
Fix XPath unary minus parsing with leading or separating spaces
`UnaryExpr` did not strip leading spaces before matching `-` or allow spaces between multiple `-` operators, and `PathExpr` / `LocationPath` stripped leading spaces even when they did not match anything. As a result, unary minus expressions with spaces such as `" -6"` or `"- - 6"` were misinterpreted by `AdditiveExpr` as a binary subtraction (`[:minus, [], [:literal, 6]]` or `[:minus, [:neg], [:literal, 6]]`). After #318 removed the `gsub!(/([\(\[])\s+/, '\1')` preprocessing in `parse`, this also affected expressions after `(` or `[` such as `"( -6 )"` and `"//v[ -1 ]"`. - Strip leading spaces in `UnaryExpr` and allow spaces between multiple `-` operators. - Restore `original_path` in `UnaryExpr`, `PathExpr`, and `LocationPath` when no expression matches.
1 parent b777bdf commit f762d6c

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎lib/rexml/parsers/xpathparser.rb‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def quote_literal( literal )
254254
# | '/' RelativeLocationPath?
255255
# | '//' RelativeLocationPath
256256
def LocationPath path, parsed
257+
original_path = path
257258
path = path.lstrip
258259
if path[0] == ?/
259260
parsed << :document
@@ -264,8 +265,12 @@ def LocationPath path, parsed
264265
else
265266
path = path[1..-1]
266267
end
268+
path = RelativeLocationPath( path, parsed ) if path.size > 0
269+
path
270+
else
271+
return original_path if path.empty?
272+
RelativeLocationPath( path, parsed )
267273
end
268-
RelativeLocationPath( path, parsed ) if path.size > 0
269274
end
270275

271276
#RelativeLocationPath
@@ -564,19 +569,25 @@ def MultiplicativeExpr path, parsed
564569
#| '-' UnaryExpr
565570
#| UnionExpr
566571
def UnaryExpr path, parsed
567-
path =~ /^(\-*)/
572+
original_path = path
573+
path = path.lstrip
574+
path =~ /^((?:-\s*)*)/
568575
path = $'
569-
if $1 and (($1.size % 2) != 0)
576+
if $1 and (($1.count("-") % 2) != 0)
570577
mult = -1
571578
else
572579
mult = 1
573580
end
574581
parsed << :neg if mult < 0
575582

576583
n = []
577-
path = UnionExpr( path, n )
584+
rest = UnionExpr( path, n )
585+
if rest == path
586+
parsed.pop if mult < 0
587+
return original_path
588+
end
578589
parsed.concat( n )
579-
path
590+
rest
580591
end
581592

582593
#| UnionExpr '|' PathExpr
@@ -601,11 +612,13 @@ def UnionExpr path, parsed
601612
#| LocationPath
602613
#| FilterExpr ('/' | '//') RelativeLocationPath
603614
def PathExpr path, parsed
615+
original_path = path
604616
path = path.lstrip
605617
n = []
606618
rest = FilterExpr( path, n )
607619
if rest == path
608620
rest = LocationPath(rest, n)
621+
return original_path if rest == path
609622
else
610623
if /\A\s*\//.match?(rest)
611624
rest = rest.lstrip

‎test/parser/test_xpath.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ def test_spaces_between_tokens
180180
)
181181
end
182182

183+
def test_unary_minus_with_spaces
184+
parser = REXML::Parsers::XPathParser.new
185+
assert_equal(
186+
parser.parse('(-6) div -3'),
187+
parser.parse(' ( - 6 ) div - 3 '),
188+
)
189+
assert_equal(
190+
parser.parse('--6'),
191+
parser.parse(' - - 6 '),
192+
)
193+
end
194+
183195
def test_mult_asterisk_without_surrounding_spaces
184196
parser = REXML::Parsers::XPathParser.new
185197
assert_equal(parser.parse("1 * 2 * 3"), parser.parse("1*2*3"))

‎test/xpath/test_base.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ def test_word_operators_after_predicates
222222
assert_equal([], XPath.match(doc, "/r/v[../v[1] div 3 = 3]"))
223223
end
224224

225+
def test_unary_minus_with_spaces
226+
doc = Document.new("<r><v>6</v><v>2</v></r>")
227+
assert_equal([-6.0], XPath.match(doc, " -6"))
228+
assert_equal([-6.0], XPath.match(doc, "- 6"))
229+
assert_equal([6], XPath.match(doc, "- - 6"))
230+
assert_equal([-6.0], XPath.match(doc, "( -6 )"))
231+
assert_equal([2.0], XPath.match(doc, "( - 6 ) div - 3"))
232+
assert_equal([], XPath.match(doc, "//v[ -1 ]"))
233+
end
234+
225235
def test_node_type
226236
doc = Document.new "<a><?foo bar?><!--comment-->text</a>"
227237
#res = XPath::first(doc.root, "text()")

0 commit comments

Comments
 (0)