From 5df57c1ff45d202efd6c13b79ce6dd339628a528 Mon Sep 17 00:00:00 2001 From: tompng Date: Fri, 5 Jun 2026 22:05:51 +0900 Subject: [PATCH] Fix XPath normalize-space function It should return normalized string of the first node, just like other functions such as `string()` and `number()` --- lib/rexml/functions.rb | 7 +------ test/functions/test_base.rb | 13 ++++++------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index 60ae34e7..3ad3fe13 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -263,12 +263,7 @@ def Functions::string_length( string ) end def Functions::normalize_space( string=nil ) - string = string(@@context[:node]) if string.nil? - if string.kind_of? Array - string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x} - else - string.to_s.strip.gsub(/\s+/um, ' ') - end + string(string).strip.gsub(/\s+/um, ' ') end # This is entirely Mike Stok's beast diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index daa38156..9e6cab8c 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -242,13 +242,12 @@ def test_normalize_space_strings Dessert \t\t after dinner XML - normalized_texts = REXML::XPath.each(REXML::Document.new(source), "normalize-space(//text())").to_a - assert_equal([ - "breakfast boosts concentration", - "Coffee beans aroma", - "Dessert after dinner", - ], - normalized_texts) + normalized_text = REXML::XPath.each(REXML::Document.new(source), "normalize-space(//text())").to_a + doc = REXML::Document.new(source) + # First node should be used for normalize-space, and the rest should be ignored. + assert_equal(["breakfast boosts concentration"], REXML::XPath.match(doc, "normalize-space(//text())")) + assert_equal(["Coffee beans aroma"], REXML::XPath.match(doc, "normalize-space(//c/text())")) + assert_equal(["Dessert after dinner"], REXML::XPath.match(doc, "normalize-space(//d/text())")) end def test_string_nil_without_context