From d7a8bbcd8f00e602bca0b7c399403b95a59790f9 Mon Sep 17 00:00:00 2001 From: FFspiky Date: Wed, 25 Nov 2026 00:50:29 +0800 Subject: [PATCH 1/2] Preserve XPath word operators following predicates --- lib/rexml/parsers/xpathparser.rb | 3 ++- test/xpath/test_base.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index faf373c8..cb2f5c67 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -411,7 +411,8 @@ def Predicate path, parsed while path[0] == ?[ path, expr = get_group(path) predicates << expr[1..-2] if expr - path = path.lstrip + stripped_path = path.lstrip + path = stripped_path if stripped_path.start_with?("[") end predicates.each{ |pred| preds = [] diff --git a/test/xpath/test_base.rb b/test/xpath/test_base.rb index f0f53219..1e7aeea0 100644 --- a/test/xpath/test_base.rb +++ b/test/xpath/test_base.rb @@ -209,6 +209,32 @@ def test_predicates assert_equal "b", XPath::first( @@doc, "//b[@x]" ).name end + sub_test_case("word operators after predicates") do + data("and" => ["//v[1] and true()", [true]], + "or" => ["//missing[1] or true()", [true]], + "div" => ["/r/v[1] div 3", [2.0]], + "mod" => ["/r/v[1] mod 4", [2.0]], + "and after consecutive predicates" => + ["//v[position() <= 2] [1] and true()", [true]], + "or after consecutive predicates" => + ["//missing[1] [1] or true()", [true]], + "div after consecutive predicates" => + ["/r/v[position() <= 2] [1] div 3", [2.0]], + "mod after consecutive predicates" => + ["/r/v[position() <= 2] [1] mod 4", [2.0]]) + def test_word_operator(data) + xpath, expected = data + document = Document.new("62") + assert_equal(expected, XPath.match(document, xpath)) + end + + def test_word_operator_in_nested_predicate + document = Document.new("62") + assert_equal([], + XPath.match(document, "/r/v[../v[1] div 3 = 3]")) + end + end + def test_node_type doc = Document.new "text" #res = XPath::first(doc.root, "text()") From c459c40bca52b72cc7264a5e80e641cc71e8d1b5 Mon Sep 17 00:00:00 2001 From: FFspiky Date: Sun, 29 Nov 2026 13:25:56 +0800 Subject: [PATCH 2/2] Simplify XPath word operator regression tests --- test/xpath/test_base.rb | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/test/xpath/test_base.rb b/test/xpath/test_base.rb index 1e7aeea0..a7d712fb 100644 --- a/test/xpath/test_base.rb +++ b/test/xpath/test_base.rb @@ -209,30 +209,17 @@ def test_predicates assert_equal "b", XPath::first( @@doc, "//b[@x]" ).name end - sub_test_case("word operators after predicates") do - data("and" => ["//v[1] and true()", [true]], - "or" => ["//missing[1] or true()", [true]], - "div" => ["/r/v[1] div 3", [2.0]], - "mod" => ["/r/v[1] mod 4", [2.0]], - "and after consecutive predicates" => - ["//v[position() <= 2] [1] and true()", [true]], - "or after consecutive predicates" => - ["//missing[1] [1] or true()", [true]], - "div after consecutive predicates" => - ["/r/v[position() <= 2] [1] div 3", [2.0]], - "mod after consecutive predicates" => - ["/r/v[position() <= 2] [1] mod 4", [2.0]]) - def test_word_operator(data) - xpath, expected = data - document = Document.new("62") - assert_equal(expected, XPath.match(document, xpath)) - end - - def test_word_operator_in_nested_predicate - document = Document.new("62") - assert_equal([], - XPath.match(document, "/r/v[../v[1] div 3 = 3]")) - end + def test_word_operators_after_predicates + doc = Document.new("62") + assert_equal([true], XPath.match(doc, "//v[1] and true()")) + assert_equal([true], XPath.match(doc, "//missing[1] or true()")) + assert_equal([2.0], XPath.match(doc, "/r/v[1] div 3")) + assert_equal([2.0], XPath.match(doc, "/r/v[1] mod 4")) + assert_equal([true], XPath.match(doc, "//v[position() <= 2] [1] and true()")) + assert_equal([true], XPath.match(doc, "//missing[1] [1] or true()")) + assert_equal([2.0], XPath.match(doc, "/r/v[position() <= 2] [1] div 3")) + assert_equal([2.0], XPath.match(doc, "/r/v[position() <= 2] [1] mod 4")) + assert_equal([], XPath.match(doc, "/r/v[../v[1] div 3 = 3]")) end def test_node_type