Skip to content

Commit 574896a

Browse files
k0kubunmatzbot
authored andcommitted
[ruby/erb] Define ERB::Escape module
(ruby/erb#38) Close #32
1 parent 15be9ec commit 574896a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
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_module_function(rb_mUtil, "html_escape", erb_escape_html, 1);
90+
rb_mEscape = rb_define_module_under(rb_cERB, "Escape");
91+
rb_define_module_function(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: 15 additions & 7 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+
unless defined?(ERB::Escape) # JRuby
11+
module ERB::Escape
12+
def html_escape(s)
13+
CGI.escapeHTML(s.to_s)
14+
end
15+
module_function :html_escape
16+
end
17+
end
618

719
#--
820
# ERB::Util
@@ -21,12 +33,8 @@ module ERB::Util
2133
#
2234
# is a > 0 & a < 10?
2335
#
24-
unless defined?(ERB::Util.html_escape) # for JRuby
25-
def html_escape(s)
26-
CGI.escapeHTML(s.to_s)
27-
end
28-
module_function :html_escape
29-
end
36+
include ERB::Escape # html_escape
37+
module_function :html_escape
3038
alias h html_escape
3139
module_function :h
3240

0 commit comments

Comments
 (0)