mirror of
https://github.com/github/rails.git
synced 2026-02-07 12:44:54 -05:00
Added block-usage to TagHelper#content_tag [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5344 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
*SVN*
|
||||
|
||||
* Added block-usage to TagHelper#content_tag [DHH]. Example:
|
||||
|
||||
<% content_tag :div, :class => "strong" %>
|
||||
Hello world!
|
||||
<% end %>
|
||||
|
||||
Will output:
|
||||
<div class="strong"><p>Hello world!</p></div>
|
||||
|
||||
* Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => true #6409 [BobSilva]
|
||||
|
||||
* Upgraded NumberHelper with number_to_phone support international formats to comply with ITU E.123 by supporting area codes with less than 3 digits, added precision argument to number_to_human_size (defaults to 1) #6421 [BobSilva]
|
||||
|
||||
@@ -15,11 +15,26 @@ module ActionView
|
||||
end
|
||||
|
||||
# Examples:
|
||||
# * <tt>content_tag("p", "Hello world!") => <p>Hello world!</p></tt>
|
||||
# * <tt>content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => </tt>
|
||||
# * <tt>content_tag(:p, "Hello world!") => <p>Hello world!</p></tt>
|
||||
# * <tt>content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong") => </tt>
|
||||
# <tt><div class="strong"><p>Hello world!</p></div></tt>
|
||||
def content_tag(name, content, options = nil)
|
||||
"<#{name}#{tag_options(options.stringify_keys) if options}>#{content}</#{name}>"
|
||||
#
|
||||
# ERb example:
|
||||
# <% content_tag :div, :class => "strong" %>
|
||||
# Hello world!
|
||||
# <% end %>
|
||||
#
|
||||
# Will output:
|
||||
# <div class="strong"><p>Hello world!</p></div>
|
||||
def content_tag(name, content_or_options_with_block = nil, options = nil, &block)
|
||||
if block_given?
|
||||
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
|
||||
content = capture(&block)
|
||||
concat(content_tag_string(name, content, options), block.binding)
|
||||
else
|
||||
content = content_or_options_with_block
|
||||
content_tag_string(name, content, options)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a CDATA section for the given +content+. CDATA sections
|
||||
@@ -41,6 +56,11 @@ module ActionView
|
||||
end
|
||||
|
||||
private
|
||||
def content_tag_string(name, content, options)
|
||||
tag_options = options ? tag_options(options.stringify_keys) : ""
|
||||
"<#{name}#{tag_options}>#{content}</#{name}>"
|
||||
end
|
||||
|
||||
def tag_options(options)
|
||||
cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?})
|
||||
' ' + cleaned_options.map {|key, value| %(#{key}="#{escape_once(value)}")}.sort * ' ' unless cleaned_options.empty?
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
require File.dirname(__FILE__) + '/../abstract_unit'
|
||||
|
||||
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
|
||||
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper'
|
||||
|
||||
class TagHelperTest < Test::Unit::TestCase
|
||||
include ActionView::Helpers::TagHelper
|
||||
include ActionView::Helpers::UrlHelper
|
||||
include ActionView::Helpers::TextHelper
|
||||
include ActionView::Helpers::CaptureHelper
|
||||
|
||||
def test_tag
|
||||
assert_equal "<p class=\"show\" />", tag("p", "class" => "show")
|
||||
@@ -35,6 +34,18 @@ class TagHelperTest < Test::Unit::TestCase
|
||||
content_tag("a", "Create", :href => "create")
|
||||
end
|
||||
|
||||
def test_content_tag_with_block
|
||||
_erbout = ''
|
||||
content_tag(:div) { _erbout.concat "Hello world!" }
|
||||
assert_dom_equal "<div>Hello world!</div>", _erbout
|
||||
end
|
||||
|
||||
def test_content_tag_with_block_and_options
|
||||
_erbout = ''
|
||||
content_tag(:div, :class => "green") { _erbout.concat "Hello world!" }
|
||||
assert_dom_equal %(<div class="green">Hello world!</div>), _erbout
|
||||
end
|
||||
|
||||
def test_cdata_section
|
||||
assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user