Repository navigation
Conversation
There was a problem hiding this comment.
🟢 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
marked this pull request as draft
November 30, 2026 12:36
naitoh
force-pushed
the
fix_unary_minus_with_spaces
branch
from
November 30, 2026 15:20
a586cc0 to
5adaac5
Compare
naitoh
force-pushed
the
fix_unary_minus_with_spaces
branch
from
November 30, 2026 15:23
5adaac5 to
f762d6c
Compare
`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
force-pushed
the
fix_unary_minus_with_spaces
branch
from
November 30, 2026 22:25
f762d6c to
84b4e8a
Compare
naitoh
marked this pull request as ready for review
November 30, 2026 22:40
kou
approved these changes
Dec 1, 2026
Comment on lines
569
to
571
| path =~ /\A((?:-\s*)*)/ | ||
| negative = $1.count("-").odd? | ||
| path = $' |
Member
There was a problem hiding this comment.
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 |
Contributor
Author
There was a problem hiding this comment.
Thank you for pointing that out.
I've implemented the change in 8ea071d.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
REXML::Parsers::XPathParser#UnaryExprdid not strip leading spaces before matching-or allow spaces between multiple-operators. In addition,LocationPathreturnednilinstead of the (empty) rest when the path was empty, andPathExprreturned thelstrip-ed string even when nothing matched.As a result,
AdditiveExprtreatedrest != pathas 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 inREXML::Parsers::XPathParser#parse, this issue also affected unary minus expressions after(or[such as"( -6 )"and
"//v[ -1 ]":Changes
UnaryExprand allow spaces after-(/\A((?:-\s*)*)/).original_pathinUnaryExprandPathExprwhen no expression matches.LocationPathreturn the rest (an empty string) instead ofnilwhen the path is empty."-"," - - ") now raisesREXML::ParseExceptioninstead of being evaluated to a meaningless value.