Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2448: simple-format-html-options.patch

File simple-format-html-options.patch, 2.3 kB (added by François Beausoleil <fbeausoleil@ftml.net>, 3 years ago)

Add html_options to #simple_format (tests & implementation)

  • actionpack/test/template/text_helper_test.rb

    old new  
    268268    assert_equal(%w{Specialized Fuji Giant}, @cycles) 
    269269  end 
    270270 
     271  def test_simple_format_one_para_with_html_options 
     272    assert_equal %q(<p class="test">this is the text</p>), 
     273        simple_format("this is the text", :class => 'test') 
     274  end 
     275 
     276  def test_simple_format_two_paras_with_html_options 
     277    assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), 
     278        simple_format("para 1\n\npara 2", :class => 'test') 
     279  end 
    271280end 
  • actionpack/lib/action_view/helpers/text_helper.rb

    old new  
    118118      # Returns +text+ transformed into HTML using very simple formatting rules 
    119119      # Surrounds paragraphs with <tt>&lt;p&gt;</tt> tags, and converts line breaks into <tt>&lt;br /&gt;</tt> 
    120120      # Two consecutive newlines(<tt>\n\n</tt>) are considered as a paragraph, one newline (<tt>\n</tt>) is 
    121       # considered a linebreak, three or more consecutive newlines are turned into two newlines  
    122       def simple_format(text) 
     121      # considered a linebreak, three or more consecutive newlines are turned into two newlines 
     122      # 
     123      # You can pass any HTML attributes into <var>html_options</var>.  These 
     124      # will be added to all created paragraphs. 
     125      def simple_format(text, html_options={}) 
    123126        text.gsub!(/(\r\n|\n|\r)/, "\n") # lets make them newlines crossplatform 
    124127        text.gsub!(/\n\n+/, "\n\n") # zap dupes 
    125         text.gsub!(/\n\n/, '</p>\0<p>') # turn two newlines into paragraph 
     128        text.gsub!(/\n\n/, "</p>\\0#{tag('p', html_options, true)}") # turn two newlines into paragraph 
    126129        text.gsub!(/([^\n])(\n)([^\n])/, '\1\2<br />\3') # turn single newline into <br /> 
    127130         
    128         content_tag("p", text
     131        content_tag("p", text, html_options
    129132      end 
    130133 
    131134      # Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.