diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-02 17:28:19 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-02 17:28:19 -0300 |
commit | 2f4b82f1d84799ca91ccfe314644bcde886851a0 (patch) | |
tree | 754effa59f34f42391545ea1b39815b5ba0c8578 | |
parent | 2bdb4ec6ad9bb99ac0029fcb030ac757307d08ae (diff) | |
download | rails-2f4b82f1d84799ca91ccfe314644bcde886851a0.tar.gz rails-2f4b82f1d84799ca91ccfe314644bcde886851a0.tar.bz2 rails-2f4b82f1d84799ca91ccfe314644bcde886851a0.zip |
Fix html_escape with Ruby 1.8
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/output_safety.rb | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index c0bd821ea5..0984b25d6b 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -6,21 +6,32 @@ class ERB HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' } JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' } - # 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 + if RUBY_VERSION >= '1.9' + # 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 + else + def html_escape(s) #:nodoc: + s = s.to_s + if s.html_safe? + s + else + s.gsub(/[&"'><]/n) { |special| HTML_ESCAPE[special] }.html_safe + end end end |