Skip to content

Commit

Permalink
javascript_tag should only concat when block_given?
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 7, 2008
1 parent fec0f06 commit e732a40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions actionpack/lib/action_view/helpers/javascript_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,14 +172,17 @@ def escape_javascript(javascript)
# alert('All is good') # alert('All is good')
# <% end -%> # <% end -%>
def javascript_tag(content_or_options_with_block = nil, html_options = {}, &block) def javascript_tag(content_or_options_with_block = nil, html_options = {}, &block)
if block_given? content =
html_options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) if block_given?
content = capture(&block) html_options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
else capture(&block)
content = content_or_options_with_block else
end content_or_options_with_block
end

tag = content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS))


concat(content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS))) block_given? ? concat(tag) : tag
end end


def javascript_cdata_section(content) #:nodoc: def javascript_cdata_section(content) #:nodoc:
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/template/javascript_helper_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ def test_button_to_function_without_function
end end


def test_javascript_tag def test_javascript_tag
@output_buffer = 'foo'

assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
javascript_tag("alert('hello')") javascript_tag("alert('hello')")

assert_equal 'foo', @output_buffer, 'javascript_tag without a block should not concat to @output_buffer'
end end


def test_javascript_tag_with_options def test_javascript_tag_with_options
Expand Down

0 comments on commit e732a40

Please sign in to comment.