From f49dc37e7100d8358d864a6e008c17acd24765fc Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 12 Oct 2005 22:47:39 +0000 Subject: Wrap javascript_tag contents in a CDATA section and add a cdata_section method to TagHelper. Closes #1691. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2543 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/javascript_helper.rb | 12 ++++++---- actionpack/lib/action_view/helpers/tag_helper.rb | 9 ++++++++ actionpack/test/controller/capture_test.rb | 2 +- .../template/java_script_macros_helper_test.rb | 12 +++++----- actionpack/test/template/javascript_helper_test.rb | 26 +++++++++++----------- actionpack/test/template/tag_helper_test.rb | 4 ++++ 7 files changed, 43 insertions(+), 24 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index b5fad73118..152c144a37 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Wrap javascript_tag contents in a CDATA section and add a cdata_section method to TagHelper #1691 [Michael Schuerig, Sam Stephenson] + * Misc doc fixes (typos/grammar/etc). #2445. [coffee2code] * Speed improvement for session_options. #2287. [skaes@web.de] diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 72d9f6dcec..6fe90f970c 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -437,9 +437,13 @@ module ActionView # Returns a JavaScript tag with the +content+ inside. Example: # javascript_tag "alert('All is good')" # => def javascript_tag(content) - content_tag("script", content, :type => "text/javascript") + content_tag("script", javascript_cdata_section(content), :type => "text/javascript") end + def javascript_cdata_section(content) #:nodoc: + "\n//#{cdata_section("\n#{content}\n//")}\n" + end + private def options_for_javascript(options) '{' + options.map {|k, v| "#{k}:#{v}"}.sort.join(', ') + '}' @@ -480,11 +484,11 @@ module ActionView def build_observer(klass, name, options = {}) options[:with] ||= 'value' if options[:update] callback = remote_function(options) - javascript = '" + javascript << "#{callback}})" + javascript_tag(javascript) end def build_callbacks(options) diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 2bd91ec547..6c71b8b767 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -22,6 +22,15 @@ module ActionView "<#{name}#{tag_options(options.stringify_keys) if options}>#{content}" end + # Returns a CDATA section for the given +content+. CDATA sections + # are used to escape blocks of text containing characters which would + # otherwise be recognized as markup. CDATA sections begin with the string + # <![CDATA[ and end with (and may not contain) the string + # ]]>. + def cdata_section(content) + "" + end + private def tag_options(options) cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?}) diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index eab7865937..461777e4e2 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -40,7 +40,7 @@ class CaptureTest < Test::Unit::TestCase def test_update_element_with_capture get :update_element_with_capture assert_equal( - "" + + "" + "\n\n$('status').innerHTML = '\\n You bought something!\\n';", @response.body.strip ) diff --git a/actionpack/test/template/java_script_macros_helper_test.rb b/actionpack/test/template/java_script_macros_helper_test.rb index 5f96d06861..1e882dba04 100644 --- a/actionpack/test/template/java_script_macros_helper_test.rb +++ b/actionpack/test/template/java_script_macros_helper_test.rb @@ -23,15 +23,15 @@ class JavaScriptMacrosHelperTest < Test::Unit::TestCase def test_auto_complete_field - assert_dom_equal %(), + assert_dom_equal %(), auto_complete_field("some_input", :url => { :action => "autocomplete" }); - assert_dom_equal %(), + assert_dom_equal %(), auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => ','); - assert_dom_equal %(), + assert_dom_equal %(), auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => [',']); - assert_dom_equal %(), + assert_dom_equal %(), auto_complete_field("some_input", :url => { :action => "autocomplete" }, :min_chars => 3); - assert_dom_equal %(), + assert_dom_equal %(), auto_complete_field("some_input", :url => { :action => "autocomplete" }, :on_hide => "function(element, update){Alert('me');}"); end @@ -50,7 +50,7 @@ class JavaScriptMacrosHelperTest < Test::Unit::TestCase def test_text_field_with_auto_complete assert_match "