Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define ERB::Escape module #38

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/erb/escape/escape.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ruby.h"
#include "ruby/encoding.h"

static VALUE rb_cERB, rb_mUtil, rb_cCGI;
static VALUE rb_cERB, rb_mEscape, rb_cCGI;
static ID id_escapeHTML;

#define HTML_ESCAPE_MAX_LEN 6
Expand Down Expand Up @@ -87,8 +87,8 @@ void
Init_escape(void)
{
rb_cERB = rb_define_class("ERB", rb_cObject);
rb_mUtil = rb_define_module_under(rb_cERB, "Util");
rb_define_module_function(rb_mUtil, "html_escape", erb_escape_html, 1);
rb_mEscape = rb_define_module_under(rb_cERB, "Escape");
rb_define_module_function(rb_mEscape, "html_escape", erb_escape_html, 1);

rb_cCGI = rb_define_class("CGI", rb_cObject);
id_escapeHTML = rb_intern("escapeHTML");
Expand Down
22 changes: 15 additions & 7 deletions lib/erb/util.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#--
# ERB::Escape
#
# A subset of ERB::Util. Unlike ERB::Util#html_escape, we expect/hope
# Rails will not monkey-patch ERB::Escape#html_escape.
begin
# ERB::Util.html_escape
require 'erb/escape'
rescue LoadError # JRuby can't load .so
end
unless defined?(ERB::Escape) # JRuby
module ERB::Escape
def html_escape(s)
CGI.escapeHTML(s.to_s)
end
module_function :html_escape
end
end

#--
# ERB::Util
Expand All @@ -21,12 +33,8 @@ module ERB::Util
#
# is a > 0 & a < 10?
#
unless defined?(ERB::Util.html_escape) # for JRuby
def html_escape(s)
CGI.escapeHTML(s.to_s)
end
module_function :html_escape
end
include ERB::Escape # html_escape
module_function :html_escape
alias h html_escape
module_function :h

Expand Down