diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/javascript_helper.rb | 12 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/tag_helper.rb | 9 |
2 files changed, 17 insertions, 4 deletions
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')" # => <script type="text/javascript">alert('All is good')</script> 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 = '<script type="text/javascript">' - javascript << "new #{klass}('#{name}', " + javascript = "new #{klass}('#{name}', " javascript << "#{options[:frequency]}, " if options[:frequency] javascript << "function(element, value) {" - javascript << "#{callback}})</script>" + 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}</#{name}>" 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 + # <tt><![CDATA[</tt> and end with (and may not contain) the string + # <tt>]]></tt>. + def cdata_section(content) + "<![CDATA[#{content}]]>" + end + private def tag_options(options) cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?}) |