Skip to content

Instantly share code, notes, and snippets.

@adzap
Created July 23, 2010 21:25
Show Gist options
  • Save adzap/488061 to your computer and use it in GitHub Desktop.
Save adzap/488061 to your computer and use it in GitHub Desktop.
Rails 3 HTML escaping when using content_tag
# Rails 3 beta 4
# Line 74 in actionpack/lib/action_view/helpers/tag_helper.rb
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
if block_given?
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
content_tag_string(name, capture(&block), options, escape)
else
content_tag_string(name, content_or_options_with_block, options, escape)
end
end
# Line 111 in actionpack/lib/action_view/helpers/tag_helper.rb
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
"<#{name}#{tag_options}>#{escape ? ERB::Util.h(content) : content}</#{name}>".html_safe
end
# Line 17 in activesupport/lib/active_support/core_ext/string/output_safety.rb
def html_escape(s)
s = s.to_s
if s.html_safe?
s
else
s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }.html_safe
end
end
alias h html_escape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment