Skip to content

Commit 6ef2455

Browse files
authored
Merge pull request #2299 from ruby/cgi-escape
Add `CGI.escape/unescape_uri_component` and `URI.encode/decode_uri_component`
2 parents 5ddd3f7 + d407fea commit 6ef2455

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

stdlib/cgi/0/core.rbs

+10
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,11 @@ class CGI
931931
#
932932
def escapeURIComponent: (string) -> String
933933

934+
# <!-- rdoc-file=ext/cgi/escape/escape.c -->
935+
# Returns URL-escaped string following RFC 3986.
936+
#
937+
alias escape_uri_component escapeURIComponent
938+
934939
# <!--
935940
# rdoc-file=ext/cgi/escape/escape.c
936941
# - CGI.unescape(string, encoding=@@accept_charset) -> string
@@ -954,6 +959,11 @@ class CGI
954959
# Returns URL-unescaped string following RFC 3986.
955960
#
956961
def unescapeURIComponent: (string) -> String
962+
963+
# <!-- rdoc-file=ext/cgi/escape/escape.c -->
964+
# Returns URL-unescaped string following RFC 3986.
965+
#
966+
alias unescape_uri_component unescapeURIComponent
957967
end
958968

959969
# <!-- rdoc-file=lib/cgi/core.rb -->

stdlib/uri/0/common.rbs

+17
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ end
115115
module URI
116116
include URI::RFC2396_REGEXP
117117

118+
# <!--
119+
# rdoc-file=lib/uri/common.rb
120+
# - decode_uri_component(str, enc=Encoding::UTF_8)
121+
# -->
122+
# Like URI.decode_www_form_component, except that `'+'` is preserved.
123+
#
124+
def self.decode_uri_component: (String str, ?encoding enc) -> String
125+
118126
# <!--
119127
# rdoc-file=lib/uri/common.rb
120128
# - decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false)
@@ -189,6 +197,15 @@ module URI
189197
#
190198
def self.decode_www_form_component: (String str, ?encoding enc) -> String
191199

200+
# <!--
201+
# rdoc-file=lib/uri/common.rb
202+
# - encode_uri_component(str, enc=nil)
203+
# -->
204+
# Like URI.encode_www_form_component, except that `' '` (space) is encoded as
205+
# `'%20'` (instead of `'+'`).
206+
#
207+
def self.encode_uri_component: (String str, ?encoding enc) -> String
208+
192209
# <!--
193210
# rdoc-file=lib/uri/common.rb
194211
# - encode_www_form(enum, enc=nil)

test/stdlib/URI_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ class URISingletonTest < Test::Unit::TestCase
77
library "uri"
88
testing "singleton(::URI)"
99

10+
def test_decode_uri_component
11+
assert_send_type(
12+
"(String) -> String",
13+
URI, :decode_uri_component, "Hello%20World%20%E6%97%A5%E6%9C%AC%E8%AA%9E"
14+
)
15+
16+
assert_send_type(
17+
"(String, Encoding) -> String",
18+
URI, :decode_uri_component, "Hello%20World%20%E6%97%A5%E6%9C%AC%E8%AA%9E", Encoding::UTF_8
19+
)
20+
end
21+
1022
def test_decode_www_form
1123
assert_send_type "(String) -> Array[[String, String]]",
1224
URI, :decode_www_form, "a=1&a=2&b=3"
@@ -45,6 +57,17 @@ def test_decode_www_form_component
4557
URI, :decode_www_form_component, "%A1", "sjis"
4658
end
4759

60+
def test_encode_uri_component
61+
assert_send_type(
62+
"(String) -> String",
63+
URI, :encode_uri_component, "Hello World 日本語"
64+
)
65+
assert_send_type(
66+
"(String, Encoding) -> String",
67+
URI, :encode_uri_component, "Hello World 日本語", Encoding::UTF_8
68+
)
69+
end
70+
4871
def test_encode_www_form
4972
assert_send_type "(Array[[String, String | Numeric]]) -> String",
5073
URI, :encode_www_form, [["a", "1"], ["a", 2], ["b", "3"]]

0 commit comments

Comments
 (0)