/gist:65e767d9d5b3283a0271 Secret
Created
September 5, 2012 14:43
-
-
Save nistude/65e767d9d5b3283a0271 to your computer and use it in GitHub Desktop.
Monkey patch to handle regression in ERB::Util#html_escape in rails 3.0.17 / ruby 1.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ERB | |
module Util | |
# see https://github.com/rails/rails/issues/7430 for details | |
# | |
# A utility method for escaping HTML tag characters. | |
# This method is also aliased as <tt>h</tt>. | |
# | |
# In your ERB templates, use this method to escape any unsafe content. For example: | |
# <%=h @person.name %> | |
# | |
# ==== Example: | |
# puts html_escape("is a > 0 & a < 10?") | |
# # => is a > 0 & a < 10? | |
def html_escape(s) | |
s = s.to_s | |
if s.html_safe? | |
s | |
else | |
s.gsub(/[&"'><]/, HTML_ESCAPE).html_safe | |
end | |
end | |
alias h html_escape | |
singleton_class.send(:remove_method, :html_escape) | |
module_function :html_escape, :h | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks you very much!