Skip to content
Browse files

Added the wrapper tag option to simple_format

  • Loading branch information...
1 parent 09fcac9 commit 4bb14b0daafb9b1c3d0d5c041703c9242b27adc6 @KensoDev KensoDev committed May 14, 2012
Showing with 12 additions and 2 deletions.
  1. +4 −2 actionpack/lib/action_view/helpers/text_helper.rb
  2. +8 −0 actionpack/test/template/text_helper_test.rb
View
6 actionpack/lib/action_view/helpers/text_helper.rb
@@ -241,6 +241,7 @@ def word_wrap(text, *args)
#
# ==== Options
# * <tt>:sanitize</tt> - If +false+, does not sanitize +text+.
+ # * <tt>:wrapper_tag</tt> - String representing the tag wrapper, defaults to <tt>"p"</tt>
#
# ==== Examples
# my_text = "Here is some basic text...\n...with a line break."
@@ -260,13 +261,14 @@ def word_wrap(text, *args)
# # => "<p><span>I'm allowed!</span> It's true.</p>"
def simple_format(text, html_options={}, options={})
text = sanitize(text) unless options[:sanitize] == false
+ wrapper_tag = options.fetch(:wrapper_tag, :p)
paragraphs = split_paragraphs(text)
if paragraphs.empty?
- content_tag('p', nil, html_options)
+ content_tag(wrapper_tag, nil, html_options)
else
paragraphs.map { |paragraph|
- content_tag('p', paragraph, html_options, options[:sanitize])
+ content_tag(wrapper_tag, paragraph, html_options, options[:sanitize])
}.join("\n\n").html_safe
end
end
View
8 actionpack/test/template/text_helper_test.rb
@@ -46,6 +46,14 @@ def test_simple_format_should_not_sanitize_input_when_sanitize_option_is_false
assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :sanitize => false)
end
+ def test_simple_format_with_custom_wrapper
+ assert_equal "<div></div>", simple_format(nil, {}, :wrapper_tag => "div")
+ end
+
+ def test_simple_format_with_custom_wrapper_and_multi_line_breaks
+ assert_equal "<div>We want to put a wrapper...</div>\n\n<div>...right there.</div>", simple_format("We want to put a wrapper...\n\n...right there.", {}, :wrapper_tag => "div")
+ end
+
def test_simple_format_should_not_change_the_text_passed
text = "<b>Ok</b><script>code!</script>"
text_clone = text.dup

0 comments on commit 4bb14b0

Please sign in to comment.
Something went wrong with that request. Please try again.