Repository navigation
Fix XPath parsing of expressions and literals that contain line breaks - #397
Open
mokshupadhyay wants to merge 1 commit into
Open
mokshupadhyay wants to merge 1 commit into
mokshupadhyay wants to merge 1 commit into
Conversation
The XPath parser matched its tokens with `^`, which in Ruby matches at
the start of every line, not only at the start of the string. When the
remaining expression did not start with the token being tried, the
pattern could match at the start of a later line and everything before
it was dropped.
As a result an expression such as "//a[\ncount(b) > 1\n]" raised
ParseException, and a string literal with a line break was cut, e.g.
string("a\n1") returned "1".
Use `\A` so that the patterns only match at the current position.
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.
The XPath parser anchors its token patterns with
^. In Ruby^matches at the start of every line, not only at the start of the string, so when the rest of the expression does not start with the token being tried, the pattern can match at the start of a later line and everything before it is dropped. An XPath expression written over several lines then raisesParseExceptionor parses to something else, and a string literal that contains a line break is cut.XPath 1.0 allows whitespace, including line breaks, between tokens (section 3.7,
ExprWhitespace), and aLiteralis'"' [^"]* '"', so it may contain a line break.This changes
^to\Ain the token patterns oflib/rexml/parsers/xpathparser.rb(LITERAL,AXIS,PREFIX_WILDCARD,LOCAL_NAME_WILDCARD,NODE_TYPE,PI,VARIABLE_REFERENCE,NUMBER, the operator loops, the function-call pattern and the two single-character patterns). I left the^inUnaryExpralone because #393 rewrites that line.Tests:
test_line_breaks_between_tokensandtest_line_breaks_in_literalintest/parser/test_xpath.rb. Both error onmasterand pass now.bundle exec rake testpasses (874 tests, 2774 assertions, 0 failures, 0 errors, 5 omissions) on Ruby 3.4.5.Not changed here: operator names still need literal spaces around them (
" and "," or "," div "," mod "), so@x=1 and\n@x=2with a line break right afterandis a separate problem.