mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #6273 from KensoDev/feature/simple_format_refactoring
Feature/simple format refactoring
This commit is contained in:
commit
7994496ab4
2 changed files with 27 additions and 9 deletions
|
@ -233,6 +233,7 @@ module ActionView
|
|||
#
|
||||
# ==== 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."
|
||||
|
@ -251,16 +252,17 @@ module ActionView
|
|||
# simple_format("<span>I'm allowed!</span> It's true.", {}, :sanitize => false)
|
||||
# # => "<p><span>I'm allowed!</span> It's true.</p>"
|
||||
def simple_format(text, html_options={}, options={})
|
||||
text = '' if text.nil?
|
||||
text = text.dup
|
||||
start_tag = tag('p', html_options, true)
|
||||
text = sanitize(text) unless options[:sanitize] == false
|
||||
text = text.to_str
|
||||
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
|
||||
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
|
||||
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
|
||||
text.insert 0, start_tag
|
||||
text.html_safe.safe_concat("</p>")
|
||||
wrapper_tag = options.fetch(:wrapper_tag, :p)
|
||||
paragraphs = split_paragraphs(text)
|
||||
|
||||
if paragraphs.empty?
|
||||
content_tag(wrapper_tag, nil, html_options)
|
||||
else
|
||||
paragraphs.map { |paragraph|
|
||||
content_tag(wrapper_tag, paragraph, html_options, options[:sanitize])
|
||||
}.join("\n\n").html_safe
|
||||
end
|
||||
end
|
||||
|
||||
# Creates a Cycle object whose _to_s_ method cycles through elements of an
|
||||
|
@ -402,6 +404,14 @@ module ActionView
|
|||
@_cycles = Hash.new unless defined?(@_cycles)
|
||||
@_cycles[name] = cycle_object
|
||||
end
|
||||
|
||||
def split_paragraphs(text)
|
||||
return [] if text.blank?
|
||||
|
||||
text.to_str.gsub(/\r\n?/, "\n").split(/\n\n+/).map! do |t|
|
||||
t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -46,6 +46,14 @@ class TextHelperTest < ActionView::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue