Skip to content

Commit 5779612

Browse files
authored
Merge pull request #2728 from ruby/cgi
Update cgi and cgi/escape type definitions
2 parents df3600c + 9a3d3ef commit 5779612

File tree

7 files changed

+188
-283
lines changed

7 files changed

+188
-283
lines changed

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ end
231231

232232
task :stdlib_test => :compile do
233233
test_files = FileList["test/stdlib/**/*_test.rb"].reject do |path|
234-
path =~ %r{Ractor} || path =~ %r{Encoding} || path =~ %r{CGI_test}
234+
path =~ %r{Ractor} || path =~ %r{Encoding} || path =~ %r{CGI-escape_test}
235235
end
236236

237237
if ENV["RANDOMIZE_STDLIB_TEST_ORDER"] == "true"
@@ -240,7 +240,7 @@ task :stdlib_test => :compile do
240240

241241
sh "#{ruby} -Ilib #{bin}/test_runner.rb #{test_files.join(' ')}"
242242
# TODO: Ractor tests need to be run in a separate process
243-
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/CGI_test.rb"
243+
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/CGI-escape_test.rb"
244244
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Ractor_test.rb"
245245
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Encoding_test.rb"
246246
end

lib/rbs/collection/config/lockfile_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class LockfileGenerator
1414
"net-smtp" => nil,
1515
"nkf" => nil,
1616
"observer" => nil,
17+
"cgi" => nil,
1718
}
1819

1920
class GemfileLockMismatchError < StandardError

stdlib/cgi-escape/0/escape.rbs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# <!-- rdoc-file=lib/cgi/escape.rb -->
2+
# :stopdoc
3+
#
4+
class CGI
5+
include Escape
6+
7+
extend Escape
8+
9+
# <!-- rdoc-file=lib/cgi/escape.rb -->
10+
# Escape/unescape for CGI, HTML, URI.
11+
#
12+
module Escape
13+
# <!--
14+
# rdoc-file=lib/cgi/escape.rb
15+
# - escape(string)
16+
# -->
17+
# URL-encode a string into application/x-www-form-urlencoded. Space characters
18+
# (+" "+) are encoded with plus signs (+"+"+)
19+
# url_encoded_string = CGI.escape("'Stop!' said Fred")
20+
# # => "%27Stop%21%27+said+Fred"
21+
#
22+
def escape: (string str) -> String
23+
24+
# <!--
25+
# rdoc-file=lib/cgi/escape.rb
26+
# - escapeElement(string, *elements)
27+
# -->
28+
# Escape only the tags of certain HTML elements in `string`.
29+
#
30+
# Takes an element or elements or array of elements. Each element is specified
31+
# by the name of the element, without angle brackets. This matches both the
32+
# start and the end tag of that element. The attribute list of the open tag will
33+
# also be escaped (for instance, the double-quotes surrounding attribute
34+
# values).
35+
#
36+
# print CGI.escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
37+
# # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
38+
#
39+
# print CGI.escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
40+
# # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
41+
#
42+
def escapeElement: (string string, *String | Array[String] elements) -> String
43+
44+
# <!--
45+
# rdoc-file=lib/cgi/escape.rb
46+
# - escapeHTML(string)
47+
# -->
48+
# Escape special characters in HTML, namely '&"<>
49+
# CGI.escapeHTML('Usage: foo "bar" <baz>')
50+
# # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
51+
#
52+
def escapeHTML: (string str) -> String
53+
54+
# <!--
55+
# rdoc-file=lib/cgi/escape.rb
56+
# - escapeURIComponent(string)
57+
# -->
58+
# URL-encode a string following RFC 3986 Space characters (+" "+) are encoded
59+
# with (+"%20"+)
60+
# url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred")
61+
# # => "%27Stop%21%27%20said%20Fred"
62+
#
63+
def escapeURIComponent: (string) -> String
64+
65+
# <!-- rdoc-file=lib/cgi/escape.rb -->
66+
# Synonym for CGI.escapeElement(str)
67+
#
68+
alias escape_element escapeElement
69+
70+
# <!-- rdoc-file=lib/cgi/escape.rb -->
71+
# Synonym for CGI.escapeHTML(str)
72+
#
73+
alias escape_html escapeHTML
74+
75+
# <!--
76+
# rdoc-file=lib/cgi/escape.rb
77+
# - escape_uri_component(string)
78+
# -->
79+
#
80+
alias escape_uri_component escapeURIComponent
81+
82+
# <!--
83+
# rdoc-file=lib/cgi/escape.rb
84+
# - h(string)
85+
# -->
86+
#
87+
alias h escapeHTML
88+
89+
# <!--
90+
# rdoc-file=lib/cgi/escape.rb
91+
# - unescape(string, encoding = @@accept_charset)
92+
# -->
93+
# URL-decode an application/x-www-form-urlencoded string with
94+
# encoding(optional).
95+
# string = CGI.unescape("%27Stop%21%27+said+Fred")
96+
# # => "'Stop!' said Fred"
97+
#
98+
def unescape: (string str, ?encoding encoding) -> String
99+
100+
# <!--
101+
# rdoc-file=lib/cgi/escape.rb
102+
# - unescapeElement(string, *elements)
103+
# -->
104+
# Undo escaping such as that done by CGI.escapeElement()
105+
#
106+
# print CGI.unescapeElement(
107+
# CGI.escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
108+
# # "&lt;BR&gt;<A HREF="url"></A>"
109+
#
110+
# print CGI.unescapeElement(
111+
# CGI.escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
112+
# # "&lt;BR&gt;<A HREF="url"></A>"
113+
#
114+
def unescapeElement: (string string, *String | Array[String] elements) -> String
115+
116+
# <!--
117+
# rdoc-file=lib/cgi/escape.rb
118+
# - unescapeHTML(string)
119+
# -->
120+
# Unescape a string that has been HTML-escaped
121+
# CGI.unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
122+
# # => "Usage: foo \"bar\" <baz>"
123+
#
124+
def unescapeHTML: (string str) -> String
125+
126+
# <!--
127+
# rdoc-file=lib/cgi/escape.rb
128+
# - unescapeURIComponent(string, encoding = @@accept_charset)
129+
# -->
130+
# URL-decode a string following RFC 3986 with encoding(optional).
131+
# string = CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred")
132+
# # => "'Stop!'+said Fred"
133+
#
134+
def unescapeURIComponent: (string) -> String
135+
136+
# <!-- rdoc-file=lib/cgi/escape.rb -->
137+
# Synonym for CGI.unescapeElement(str)
138+
#
139+
alias unescape_element unescapeElement
140+
141+
# <!-- rdoc-file=lib/cgi/escape.rb -->
142+
# Synonym for CGI.unescapeHTML(str)
143+
#
144+
alias unescape_html unescapeHTML
145+
146+
# <!--
147+
# rdoc-file=lib/cgi/escape.rb
148+
# - unescape_uri_component(string, encoding = @@accept_charset)
149+
# -->
150+
#
151+
alias unescape_uri_component unescapeURIComponent
152+
end
153+
end

stdlib/cgi/0/core.rbs

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -637,88 +637,6 @@ class CGI
637637
| (Hash[String, untyped] options_hash) -> void
638638
end
639639

640-
# <!-- rdoc-file=lib/cgi/escape.rb -->
641-
# Escape/unescape for CGI, HTML, URI.
642-
#
643-
module Escape
644-
# <!--
645-
# rdoc-file=lib/cgi/escape.rb
646-
# - escape(string)
647-
# -->
648-
# URL-encode a string into application/x-www-form-urlencoded. Space characters
649-
# (+" "+) are encoded with plus signs (+"+"+)
650-
# url_encoded_string = CGI.escape("'Stop!' said Fred")
651-
# # => "%27Stop%21%27+said+Fred"
652-
#
653-
def escape: (string str) -> String
654-
655-
# <!--
656-
# rdoc-file=lib/cgi/escape.rb
657-
# - escapeHTML(string)
658-
# -->
659-
# Escape special characters in HTML, namely '&"<>
660-
# CGI.escapeHTML('Usage: foo "bar" <baz>')
661-
# # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
662-
#
663-
def escapeHTML: (string str) -> String
664-
665-
# <!--
666-
# rdoc-file=lib/cgi/escape.rb
667-
# - escapeURIComponent(string)
668-
# -->
669-
# URL-encode a string following RFC 3986 Space characters (+" "+) are encoded
670-
# with (+"%20"+)
671-
# url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred")
672-
# # => "%27Stop%21%27%20said%20Fred"
673-
#
674-
def escapeURIComponent: (string) -> String
675-
676-
# <!--
677-
# rdoc-file=lib/cgi/escape.rb
678-
# - escape_uri_component(string)
679-
# -->
680-
#
681-
alias escape_uri_component escapeURIComponent
682-
683-
# <!--
684-
# rdoc-file=lib/cgi/escape.rb
685-
# - unescape(string, encoding = @@accept_charset)
686-
# -->
687-
# URL-decode an application/x-www-form-urlencoded string with
688-
# encoding(optional).
689-
# string = CGI.unescape("%27Stop%21%27+said+Fred")
690-
# # => "'Stop!' said Fred"
691-
#
692-
def unescape: (string str, ?encoding encoding) -> String
693-
694-
# <!--
695-
# rdoc-file=lib/cgi/escape.rb
696-
# - unescapeHTML(string)
697-
# -->
698-
# Unescape a string that has been HTML-escaped
699-
# CGI.unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
700-
# # => "Usage: foo \"bar\" <baz>"
701-
#
702-
def unescapeHTML: (string str) -> String
703-
704-
# <!--
705-
# rdoc-file=lib/cgi/escape.rb
706-
# - unescapeURIComponent(string, encoding = @@accept_charset)
707-
# -->
708-
# URL-decode a string following RFC 3986 with encoding(optional).
709-
# string = CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred")
710-
# # => "'Stop!'+said Fred"
711-
#
712-
def unescapeURIComponent: (string) -> String
713-
714-
# <!--
715-
# rdoc-file=lib/cgi/escape.rb
716-
# - unescape_uri_component(string, encoding = @@accept_charset)
717-
# -->
718-
#
719-
alias unescape_uri_component unescapeURIComponent
720-
end
721-
722640
# <!-- rdoc-file=lib/cgi/core.rb -->
723641
# Exception raised when there is an invalid encoding detected
724642
#
@@ -935,45 +853,6 @@ class CGI
935853
end
936854

937855
module Util
938-
include CGI::Escape
939-
940-
# <!--
941-
# rdoc-file=lib/cgi/util.rb
942-
# - escapeElement(string, *elements)
943-
# -->
944-
# Escape only the tags of certain HTML elements in `string`.
945-
#
946-
# Takes an element or elements or array of elements. Each element is specified
947-
# by the name of the element, without angle brackets. This matches both the
948-
# start and the end tag of that element. The attribute list of the open tag will
949-
# also be escaped (for instance, the double-quotes surrounding attribute
950-
# values).
951-
#
952-
# print CGI.escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
953-
# # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
954-
#
955-
# print CGI.escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
956-
# # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
957-
#
958-
def escapeElement: (string string, *String | Array[String] elements) -> String
959-
960-
# <!-- rdoc-file=lib/cgi/util.rb -->
961-
# Synonym for CGI.escapeElement(str)
962-
#
963-
alias escape_element escapeElement
964-
965-
# <!-- rdoc-file=lib/cgi/util.rb -->
966-
# Synonym for CGI.escapeHTML(str)
967-
#
968-
alias escape_html escapeHTML
969-
970-
# <!--
971-
# rdoc-file=lib/cgi/util.rb
972-
# - h(string)
973-
# -->
974-
#
975-
alias h escapeHTML
976-
977856
# <!--
978857
# rdoc-file=lib/cgi/util.rb
979858
# - pretty(string, shift = " ")
@@ -1008,32 +887,6 @@ class CGI
1008887
#
1009888
def rfc1123_date: (Time time) -> String
1010889

1011-
# <!--
1012-
# rdoc-file=lib/cgi/util.rb
1013-
# - unescapeElement(string, *elements)
1014-
# -->
1015-
# Undo escaping such as that done by CGI.escapeElement()
1016-
#
1017-
# print CGI.unescapeElement(
1018-
# CGI.escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
1019-
# # "&lt;BR&gt;<A HREF="url"></A>"
1020-
#
1021-
# print CGI.unescapeElement(
1022-
# CGI.escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
1023-
# # "&lt;BR&gt;<A HREF="url"></A>"
1024-
#
1025-
def unescapeElement: (string string, *String | Array[String] elements) -> String
1026-
1027-
# <!-- rdoc-file=lib/cgi/util.rb -->
1028-
# Synonym for CGI.unescapeElement(str)
1029-
#
1030-
alias unescape_element unescapeElement
1031-
1032-
# <!-- rdoc-file=lib/cgi/util.rb -->
1033-
# Synonym for CGI.unescapeHTML(str)
1034-
#
1035-
alias unescape_html unescapeHTML
1036-
1037890
# Abbreviated day-of-week names specified by RFC 822
1038891
RFC822_DAYS: Array[String]
1039892

stdlib/cgi/0/manifest.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dependencies:
22
- name: tempfile
33
- name: stringio
4+
- name: cgi-escape

test/stdlib/CGI-escape_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require_relative "test_helper"
2+
require "cgi"
3+
4+
class CGI__EscapeSingletonTest < Test::Unit::TestCase
5+
include TestHelper
6+
7+
library "cgi-escape"
8+
testing "singleton(::CGI)"
9+
10+
def test_escapeURIComponent
11+
assert_send_type(
12+
"(String) -> String",
13+
CGI, :escapeURIComponent, "hogehoge"
14+
)
15+
assert_send_type(
16+
"(ToStr) -> String",
17+
CGI, :escapeURIComponent, ToStr.new("hogehoge")
18+
)
19+
end
20+
21+
def test_unescapeURIComponent
22+
assert_send_type(
23+
"(String) -> String",
24+
CGI, :unescapeURIComponent, "hogehoge"
25+
)
26+
assert_send_type(
27+
"(ToStr) -> String",
28+
CGI, :unescapeURIComponent, ToStr.new("hogehoge")
29+
)
30+
end
31+
end

0 commit comments

Comments
 (0)