[ruby/erb] Support all cgi.gem versions

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

https://github.com/ruby/erb/commit/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

@ -14,7 +14,6 @@ Gem::Specification.new do |spec|
spec.summary = %q{An easy to use but powerful templating system for Ruby.}
spec.description = %q{An easy to use but powerful templating system for Ruby.}
spec.homepage = 'https://github.com/ruby/erb'
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
spec.licenses = ['Ruby', 'BSD-2-Clause']
spec.metadata['homepage_uri'] = spec.homepage
@ -27,10 +26,11 @@ Gem::Specification.new do |spec|
spec.executables = ['erb']
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.2.0'
if RUBY_ENGINE == 'jruby'
spec.platform = 'java'
else
spec.required_ruby_version = '>= 2.7.0'
spec.extensions = ['ext/erb/escape/extconf.rb']
end
end

View File

@ -12,7 +12,6 @@
#
# You can redistribute it and/or modify it under the same terms as Ruby.
require 'cgi/escape'
require 'erb/version'
require 'erb/compiler'
require 'erb/def_method'

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