Repository navigation
Fix XPath parsing of expressions and literals that contain line breaks - #397
mokshupadhyay wants to merge 3 commits 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.
…-token-anchors # Conflicts: # test/parser/test_xpath.rb
|
Hi @naitoh, a friendly ping: this is ready for review whenever you have time. I merged the latest master into the branch to fix a conflict in the XPath tests (kept both sets of tests; the full suite passes). Happy to adjust anything. Thanks! |
|
Thanks for the PR! One remaining case: Lines 13 to 14 in f073640 Because of this, invalid multiline expressions like Could you also update |
QNAME in the XPath parser uses NAMESPLIT, and with ^ a name on a later line matched, so invalid expressions such as "//a/1\nb" were parsed with the first line silently dropped instead of raising ParseException.
|
Thanks for spotting that. I changed Before: |
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.