Skip to content

Commit 6bdcd64

Browse files
committed
Fix XPath normalize-space function
It should return normalized string of the first node, just like other functions such as `string()` and `number()`
1 parent 8797f17 commit 6bdcd64

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

lib/rexml/functions.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,8 @@ def Functions::string_length( string )
262262
string(string).length
263263
end
264264

265-
def Functions::normalize_space( string=nil )
266-
string = string(@@context[:node]) if string.nil?
267-
if string.kind_of? Array
268-
string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x}
269-
else
270-
string.to_s.strip.gsub(/\s+/um, ' ')
271-
end
265+
def Functions::normalize_space( object=@@context[:node] )
266+
string(object).strip.gsub(/\s+/um, ' ')
272267
end
273268

274269
# This is entirely Mike Stok's beast

test/functions/test_base.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,18 @@ def test_normalize_space_strings
242242
</c><d> Dessert
243243
\t\t after dinner</d></a>
244244
XML
245-
normalized_texts = REXML::XPath.each(REXML::Document.new(source), "normalize-space(//text())").to_a
246-
assert_equal([
247-
"breakfast boosts concentration",
248-
"Coffee beans aroma",
249-
"Dessert after dinner",
250-
],
251-
normalized_texts)
245+
doc = REXML::Document.new(source)
246+
# First node should be used for normalize-space, and the rest should be ignored.
247+
assert_equal(["breakfast boosts concentration"], REXML::XPath.match(doc, "normalize-space(//text())"))
248+
assert_equal(["Coffee beans aroma"], REXML::XPath.match(doc, "normalize-space(//c/text())"))
249+
assert_equal(["Dessert after dinner"], REXML::XPath.match(doc, "normalize-space(//d/text())"))
250+
end
251+
252+
def test_normalize_space_without_argument
253+
source = "<root><item> foo </item><item> bar </item><item> baz </item></root>"
254+
doc = REXML::Document.new(source)
255+
match = REXML::XPath.match(doc, "//item[normalize-space()='bar']")
256+
assert_equal([" bar "], match.map(&:text))
252257
end
253258

254259
def test_string_nil_without_context

0 commit comments

Comments
 (0)