1
1
# frozen_string_literal: true
2
2
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 > 0 & a < 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
+
3
36
class ERB
4
37
module Util
5
38
HTML_ESCAPE = { "&" => "&" , ">" => ">" , "<" => "<" , '"' => """ , "'" => "'" }
@@ -17,34 +50,8 @@ module Util
17
50
SAFE_XML_TAG_NAME_REGEXP = /\A [#{ TAG_NAME_START_CODEPOINTS } ][#{ TAG_NAME_FOLLOWING_CODEPOINTS } ]*\z /
18
51
TAG_NAME_REPLACEMENT_CHAR = "_"
19
52
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 > 0 & a < 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
48
55
49
56
# A utility method for escaping HTML without affecting existing escaped entities.
50
57
#
0 commit comments