Skip to content

Commit

Permalink
xpath: Fix normalize_space(array) case (#111)
Browse files Browse the repository at this point in the history
GitHub: fix GH-110

Fixed a bug in `REXML::Functions.normalize_space(array)` and introduced
test cases for it:

- Corrected a typo in the variable name within the collect block
(`string` -> `x`).
- Added `test_normalize_space_strings` to `test/functions/test_base.rb`.

---------

Co-authored-by: Sutou Kouhei <[email protected]>
  • Loading branch information
flatland001 and kou authored Feb 8, 2024
1 parent 7e4049f commit 444c9ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/rexml/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,10 @@ def Functions::string_length( string )
string(string).length
end

# UNTESTED
def Functions::normalize_space( string=nil )
string = string(@@context[:node]) if string.nil?
if string.kind_of? Array
string.collect{|x| string.to_s.strip.gsub(/\s+/um, ' ') if string}
string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x}
else
string.to_s.strip.gsub(/\s+/um, ' ')
end
Expand Down
22 changes: 22 additions & 0 deletions test/functions/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ def test_normalize_space
assert_equal( [REXML::Comment.new("COMMENT A")], m )
end

def test_normalize_space_strings
source = <<-XML
<a><b>breakfast boosts\t\t
concentration </b><c>
Coffee beans
aroma
</c><d> Dessert
\t\t after dinner</d></a>
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)
end

def test_string_nil_without_context
doc = REXML::Document.new(<<-XML)
<?xml version="1.0" encoding="UTF-8"?>
Expand Down

0 comments on commit 444c9ce

Please sign in to comment.