Skip to content
This repository
Browse code

Merge pull request #6273 from KensoDev/feature/simple_format_refactoring

Feature/simple format refactoring
  • Loading branch information...
commit 7994496ab4127cf15323563acd55170b9d35821a 2 parents dafc734 + 4bb14b0
Rafael Mendonça França authored May 15, 2012
28  actionpack/lib/action_view/helpers/text_helper.rb
@@ -233,6 +233,7 @@ def word_wrap(text, *args)
233 233
       #
234 234
       # ==== Options
235 235
       # * <tt>:sanitize</tt> - If +false+, does not sanitize +text+.
  236
+      # * <tt>:wrapper_tag</tt> - String representing the tag wrapper, defaults to <tt>"p"</tt>
236 237
       #
237 238
       # ==== Examples
238 239
       #   my_text = "Here is some basic text...\n...with a line break."
@@ -251,16 +252,17 @@ def word_wrap(text, *args)
251 252
       #   simple_format("<span>I'm allowed!</span> It's true.", {}, :sanitize => false)
252 253
       #   # => "<p><span>I'm allowed!</span> It's true.</p>"
253 254
       def simple_format(text, html_options={}, options={})
254  
-        text = '' if text.nil?
255  
-        text = text.dup
256  
-        start_tag = tag('p', html_options, true)
257 255
         text = sanitize(text) unless options[:sanitize] == false
258  
-        text = text.to_str
259  
-        text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
260  
-        text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}")  # 2+ newline  -> paragraph
261  
-        text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline   -> br
262  
-        text.insert 0, start_tag
263  
-        text.html_safe.safe_concat("</p>")
  256
+        wrapper_tag = options.fetch(:wrapper_tag, :p)
  257
+        paragraphs = split_paragraphs(text)
  258
+
  259
+        if paragraphs.empty?
  260
+          content_tag(wrapper_tag, nil, html_options)
  261
+        else
  262
+          paragraphs.map { |paragraph|
  263
+            content_tag(wrapper_tag, paragraph, html_options, options[:sanitize])
  264
+          }.join("\n\n").html_safe
  265
+        end
264 266
       end
265 267
 
266 268
       # Creates a Cycle object whose _to_s_ method cycles through elements of an
@@ -402,6 +404,14 @@ def set_cycle(name, cycle_object)
402 404
           @_cycles = Hash.new unless defined?(@_cycles)
403 405
           @_cycles[name] = cycle_object
404 406
         end
  407
+
  408
+        def split_paragraphs(text)
  409
+          return [] if text.blank?
  410
+
  411
+          text.to_str.gsub(/\r\n?/, "\n").split(/\n\n+/).map! do |t|
  412
+            t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
  413
+          end
  414
+        end
405 415
     end
406 416
   end
407 417
 end
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
46 46
     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)
47 47
   end
48 48
 
  49
+  def test_simple_format_with_custom_wrapper
  50
+    assert_equal "<div></div>", simple_format(nil, {}, :wrapper_tag => "div")
  51
+  end
  52
+
  53
+  def test_simple_format_with_custom_wrapper_and_multi_line_breaks
  54
+    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")
  55
+  end
  56
+
49 57
   def test_simple_format_should_not_change_the_text_passed
50 58
     text = "<b>Ok</b><script>code!</script>"
51 59
     text_clone = text.dup

0 notes on commit 7994496

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