Skip to content

Commit

Permalink
Merge pull request #34488 from mullican/word_wrap_preserve_left_side_…
Browse files Browse the repository at this point in the history
…whitespace

Prevent TextHelper#word_wrap from stripping white space on the left side of long lines; Fixes #34487
  • Loading branch information
rafaelfranca committed Nov 19, 2018
2 parents 53478c3 + 4fdc626 commit 0bbe340
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
* Prevent `ActionView::TextHelper#word_wrap` from unexpectedly stripping white space from the _left_ side of lines.

For example, given input like this:

```
This is a paragraph with an initial indent,
followed by additional lines that are not indented,
and finally terminated with a blockquote:
"A pithy saying"
```

Calling `word_wrap` should not trim the indents on the first and last lines.

Fixes #34487

*Lyle Mullican*


* Add allocations to template rendering instrumentation.

Adds the allocations for template and partial rendering to the server output on render.
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/text_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18
# # => 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}").strip : line
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1#{break_sequence}").rstrip : line
end * break_sequence
end

Expand Down
4 changes: 4 additions & 0 deletions actionview/test/template/text_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ def test_word_wrap_with_extra_newlines
assert_equal("my very very\nvery long\nstring\n\nwith another\nline", word_wrap("my very very very long string\n\nwith another line", line_width: 15))
end

def test_word_wrap_with_leading_spaces
assert_equal(" This is a paragraph\nthat includes some\nindented lines:\n Like this sample\n blockquote", word_wrap(" This is a paragraph that includes some\nindented lines:\n Like this sample\n blockquote", line_width: 25))
end

def test_word_wrap_does_not_modify_the_options_hash
options = { line_width: 15 }
passed_options = options.dup
Expand Down

0 comments on commit 0bbe340

Please sign in to comment.