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 3 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
19 changes: 19 additions & 0 deletions test/functions/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ def test_normalize_space
assert_equal( [REXML::Comment.new("COMMENT A")], m )
end

def test_normalize_space2
flatland001 marked this conversation as resolved.
Show resolved Hide resolved
source1 = "<a><b>breakfast boosts concentration</b><c>Coffee beans aroma</c><d>Dessert after dinner</d></a>"
source2 = <<-XML
<a><b>breakfast boosts\t\t

concentration </b><c>
Coffee beans
aroma



</c><d> Dessert
\t\t after dinner</d></a>
XML
ret1 = REXML::XPath.each(REXML::Document.new(source1), "//text()").to_a
ret2 = REXML::XPath.each(REXML::Document.new(source2), "normalize-space(//text())").to_a
assert_equal( ret1, ret2 )
flatland001 marked this conversation as resolved.
Show resolved Hide resolved
end

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