Skip to content

Commit 611de5a

Browse files
authored
Avoid using prepend + super for fallback (#28)
`prepend` is prioritized more than ActiveSupport's monkey-patch, but the monkey-patch needs to work.
1 parent ecebf80 commit 611de5a

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
/tmp/
99
Gemfile.lock
1010
*.so
11+
*.bundle
1112
*.gem

ext/erb/erb.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "ruby.h"
22
#include "ruby/encoding.h"
33

4-
static VALUE rb_cERB, rb_mEscape;
4+
static VALUE rb_cERB, rb_mUtil, rb_cCGI;
5+
static ID id_escapeHTML;
56

67
#define HTML_ESCAPE_MAX_LEN 6
78

@@ -76,14 +77,17 @@ erb_escape_html(VALUE self, VALUE str)
7677
return optimized_escape_html(str);
7778
}
7879
else {
79-
return rb_call_super(1, &str);
80+
return rb_funcall(rb_cCGI, id_escapeHTML, 1, str);
8081
}
8182
}
8283

8384
void
8485
Init_erb(void)
8586
{
8687
rb_cERB = rb_define_class("ERB", rb_cObject);
87-
rb_mEscape = rb_define_module_under(rb_cERB, "Escape");
88-
rb_define_method(rb_mEscape, "html_escape", erb_escape_html, 1);
88+
rb_mUtil = rb_define_module_under(rb_cERB, "Util");
89+
rb_define_method(rb_mUtil, "html_escape", erb_escape_html, 1);
90+
91+
rb_cCGI = rb_define_class("CGI", rb_cObject);
92+
id_escapeHTML = rb_intern("escapeHTML");
8993
}

lib/erb.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -998,20 +998,14 @@ module Util
998998
#
999999
# is a > 0 & a < 10?
10001000
#
1001-
def html_escape(s)
1002-
CGI.escapeHTML(s.to_s)
1001+
begin
1002+
# ERB::Util.html_escape
1003+
require 'erb.so'
1004+
rescue LoadError
1005+
def html_escape(s)
1006+
CGI.escapeHTML(s.to_s)
1007+
end
10031008
end
1004-
end
1005-
1006-
begin
1007-
require 'erb.so'
1008-
rescue LoadError
1009-
else
1010-
private_constant :Escape
1011-
Util.prepend(Escape)
1012-
end
1013-
1014-
module Util
10151009
alias h html_escape
10161010
module_function :h
10171011
module_function :html_escape

0 commit comments

Comments
 (0)