Skip to content

Commit 2191f20

Browse files
Fix word_wrap with empty string
Follow-up to rails#45948. This fixes `word_wrap` to return an empty string instead of `nil` when given an empty string. Fixes rails#50067.
1 parent d424cab commit 2191f20

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

actionview/lib/action_view/helpers/text_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18
266266
# word_wrap('Once upon a time', line_width: 1, break_sequence: "\r\n")
267267
# # => Once\r\nupon\r\na\r\ntime
268268
def word_wrap(text, line_width: 80, break_sequence: "\n")
269+
return +"" if text.empty?
270+
269271
# Match up to `line_width` characters, followed by one of
270272
# (1) non-newline whitespace plus an optional newline
271273
# (2) the end of the string, ignoring any trailing newlines

actionview/test/template/text_helper_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ def test_excerpt_with_separator
393393
assert_equal " 1-+1-+ 1-+1", word_wrap(input, line_width: 3, break_sequence: "-+")
394394
end
395395

396+
test "word_wrap when no wrapping is necessary" do
397+
assert_equal "1", word_wrap("1", line_width: 3)
398+
assert_equal "", word_wrap("", line_width: 3)
399+
end
400+
396401
def test_pluralization
397402
assert_equal("1 count", pluralize(1, "count"))
398403
assert_equal("2 counts", pluralize(2, "count"))

0 commit comments

Comments
 (0)