Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit efbf0c0

Browse files
committed
Try to simplyfiy the ERB patch
1 parent fecdf8b commit efbf0c0

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

lib/angular_xss/erb.rb

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# Use module_eval so we crash when ERB::Util has not yet been loaded.
2-
if defined?(ActiveSupport::CoreExt::ERBUtil) && ERB::Util.is_a?(ActiveSupport::CoreExt::ERBUtil)
3-
# Rails 7.1+
1+
if ERB::Util.private_method_defined? :unwrapped_html_escape
2+
# Rails 4.2 +
43
# https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/erb/util.rb
54
module ERBUtilExt
65
def html_escape_once(s)
@@ -17,51 +16,22 @@ def unwrapped_html_escape(s)
1716

1817
else
1918
ERB::Util.module_eval do
20-
if private_method_defined? :unwrapped_html_escape
21-
# Rails 4.2 - 7.0
22-
# patch ERB::Util#unwrapped_html_escape
23-
def unwrapped_html_escape_with_escaping_angular_expressions(s)
24-
unwrapped_html_escape_without_escaping_angular_expressions(AngularXss::Escaper.escape_if_unsafe(s))
25-
end
19+
# Rails < 4.2
2620

27-
alias_method :unwrapped_html_escape_without_escaping_angular_expressions, :unwrapped_html_escape
28-
alias_method :unwrapped_html_escape, :unwrapped_html_escape_with_escaping_angular_expressions
29-
30-
singleton_class.send(:remove_method, :unwrapped_html_escape)
31-
module_function :unwrapped_html_escape
32-
module_function :unwrapped_html_escape_without_escaping_angular_expressions
33-
34-
# patch ERB::Util#html_escape_once
35-
def html_escape_once_with_escaping_angular_expressions(s)
36-
html_escape_once_without_escaping_angular_expressions(AngularXss::Escaper.escape_if_unsafe(s))
37-
end
38-
39-
alias_method :html_escape_once_without_escaping_angular_expressions, :html_escape_once
40-
alias_method :html_escape_once, :html_escape_once_with_escaping_angular_expressions
41-
42-
singleton_class.send(:remove_method, :html_escape_once)
43-
module_function :html_escape_once
44-
module_function :html_escape_once_without_escaping_angular_expressions
45-
else
46-
# Rails < 4.2
47-
48-
def html_escape_with_escaping_angular_expressions(s)
49-
html_escape_without_escaping_angular_expressions(AngularXss::Escaper.escape_if_unsafe(s))
50-
end
51-
52-
alias_method_chain :html_escape, :escaping_angular_expressions
53-
54-
# Aliasing twice issues a warning "discarding old...". Remove first to avoid it.
55-
remove_method(:h)
56-
alias h html_escape
21+
def html_escape_with_escaping_angular_expressions(s)
22+
html_escape_without_escaping_angular_expressions(AngularXss::Escaper.escape_if_unsafe(s))
23+
end
5724

58-
module_function :h
25+
alias_method_chain :html_escape, :escaping_angular_expressions
5926

60-
singleton_class.send(:remove_method, :html_escape)
61-
module_function :html_escape
62-
module_function :html_escape_without_escaping_angular_expressions
27+
# Aliasing twice issues a warning "discarding old...". Remove first to avoid it.
28+
remove_method(:h)
29+
alias h html_escape
6330

64-
end
31+
module_function :h
6532

33+
singleton_class.send(:remove_method, :html_escape)
34+
module_function :html_escape
35+
module_function :html_escape_without_escaping_angular_expressions
6636
end
6737
end

0 commit comments

Comments
 (0)