Skip to content

Commit 75f8089

Browse files
committed
Define ERB::Escape module
Close #32
1 parent 5c10f56 commit 75f8089

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ext/erb/escape/escape.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "ruby.h"
22
#include "ruby/encoding.h"
33

4-
static VALUE rb_cERB, rb_mUtil, rb_cCGI;
4+
static VALUE rb_cERB, rb_mEscape, rb_cCGI;
55
static ID id_escapeHTML;
66

77
#define HTML_ESCAPE_MAX_LEN 6
@@ -87,8 +87,8 @@ void
8787
Init_escape(void)
8888
{
8989
rb_cERB = rb_define_class("ERB", rb_cObject);
90-
rb_mUtil = rb_define_module_under(rb_cERB, "Util");
91-
rb_define_method(rb_mUtil, "html_escape", erb_escape_html, 1);
90+
rb_mEscape = rb_define_module_under(rb_cERB, "Escape");
91+
rb_define_method(rb_mEscape, "html_escape", erb_escape_html, 1);
9292

9393
rb_cCGI = rb_define_class("CGI", rb_cObject);
9494
id_escapeHTML = rb_intern("escapeHTML");

lib/erb/util.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
#--
2+
# ERB::Escape
3+
#
4+
# A subset of ERB::Util. Unlike ERB::Util#html_escape, we expect/hope
5+
# Rails will not monkey-patch ERB::Escape#html_escape.
16
begin
2-
# ERB::Util.html_escape
37
require 'erb/escape'
48
rescue LoadError # JRuby can't load .so
59
end
10+
module ERB::Escape
11+
unless method_defined?(:html_escape) # JRuby
12+
def html_escape(s)
13+
CGI.escapeHTML(s.to_s)
14+
end
15+
end
16+
module_function :html_escape
17+
end
618

719
#--
820
# ERB::Util
@@ -21,7 +33,9 @@ module ERB::Util
2133
#
2234
# is a > 0 & a < 10?
2335
#
24-
unless method_defined?(:html_escape) # for JRuby
36+
if defined?(ERB::Escape)
37+
include ERB::Escape # html_escape
38+
else # JRuby
2539
def html_escape(s)
2640
CGI.escapeHTML(s.to_s)
2741
end

0 commit comments

Comments
 (0)