[ruby/erb] Support all cgi.gem versions

(https://github.com/ruby/erb/pull/60)

de9bb8c3cc
This commit is contained in:
Takashi Kokubun 2025-05-13 10:50:00 -07:00 committed by git
parent 9420fc8ad6
commit 735f28388c
3 changed files with 20 additions and 5 deletions

View file

@ -1,5 +1,13 @@
# frozen_string_literal: true
# Load CGI.escapeHTML and CGI.escapeURIComponent.
# CRuby:
# cgi.gem v0.1.0+ (Ruby 2.7-3.4) and Ruby 3.5+ stdlib have 'cgi/escape' and CGI.escapeHTML.
# cgi.gem v0.3.3+ (Ruby 3.2-3.4) and Ruby 3.5+ stdlib have CGI.escapeURIComponent.
# JRuby: cgi.gem has a Java extension 'cgi/escape'.
# TruffleRuby: lib/truffle/cgi/escape.rb requires 'cgi/util'.
require 'cgi/escape'
begin
# We don't build the C extension for JRuby, TruffleRuby, and WASM
if $LOAD_PATH.resolve_feature_path('erb/escape')
@ -53,8 +61,16 @@ module ERB::Util
#
# Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
#
def url_encode(s)
CGI.escapeURIComponent(s.to_s)
if CGI.respond_to?(:escapeURIComponent)
def url_encode(s)
CGI.escapeURIComponent(s.to_s)
end
else # cgi.gem <= v0.3.2
def url_encode(s)
s.to_s.b.gsub(/[^a-zA-Z0-9_\-.~]/n) do |m|
sprintf("%%%02X", m.unpack1("C"))
end
end
end
alias u url_encode
module_function :u