aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2005-10-12 22:47:39 +0000
committerSam Stephenson <sam@37signals.com>2005-10-12 22:47:39 +0000
commitf49dc37e7100d8358d864a6e008c17acd24765fc (patch)
tree10b737ba4c573cdec74362f427e605d72996f166 /actionpack/lib
parentb840e4ed509da433895fd2becf13f3e5656708e4 (diff)
downloadrails-f49dc37e7100d8358d864a6e008c17acd24765fc.tar.gz
rails-f49dc37e7100d8358d864a6e008c17acd24765fc.tar.bz2
rails-f49dc37e7100d8358d864a6e008c17acd24765fc.zip
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
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/javascript_helper.rb12
-rw-r--r--actionpack/lib/action_view/helpers/tag_helper.rb9
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>&lt;![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?})