Fix xpath parsing xpath that has string literal containing parentheses and brackets#318
Open
tompng wants to merge 1 commit into
Open
Fix xpath parsing xpath that has string literal containing parentheses and brackets#318tompng wants to merge 1 commit into
tompng wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves XPath parsing robustness around whitespace handling, especially when spaces appear inside string literals, and adds regression tests to cover these cases.
Changes:
- Add tests validating that extra whitespace in XPath expressions is treated as ignorable where appropriate.
- Update
XPathParser#parseto strip ignorable spaces without altering whitespace inside quoted string literals. - Update group parsing to ignore parentheses/brackets that appear inside quoted string literals.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/xpath/test_base.rb | Adds test coverage for whitespace normalization and for bracket/paren characters inside XPath string literals. |
| lib/rexml/parsers/xpathparser.rb | Refactors space-stripping into a helper that respects string literals; improves grouping logic to ignore delimiters inside quotes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+34
to
+46
| def strip_ignorable_spaces(path) | ||
| # Safely do `path.gsub(/([\(\[])\s+/, '\1').gsub( /\s+([\]\)])/, '\1')` | ||
| # without modifying spaces inside string literals. | ||
| quote = nil | ||
| path.gsub(/([\(\[])\s+|\s+([\]\)])|(['"])/) do | ||
| if quote | ||
| quote = nil if $3 == quote | ||
| $& | ||
| else | ||
| $1 || $2 || (quote = $3) | ||
| end | ||
| end | ||
| end |
Comment on lines
+695
to
+706
| if quote | ||
| # ignore () [] inside quotes | ||
| quote = nil if string[ind] == quote | ||
| else | ||
| case string[ind] | ||
| when st | ||
| depth += 1 | ||
| when en | ||
| depth -= 1 | ||
| when '"', "'" | ||
| quote = string[ind] | ||
| end |
Comment on lines
+38
to
+44
| path.gsub(/([\(\[])\s+|\s+([\]\)])|(['"])/) do | ||
| if quote | ||
| quote = nil if $3 == quote | ||
| $& | ||
| else | ||
| $1 || $2 || (quote = $3) | ||
| end |
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.
String literal in XPath 1.0 doesn't have escapes.
begin end while condis not a normal modifier while, but a do-while loop. (I didn't know it). REXML uses this syntax in several files.I don't think preprocess like
strip_ignorable_spacesis a good idea. This is just a first aid.I found that REXML can't parse
//a[1] [2]. I think it could be fixed by changingstrip_ignoreable_spaces, but leaved as, it's a separate issue, and I think it should be fixed elsewhere.