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/lib/action_view/helpers/javascript_helper.rb | 12 ++++++++---- actionpack/lib/action_view/helpers/tag_helper.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'actionpack/lib') 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?}) -- cgit v1.2.3