Skip to content

Commit

Permalink
Added an html_options hash parameter to javascript_tag() and update_p…
Browse files Browse the repository at this point in the history
…age_tag() helpers #6311 [tzaharia]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5245 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Oct 9, 2006
1 parent ca35e26 commit 8ff92e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
10 changes: 10 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,15 @@
*SVN*

* Added an html_options hash parameter to javascript_tag() and update_page_tag() helpers #6311 [tzaharia]. Example:

update_page_tag :defer => 'true' { |page| ... }

Gives:

<script defer="true" type="text/javascript">...</script>

Which is needed for dealing with the IE6 DOM when it's not yet fully loaded.

* Fixed that rescue template path shouldn't be hardcoded, then it's easier to hook in your own #6295 [mnaberez]

* Fixed escaping of backslashes in JavaScriptHelper#escape_javascript #6302 [sven@c3d2.de]
Expand Down
7 changes: 5 additions & 2 deletions actionpack/lib/action_view/helpers/javascript_helper.rb
Expand Up @@ -162,8 +162,11 @@ def escape_javascript(javascript)
# alert('All is good')
# //]]>
# </script>
def javascript_tag(content)
content_tag("script", javascript_cdata_section(content), :type => "text/javascript")
#
# +html_options+ may be a hash of attributes for the <script> tag. Example:
# javascript_tag "alert('All is good')", :defer => 'true' # => <script defer="true" type="text/javascript">alert('All is good')</script>
def javascript_tag(content, html_options = {})
content_tag("script", javascript_cdata_section(content), html_options.merge(:type => "text/javascript"))
end

def javascript_cdata_section(content) #:nodoc:
Expand Down
7 changes: 5 additions & 2 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -684,8 +684,11 @@ def update_page(&block)
# Works like update_page but wraps the generated JavaScript in a <script>
# tag. Use this to include generated JavaScript in an ERb template.
# See JavaScriptGenerator for more information.
def update_page_tag(&block)
javascript_tag update_page(&block)
#
# +html_options+ may be a hash of <script> attributes to be passed
# to ActionView::Helpers::JavaScriptHelper#javascript_tag.
def update_page_tag(html_options = {}, &block)
javascript_tag update_page(&block), html_options
end

protected
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -171,6 +171,12 @@ def test_update_page_tag
block = Proc.new { |page| page.replace_html('foo', 'bar') }
assert_equal javascript_tag(create_generator(&block).to_s), update_page_tag(&block)
end

def test_update_page_tag_with_html_options
block = Proc.new { |page| page.replace_html('foo', 'bar') }
assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block)
end

end

class JavaScriptGeneratorTest < Test::Unit::TestCase
Expand Down

0 comments on commit 8ff92e2

Please sign in to comment.