From c792de5b7596589bb87c6c84b8df43ef3f11c61c Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Wed, 2 Dec 2026 16:42:35 +0530 Subject: [PATCH] Accept any whitespace around the XPath operators and, or, div and mod 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. --- lib/rexml/parsers/xpathparser.rb | 6 +++--- test/parser/test_xpath.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index 9ffcb8a8..6f336e06 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -433,7 +433,7 @@ def OrExpr path, parsed n = [] rest = AndExpr( path, n ) if rest != path - while rest =~ /\A\s*( or )/ + while rest =~ /\A\s+(or)\s+/ n = [ :or, n, [] ] rest = AndExpr( $', n[-1] ) end @@ -452,7 +452,7 @@ def AndExpr path, parsed n = [] rest = EqualityExpr( path, n ) if rest != path - while rest =~ /\A\s*( and )/ + while rest =~ /\A\s+(and)\s+/ n = [ :and, n, [] ] rest = EqualityExpr( $', n[-1] ) end @@ -542,7 +542,7 @@ def MultiplicativeExpr path, parsed n = [] rest = UnaryExpr( path, n ) if rest != path - while rest =~ /\A\s*(\*| div | mod )\s*/ + while rest =~ /\A\s*(\*|\s(?:div|mod)\s)\s*/ if $1[0] == ?* n = [ :mult, n, [] ] elsif $1.include?( "div" ) diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb index 9d8ee3fb..07242b3b 100644 --- a/test/parser/test_xpath.rb +++ b/test/parser/test_xpath.rb @@ -237,6 +237,23 @@ def test_qname_must_start_at_the_beginning_of_the_expression assert_equal([:child, :qname, "a", "b"], parser.parse("a:b")) end + def test_word_operators_with_any_whitespace + parser = REXML::Parsers::XPathParser.new + # XPath 1.0 allows a space, tab, CR or LF around the operator names + %w[and or div mod].each do |operator| + expected = parser.parse("a #{operator} b") + ["\t", "\n", "\r\n", " \t "].each do |space| + assert_equal(expected, + parser.parse("a#{space}#{operator}#{space}b"), + "#{operator} separated by #{space.inspect}") + end + end + assert_equal(parser.parse('//a[@b = 1 and @c = 2]'), + parser.parse("//a[@b = 1\nand\t@c = 2]")) + assert_equal(parser.parse('3 div 2 mod 1'), + parser.parse("3\ndiv\n2\tmod\t1")) + end + def test_unary_minus_with_spaces parser = REXML::Parsers::XPathParser.new assert_equal(