From ced84beb2518d173988bb92c6d96aa854a35abe6 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 16 Aug 2022 12:43:36 +0200 Subject: [ruby/cgi] Add snake case aliases for escapeURIComponent As agreed in [Feature #18822] https://github.com/ruby/cgi/commit/9d1161ec9d --- ext/cgi/escape/escape.c | 2 ++ lib/cgi/util.rb | 3 +++ test/cgi/test_cgi_util.rb | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/ext/cgi/escape/escape.c b/ext/cgi/escape/escape.c index c5b76de596..17a134aa1a 100644 --- a/ext/cgi/escape/escape.c +++ b/ext/cgi/escape/escape.c @@ -458,7 +458,9 @@ InitVM_escape(void) rb_define_method(rb_mEscape, "escapeHTML", cgiesc_escape_html, 1); rb_define_method(rb_mEscape, "unescapeHTML", cgiesc_unescape_html, 1); rb_define_method(rb_mEscape, "escapeURIComponent", cgiesc_escape_uri_component, 1); + rb_define_alias(rb_mEscape, "escape_uri_component", "escapeURIComponent"); rb_define_method(rb_mEscape, "unescapeURIComponent", cgiesc_unescape_uri_component, -1); + rb_define_alias(rb_mEscape, "unescape_uri_component", "unescapeURIComponent"); rb_define_method(rb_mEscape, "escape", cgiesc_escape, 1); rb_define_method(rb_mEscape, "unescape", cgiesc_unescape, -1); rb_prepend_module(rb_mUtil, rb_mEscape); diff --git a/lib/cgi/util.rb b/lib/cgi/util.rb index 8b129a44a5..4986e544e0 100644 --- a/lib/cgi/util.rb +++ b/lib/cgi/util.rb @@ -46,6 +46,7 @@ module CGI::Util end buffer.force_encoding(encoding) end + alias escape_uri_component escapeURIComponent # URL-decode a string following RFC 3986 with encoding(optional). # string = CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred") @@ -59,6 +60,8 @@ module CGI::Util str.valid_encoding? ? str : str.force_encoding(string.encoding) end + alias unescape_uri_component unescapeURIComponent + # The set of special characters and their escaped values TABLE_FOR_ESCAPE_HTML__ = { "'" => ''', diff --git a/test/cgi/test_cgi_util.rb b/test/cgi/test_cgi_util.rb index b3a46a1c25..1bdc0e4218 100644 --- a/test/cgi/test_cgi_util.rb +++ b/test/cgi/test_cgi_util.rb @@ -74,6 +74,10 @@ class CGIUtilTest < Test::Unit::TestCase assert_equal('%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93'.ascii_only?, CGI.escapeURIComponent(@str1).ascii_only?) if defined?(::Encoding) end + def test_cgi_escape_uri_component + assert_equal('%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93', CGI.escape_uri_component(@str1)) + end + def test_cgi_escapeURIComponent_with_unreserved_characters assert_equal("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~", CGI.escapeURIComponent("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"), @@ -101,6 +105,11 @@ class CGIUtilTest < Test::Unit::TestCase assert_equal("\u{30E1 30E2 30EA 691C 7D22}", CGI.unescapeURIComponent("\u{30E1 30E2 30EA}%E6%A4%9C%E7%B4%A2")) end + def test_cgi_unescape_uri_component + str = CGI.unescape_uri_component('%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93') + assert_equal(@str1, str) + end + def test_cgi_unescapeURIComponent_preserve_encoding assert_equal(Encoding::US_ASCII, CGI.unescapeURIComponent("%C0%3C%3C".dup.force_encoding("US-ASCII")).encoding) assert_equal(Encoding::ASCII_8BIT, CGI.unescapeURIComponent("%C0%3C%3C".dup.force_encoding("ASCII-8BIT")).encoding) -- cgit v1.2.3