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

Avoid using prepend + super for fallback #28

Merged
merged 1 commit into from
Nov 4, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/tmp/
Gemfile.lock
*.so
*.bundle
*.gem
12 changes: 8 additions & 4 deletions ext/erb/erb.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "ruby.h"
#include "ruby/encoding.h"

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

#define HTML_ESCAPE_MAX_LEN 6

Expand Down Expand Up @@ -76,14 +77,17 @@ erb_escape_html(VALUE self, VALUE str)
return optimized_escape_html(str);
}
else {
return rb_call_super(1, &str);
return rb_funcall(rb_cCGI, id_escapeHTML, 1, str);
}
}

void
Init_erb(void)
{
rb_cERB = rb_define_class("ERB", rb_cObject);
rb_mEscape = rb_define_module_under(rb_cERB, "Escape");
rb_define_method(rb_mEscape, "html_escape", erb_escape_html, 1);
rb_mUtil = rb_define_module_under(rb_cERB, "Util");
rb_define_method(rb_mUtil, "html_escape", erb_escape_html, 1);

rb_cCGI = rb_define_class("CGI", rb_cObject);
id_escapeHTML = rb_intern("escapeHTML");
}
20 changes: 7 additions & 13 deletions lib/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -998,20 +998,14 @@ module Util
#
# is a > 0 & a < 10?
#
def html_escape(s)
CGI.escapeHTML(s.to_s)
begin
# ERB::Util.html_escape
require 'erb.so'
rescue LoadError
def html_escape(s)
CGI.escapeHTML(s.to_s)
end
end
end

begin
require 'erb.so'
rescue LoadError
else
private_constant :Escape
Util.prepend(Escape)
end

module Util
alias h html_escape
module_function :h
module_function :html_escape
Expand Down