1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Chomp the break_sequence in word_wrap

When word_wrap's break_sequence contains non-rstrippable
characters, word_wrap fails to strip the extra break_sequence at the
end properly. Using chomp! helps cut exactly what was specified in the
argument.
This commit is contained in:
Max Chernyak 2022-09-04 13:44:00 -04:00
parent 9ff7c9c023
commit fe554a3daf
3 changed files with 37 additions and 1 deletions

View file

@ -1,3 +1,35 @@
* Strip `break_sequence` at the end of `word_wrap`.
This fixes a bug where `word_wrap` didn't properly strip off break sequences that had printable characters.
For example, compare the outputs of this template:
```erb
# <%= word_wrap("11 22\n33 44", line_width: 2, break_sequence: "\n# ") %>
```
Before:
```
# 11
# 22
#
# 33
# 44
#
```
After:
```
# 11
# 22
# 33
# 44
```
*Max Chernyak*
* Allow templates to set strict `locals`.
By default, templates will accept any `locals` as keyword arguments. To define what `locals` a template accepts, add a `locals` magic comment:

View file

@ -263,7 +263,7 @@ module ActionView
# # => Once\r\nupon\r\na\r\ntime
def word_wrap(text, line_width: 80, break_sequence: "\n")
text.split("\n").collect! do |line|
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1#{break_sequence}").rstrip : line
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1#{break_sequence}").chomp!(break_sequence) : line
end * break_sequence
end

View file

@ -376,6 +376,10 @@ class TextHelperTest < ActionView::TestCase
assert_equal("1234567890\r\n1234567890\r\n1234567890", word_wrap("1234567890 " * 3, line_width: 2, break_sequence: "\r\n"))
end
def test_word_wrap_with_non_strippable_break_sequence
assert_equal("11\n# 22\n# 33", word_wrap("11 22 33", line_width: 2, break_sequence: "\n# "))
end
def test_pluralization
assert_equal("1 count", pluralize(1, "count"))
assert_equal("2 counts", pluralize(2, "count"))