Skip to content

Commit 89c3746

Browse files
authored
Merge pull request rails#46573 from k0kubun/erb-util-prepend
Override ERB::Util#html_escape with Module#prepend
2 parents 324880c + d6c42b3 commit 89c3746

File tree

1 file changed

+35
-28
lines changed
  • activesupport/lib/active_support/core_ext/erb

1 file changed

+35
-28
lines changed

activesupport/lib/active_support/core_ext/erb/util.rb

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
# frozen_string_literal: true
22

3+
module ActiveSupport
4+
module CoreExt
5+
module ERBUtil
6+
# HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
7+
# This method is not for public consumption! Seriously!
8+
def html_escape(s) # :nodoc:
9+
s = s.to_s
10+
if s.html_safe?
11+
s
12+
else
13+
super(ActiveSupport::Multibyte::Unicode.tidy_bytes(s))
14+
end
15+
end
16+
alias :unwrapped_html_escape :html_escape # :nodoc:
17+
18+
# A utility method for escaping HTML tag characters.
19+
# This method is also aliased as <tt>h</tt>.
20+
#
21+
# puts html_escape('is a > 0 & a < 10?')
22+
# # => is a &gt; 0 &amp; a &lt; 10?
23+
def html_escape(s) # rubocop:disable Lint/DuplicateMethods
24+
unwrapped_html_escape(s).html_safe
25+
end
26+
alias h html_escape
27+
end
28+
29+
module ERBUtilPrivate
30+
include ERBUtil
31+
private :unwrapped_html_escape, :html_escape, :h
32+
end
33+
end
34+
end
35+
336
class ERB
437
module Util
538
HTML_ESCAPE = { "&" => "&amp;", ">" => "&gt;", "<" => "&lt;", '"' => "&quot;", "'" => "&#39;" }
@@ -17,34 +50,8 @@ module Util
1750
SAFE_XML_TAG_NAME_REGEXP = /\A[#{TAG_NAME_START_CODEPOINTS}][#{TAG_NAME_FOLLOWING_CODEPOINTS}]*\z/
1851
TAG_NAME_REPLACEMENT_CHAR = "_"
1952

20-
# A utility method for escaping HTML tag characters.
21-
# This method is also aliased as <tt>h</tt>.
22-
#
23-
# puts html_escape('is a > 0 & a < 10?')
24-
# # => is a &gt; 0 &amp; a &lt; 10?
25-
def html_escape(s)
26-
unwrapped_html_escape(s).html_safe
27-
end
28-
29-
silence_redefinition_of_method :h
30-
alias h html_escape
31-
32-
module_function :h
33-
34-
singleton_class.silence_redefinition_of_method :html_escape
35-
module_function :html_escape
36-
37-
# HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
38-
# This method is not for public consumption! Seriously!
39-
def unwrapped_html_escape(s) # :nodoc:
40-
s = s.to_s
41-
if s.html_safe?
42-
s
43-
else
44-
CGI.escapeHTML(ActiveSupport::Multibyte::Unicode.tidy_bytes(s))
45-
end
46-
end
47-
module_function :unwrapped_html_escape
53+
prepend ActiveSupport::CoreExt::ERBUtilPrivate
54+
singleton_class.prepend ActiveSupport::CoreExt::ERBUtil
4855

4956
# A utility method for escaping HTML without affecting existing escaped entities.
5057
#

0 commit comments

Comments
 (0)