[前][次][番号順一覧][スレッド一覧]

ruby-changes:74354

From: Takashi <ko1@a...>
Date: Sat, 5 Nov 2022 01:46:41 +0900 (JST)
Subject: [ruby-changes:74354] b169d78c88 (master): [ruby/erb] Avoid using prepend + super for fallback

https://git.ruby-lang.org/ruby.git/commit/?id=b169d78c88

From b169d78c882efdb4a3da6077f6d723f65ded6f15 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Fri, 4 Nov 2022 09:46:23 -0700
Subject: [ruby/erb] Avoid using prepend + super for fallback
 (https://github.com/ruby/erb/pull/28)

`prepend` is prioritized more than ActiveSupport's monkey-patch, but the
monkey-patch needs to work.

https://github.com/ruby/erb/commit/611de5a865
---
 ext/erb/erb.c | 12 ++++++++----
 lib/erb.rb    | 20 +++++++-------------
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/ext/erb/erb.c b/ext/erb/erb.c
index 4adab8ad33..c90f77f7b1 100644
--- a/ext/erb/erb.c
+++ b/ext/erb/erb.c
@@ -1,7 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ext/erb/erb.c#L1
 #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
 
@@ -76,7 +77,7 @@ erb_escape_html(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/erb/erb.c#L77
         return optimized_escape_html(str);
     }
     else {
-        return rb_call_super(1, &str);
+        return rb_funcall(rb_cCGI, id_escapeHTML, 1, str);
     }
 }
 
@@ -84,6 +85,9 @@ void https://github.com/ruby/ruby/blob/trunk/ext/erb/erb.c#L85
 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");
 }
diff --git a/lib/erb.rb b/lib/erb.rb
index c588ae1a65..48dcca71aa 100644
--- a/lib/erb.rb
+++ b/lib/erb.rb
@@ -998,20 +998,14 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L998
     #
     #   is a &gt; 0 &amp; a &lt; 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
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]