1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add block support to button_tag helper

As per the HTML 4.01 spec:

  Buttons created with the BUTTON element function just like buttons
  created with the INPUT element, but they offer richer rendering
  possibilities: the BUTTON element may have content. For example, a
  BUTTON element that contains an image functions like and may resemble
  an INPUT element whose type is set to "image", but the BUTTON element
  type allows content.

Since rich content is the main purpose of the <button> element, it makes
sense for the button_tag helper to accept a block.

http://www.w3.org/TR/html401/interact/forms.html#edef-BUTTON
http://dev.w3.org/html5/spec/the-button-element.html#the-button-element

Signed-off-by: Santiago Pastorino and Emilio Tagua <santiago+emilioe@wyeworks.com>
This commit is contained in:
Tom Stuart 2011-02-12 12:07:47 +00:00 committed by Santiago Pastorino and Emilio Tagua
parent 03749d6c88
commit 829de9d98e
2 changed files with 18 additions and 4 deletions

View file

@ -414,7 +414,8 @@ module ActionView
# <tt>reset</tt>button or a generic button which can be used in # <tt>reset</tt>button or a generic button which can be used in
# JavaScript, for example. You can use the button tag as a regular # JavaScript, for example. You can use the button tag as a regular
# submit tag but it isn't supported in legacy browsers. However, # submit tag but it isn't supported in legacy browsers. However,
# button tag allows richer labels such as images and emphasis. # the button tag allows richer labels such as images and emphasis,
# so this helper will also accept a block.
# #
# ==== Options # ==== Options
# * <tt>:confirm => 'question?'</tt> - If present, the # * <tt>:confirm => 'question?'</tt> - If present, the
@ -433,7 +434,9 @@ module ActionView
# button_tag # button_tag
# # => <button name="button" type="submit">Button</button> # # => <button name="button" type="submit">Button</button>
# #
# button_tag "<strong>Ask me!</strong>", :type => 'button' # button_tag(:type => 'button') do
# content_tag(:strong, 'Ask me!')
# end
# # => <button name="button" type="button"> # # => <button name="button" type="button">
# <strong>Ask me!</strong> # <strong>Ask me!</strong>
# </button> # </button>
@ -442,7 +445,9 @@ module ActionView
# # => <button data-disable-with="Please wait..." name="button" # # => <button data-disable-with="Please wait..." name="button"
# type="submit">Checkout</button> # type="submit">Checkout</button>
# #
def button_tag(label = "Button", options = {}) def button_tag(content_or_options = nil, options = nil, &block)
options = content_or_options if block_given? && content_or_options.is_a?(Hash)
options ||= {}
options.stringify_keys! options.stringify_keys!
if disable_with = options.delete("disable_with") if disable_with = options.delete("disable_with")
@ -455,7 +460,7 @@ module ActionView
options.reverse_merge! 'name' => 'button', 'type' => 'submit' options.reverse_merge! 'name' => 'button', 'type' => 'submit'
content_tag :button, label, { "type" => options.delete("type") }.update(options) content_tag :button, content_or_options || 'Button', options, &block
end end
# Displays an image which when clicked will submit the form. # Displays an image which when clicked will submit the form.

View file

@ -427,6 +427,15 @@ class FormTagHelperTest < ActionView::TestCase
) )
end end
def test_button_tag_with_block
assert_dom_equal('<button name="button" type="submit">Content</button>', button_tag { 'Content' })
end
def test_button_tag_with_block_and_options
output = button_tag(:name => 'temptation', :type => 'button') { content_tag(:strong, 'Do not press me') }
assert_dom_equal('<button name="temptation" type="button"><strong>Do not press me</strong></button>', output)
end
def test_image_submit_tag_with_confirmation def test_image_submit_tag_with_confirmation
assert_dom_equal( assert_dom_equal(
%(<input type="image" src="/images/save.gif" data-confirm="Are you sure?" />), %(<input type="image" src="/images/save.gif" data-confirm="Are you sure?" />),