aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-03-24 02:29:30 +0000
committerRick Olson <technoweenie@gmail.com>2008-03-24 02:29:30 +0000
commit5bf4cbbdb74dfaaac258dc93e54dcae8295211e9 (patch)
tree878655113e18bf45e5b8a7e7d1a90716ba56e60c /actionpack
parent0ff031992f1dc36cf36eaf26a1e476f67c29c048 (diff)
downloadrails-5bf4cbbdb74dfaaac258dc93e54dcae8295211e9.tar.gz
rails-5bf4cbbdb74dfaaac258dc93e54dcae8295211e9.tar.bz2
rails-5bf4cbbdb74dfaaac258dc93e54dcae8295211e9.zip
Allow the #simple_format text_helper to take an html_options hash for each paragraph. Closes #2448 [Francois Beausoleil, thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9083 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb18
-rw-r--r--actionpack/test/template/text_helper_test.rb3
3 files changed, 18 insertions, 5 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index bcc258acc2..a1e487c2e1 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+# Allow the #simple_format text_helper to take an html_options hash for each paragraph. #2448 [Francois Beausoleil, thechrisoshow]
+
* Fix regression from filter refactoring where re-adding a skipped filter resulted in it being called twice. [rick]
* Refactor filters to use Active Support callbacks. #11235 [Josh Peek]
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 3d479608f3..f8c3b67474 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -291,6 +291,8 @@ module ActionView
# considered as a linebreak and a <tt><br /></tt> tag is appended. This
# method does not remove the newlines from the +text+.
#
+ # You can pass any HTML attributes into <tt>html_options</tt>. These
+ # will be added to all created paragraphs.
# ==== Examples
# my_text = "Here is some basic text...\n...with a line break."
#
@@ -301,11 +303,17 @@ module ActionView
#
# simple_format(more_text)
# # => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
- def simple_format(text)
- content_tag 'p', text.to_s.
- gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
- gsub(/\n\n+/, "</p>\n\n<p>"). # 2+ newline -> paragraph
- gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
+ #
+ # simple_format("Look ma! A class!", :class => 'description')
+ # # => "<p class='description'>Look ma! A class!</p>"
+ def simple_format(text, html_options={})
+ start_tag = tag('p', html_options, true)
+ text = text.to_s.dup
+ 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 << "</p>"
end
# Turns all URLs and e-mail addresses into clickable links. The +link+ parameter
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index dd5725376f..0cd83334d5 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -24,6 +24,9 @@ class TextHelperTest < Test::Unit::TestCase
text = "A\r\n \nB\n\n\r\n\t\nC\nD".freeze
assert_equal "<p>A\n<br /> \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text)
+
+ assert_equal %q(<p class="test">This is a classy test</p>), simple_format("This is a classy test", :class => 'test')
+ assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), simple_format("para 1\n\npara 2", :class => 'test')
end
def test_truncate