Skip to content

Accept any whitespace around the XPath operators and, or, div and mod - #399

Merged
naitoh merged 1 commit into
ruby:masterfrom
mokshupadhyay:fix/xpath-whitespace-between-word-operators
Dec 2, 2026
Merged

naitoh merged 1 commit into
ruby:masterfrom
mokshupadhyay:fix/xpath-whitespace-between-word-operators

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

The XPath parser only recognised the operator names and, or, div and mod when they were surrounded by one literal space, because the patterns were /\A\s*( or )/, /\A\s*( and )/ and /\A\s*(\*| div | mod )\s*/. The grammar comments right above them say S 'or' S, and XPath 1.0 allows any whitespace there (S is a space, tab, CR or LF). So a tab or a line break between the tokens raised ParseException, which is the same family of problem as #397 (line breaks inside expressions) and #393 (spaces).

Real results of REXML::Parsers::XPathParser.new.parse(...) on master with a and b as operands:

expression before after
a and b [:and, [...a], [...b]] same
a\tand\tb ParseException ("Garbage component exists at the end") same tree as a and b
a\nor\nb ParseException same tree as a or b
a\ndiv\nb, a\tmod\tb ParseException same tree as with spaces

To find every affected case I compared the parse tree of the same expression written with different spacing: 10 operand pairs (names, numbers, attributes, paths, function calls, strings, variables, parentheses, predicates, . and ..) × 14 operators × several spacings, 824 comparisons. Before the change 80 of them differed: 72 are exactly this problem (4 word operators × tab or newline × 9 operand pairs). The other 8 (a-b, a- b, $v-1, ...) are correct, because a hyphen belongs to a name in XPath, so I left them alone. After the change only those 8 differ.

The fix replaces the single-space patterns with "one or more whitespace characters" around the names (/\A\s+(or)\s+/, /\A\s+(and)\s+/ and /\A\s*(\*|\s(?:div|mod)\s)\s*/). Everything that parsed before still parses to the same tree: the new patterns accept a superset of the old ones, and I did not change the symbol operators.

Test: test_word_operators_with_any_whitespace in test/parser/test_xpath.rb (the four operators with a tab, LF, CRLF and a mixed separator, plus the same inside a predicate and a div / mod chain). It fails on master (REXML::ParseException: Garbage component exists at the end: <\tand\tb>) and passes now. rake test passes (879 tests, 0 failures, 5 omissions as before). I did not run a linter, since I did not find one configured for this repo.

The parser only matched a single space on each side of these operator names, so a tab or a line break between the tokens, which XPath 1.0 allows, raised ParseException.

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

@naitoh
naitoh merged commit 4ed5ca8 into ruby:master Dec 2, 2026
71 checks passed
@naitoh

naitoh commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thanks!

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.

2 participants