Fix: simple_format with blank wrapper_tag option returns plain html tag.

By default `simple_format` method returns the text wrapped with `<p>`. But if we explicitly specify
the `wrapper_tag: nil` in the options, it returns the text wrapped with `<></>` tag.

Before:
```ruby
 simple_format("Hello World", {},  { wrapper_tag: nil })
 # <>Hello World</>
```

After:
```ruby
 simple_format("Hello World", {},  { wrapper_tag: nil })
 # <p>Hello World</p>
```

Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
This commit is contained in:
Akhil G Krishnan 2023-09-02 15:21:16 +00:00 committed by GitHub
parent 2fbb25b771
commit 1677e31240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View File

@ -1,3 +1,24 @@
* Fix `simple_format` with blank `wrapper_tag` option returns plain html tag
By default `simple_format` method returns the text wrapped with `<p>`. But if we explicitly specify
the `wrapper_tag: nil` in the options, it returns the text wrapped with `<></>` tag.
Before:
```ruby
simple_format("Hello World", {}, { wrapper_tag: nil })
# <>Hello World</>
```
After:
```ruby
simple_format("Hello World", {}, { wrapper_tag: nil })
# <p>Hello World</p>
```
*Akhil G Krishnan*, *Junichi Ito*
* Don't double-encode nested `field_id` and `field_name` index values * Don't double-encode nested `field_id` and `field_name` index values
Pass `index: @options` as a default keyword argument to `field_id` and Pass `index: @options` as a default keyword argument to `field_id` and

View File

@ -320,7 +320,7 @@ module ActionView
# simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>", {}, { sanitize_options: { attributes: %w[target href] } }) # simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>", {}, { sanitize_options: { attributes: %w[target href] } })
# # => "<p><a target=\"_blank\" href=\"http://example.com\">Continue</a></p>" # # => "<p><a target=\"_blank\" href=\"http://example.com\">Continue</a></p>"
def simple_format(text, html_options = {}, options = {}) def simple_format(text, html_options = {}, options = {})
wrapper_tag = options.fetch(:wrapper_tag, :p) wrapper_tag = options[:wrapper_tag] || "p"
text = sanitize(text, options.fetch(:sanitize_options, {})) if options.fetch(:sanitize, true) text = sanitize(text, options.fetch(:sanitize_options, {})) if options.fetch(:sanitize, true)
paragraphs = split_paragraphs(text) paragraphs = split_paragraphs(text)

View File

@ -68,6 +68,7 @@ class TextHelperTest < ActionView::TestCase
def test_simple_format_with_custom_wrapper def test_simple_format_with_custom_wrapper
assert_equal "<div></div>", simple_format(nil, {}, { wrapper_tag: "div" }) assert_equal "<div></div>", simple_format(nil, {}, { wrapper_tag: "div" })
assert_equal "<p></p>", simple_format(nil, {}, { wrapper_tag: nil })
end end
def test_simple_format_with_custom_wrapper_and_multi_line_breaks def test_simple_format_with_custom_wrapper_and_multi_line_breaks