Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xpath: Fix normalize_space(array) case #111

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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