Skip to content

Fix XPath unary minus parsing with leading or separating spaces - #393

Open
naitoh wants to merge 2 commits into
ruby:masterfrom
naitoh:fix_unary_minus_with_spaces
Open

naitoh wants to merge 2 commits into
ruby:masterfrom
naitoh:fix_unary_minus_with_spaces

Conversation

@naitoh

@naitoh naitoh commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Why?

REXML::Parsers::XPathParser#UnaryExpr did not strip leading spaces before matching - or allow spaces between multiple - operators. In addition, LocationPath returned nil instead of the (empty) rest when the path was empty, and PathExpr returned the lstrip-ed string even when nothing matched.

As a result, AdditiveExpr treated rest != path as a match and misinterpreted unary minus expressions with spaces (such as " -6" or "- - 6") as binary subtractions ([:minus, [], [:literal, 6]] or
[:minus, [:neg], [:literal, 6]]).

Furthermore, after #318 removed the gsub!(/([\(\[])\s+/, '\1') preprocessing in REXML::Parsers::XPathParser#parse, this issue also affected unary minus expressions after ( or [ such as "( -6 )"
and "//v[ -1 ]":

doc = REXML::Document.new("<r><v>6</v><v>2</v></r>")

# Before
REXML::XPath.match(doc, "-6")     # => [-6.0]
REXML::XPath.match(doc, " -6")    # => [56.0] (evaluated as 62 - 6)
REXML::XPath.match(doc, "--6")    # => [6]
REXML::XPath.match(doc, "- - 6")  # => [-68.0] (evaluated as -62 - 6)
REXML::XPath.match(doc, "(-6)")   # => [-6.0]
REXML::XPath.match(doc, "( -6 )") # => [56.0] (evaluated as 62 - 6)

# After
REXML::XPath.match(doc, " -6")    # => [-6.0]
REXML::XPath.match(doc, "- - 6")  # => [6]
REXML::XPath.match(doc, "( -6 )") # => [-6.0]
REXML::XPath.match(doc, "-")      # => REXML::ParseException (was [-62.0])

Changes

  • Strip leading spaces in UnaryExpr and allow spaces after - (/\A((?:-\s*)*)/).
  • Return original_path in UnaryExpr and PathExpr when no expression matches.
  • Make LocationPath return the rest (an empty string) instead of nil when the path is empty.
  • Unary minus without an operand (e.g. "-", " - - ") now raises REXML::ParseException instead of being evaluated to a meaningless value.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the XPath unary-minus whitespace handling change.

No open issues 🎉

What changed in this PR

Fixes whitespace handling in REXML’s XPath unary minus parsing, addressing a regression introduced when #318 removed whitespace preprocessing.

Changes:

  • Accept leading whitespace and spaces after unary minus signs.
  • Preserve the original input when no expression matches, avoiding false binary-subtraction matches.
  • Add parser and evaluation regression tests for spaced unary minus expressions.
File Description
test/​xpath/​test_base.rb Tests evaluation of unary minus with spaces, parentheses, division, and predicates.
test/​parser/​test_xpath.rb Checks that spaced and compact unary minus expressions parse identically.
lib/​rexml/​parsers/​xpathparser.rb Updates unary minus whitespace handling and unmatched-input return behavior.

Review effort: Balanced

@naitoh
naitoh marked this pull request as draft November 30, 2026 12:36
@naitoh
naitoh force-pushed the fix_unary_minus_with_spaces branch from a586cc0 to 5adaac5 Compare November 30, 2026 15:20
@naitoh naitoh changed the title Fix XPath unary minus parsing with leading or trailing spaces Fix XPath unary minus parsing with leading or separating spaces Nov 30, 2026
@naitoh
naitoh force-pushed the fix_unary_minus_with_spaces branch from 5adaac5 to f762d6c Compare November 30, 2026 15:23
Copilot AI balanced review requested due to automatic review settings November 30, 2026 15:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the unary-minus whitespace-handling change.

No open issues 🎉

Review effort: Balanced

`UnaryExpr` did not strip leading spaces before matching `-` or allow
spaces between multiple `-` operators. In addition, `LocationPath`
returned `nil` instead of the (empty) rest when the path was empty, and
`PathExpr` returned the `lstrip`-ed string even when nothing matched.
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 ruby#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.
- Return `original_path` in `UnaryExpr` and `PathExpr` when no
  expression matches.
- Make `LocationPath` return the rest (an empty string) instead of `nil`
  when the path is empty.
- Unary minus without an operand (e.g. `"-"`, `" - - "`) now raises
  `REXML::ParseException` instead of being evaluated to a meaningless
  value.
@naitoh
naitoh force-pushed the fix_unary_minus_with_spaces branch from f762d6c to 84b4e8a Compare November 30, 2026 22:25
Copilot AI balanced review requested due to automatic review settings November 30, 2026 22:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the XPath unary-minus whitespace fix.

No open issues 🎉

Review effort: Balanced

@naitoh
naitoh marked this pull request as ready for review November 30, 2026 22:40
@naitoh
naitoh requested a review from kou November 30, 2026 22:41

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment thread lib/rexml/parsers/xpathparser.rb Outdated
Comment on lines 569 to 571
path =~ /\A((?:-\s*)*)/
negative = $1.count("-").odd?
path = $'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using MatchData for readability?

Suggested change
path =~ /\A((?:-\s*)*)/
negative = $1.count("-").odd?
path = $'
match_data = /\A((?:-\s*)*)/.match(path)
negative = match_data[1].count("-").odd?
path = match_data.post_match

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out.
I've implemented the change in 8ea071d.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 09:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because operand-less unary minus is still accepted in nested expressions, leaving a critical parsing correctness risk.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants