Skip to content

Commit 9154d72

Browse files
zverokhsbt
authored andcommitted
Improve CGI.escape* docs
1 parent 10a6821 commit 9154d72

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/cgi/escape.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
# frozen_string_literal: true
22

3-
# :stopdoc
3+
# Since Ruby 4.0, \CGI is a small holder for various escaping methods, included from CGI::Escape
4+
#
5+
# require 'cgi/escape'
6+
#
7+
# CGI.escape("Ruby programming language")
8+
# #=> "Ruby+programming+language"
9+
# CGI.escapeURIComponent("Ruby programming language")
10+
# #=> "Ruby%20programming%20language"
11+
#
12+
# See CGI::Escape module for methods list and their description.
413
class CGI
514
module Escape; end
615
include Escape
716
extend Escape
817
module EscapeExt; end # :nodoc:
918
end
10-
# :startdoc:
1119

12-
# Escape/unescape for CGI, HTML, URI.
20+
# Web-related escape/unescape functionality.
1321
module CGI::Escape
1422
@@accept_charset = Encoding::UTF_8 unless defined?(@@accept_charset)
1523

1624
# URL-encode a string into application/x-www-form-urlencoded.
17-
# Space characters (+" "+) are encoded with plus signs (+"+"+)
25+
# Space characters (<tt>" "</tt>) are encoded with plus signs (<tt>"+"</tt>)
1826
# url_encoded_string = CGI.escape("'Stop!' said Fred")
1927
# # => "%27Stop%21%27+said+Fred"
2028
def escape(string)
@@ -41,7 +49,7 @@ def unescape(string, encoding = @@accept_charset)
4149
end
4250

4351
# URL-encode a string following RFC 3986
44-
# Space characters (+" "+) are encoded with (+"%20"+)
52+
# Space characters (<tt>" "</tt>) are encoded with (<tt>"%20"</tt>)
4553
# url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred")
4654
# # => "%27Stop%21%27%20said%20Fred"
4755
def escapeURIComponent(string)
@@ -69,15 +77,15 @@ def unescapeURIComponent(string, encoding = @@accept_charset)
6977
alias unescape_uri_component unescapeURIComponent
7078

7179
# The set of special characters and their escaped values
72-
TABLE_FOR_ESCAPE_HTML__ = {
80+
TABLE_FOR_ESCAPE_HTML__ = { # :nodoc:
7381
"'" => '&#39;',
7482
'&' => '&amp;',
7583
'"' => '&quot;',
7684
'<' => '&lt;',
7785
'>' => '&gt;',
7886
}
7987

80-
# Escape special characters in HTML, namely '&\"<>
88+
# \Escape special characters in HTML, namely <tt>'&\"<></tt>
8189
# CGI.escapeHTML('Usage: foo "bar" <baz>')
8290
# # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
8391
def escapeHTML(string)
@@ -160,11 +168,9 @@ def unescapeHTML(string)
160168
string.force_encoding enc
161169
end
162170

163-
# Synonym for CGI.escapeHTML(str)
164171
alias escape_html escapeHTML
165172
alias h escapeHTML
166173

167-
# Synonym for CGI.unescapeHTML(str)
168174
alias unescape_html unescapeHTML
169175

170176
# TruffleRuby runs the pure-Ruby variant faster, do not use the C extension there
@@ -175,7 +181,7 @@ def unescapeHTML(string)
175181
end
176182
end
177183

178-
# Escape only the tags of certain HTML elements in +string+.
184+
# \Escape only the tags of certain HTML elements in +string+.
179185
#
180186
# Takes an element or elements or array of elements. Each element
181187
# is specified by the name of the element, without angle brackets.
@@ -199,7 +205,7 @@ def escapeElement(string, *elements)
199205
end
200206
end
201207

202-
# Undo escaping such as that done by CGI.escapeElement()
208+
# Undo escaping such as that done by CGI.escapeElement
203209
#
204210
# print CGI.unescapeElement(
205211
# CGI.escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
@@ -219,10 +225,8 @@ def unescapeElement(string, *elements)
219225
end
220226
end
221227

222-
# Synonym for CGI.escapeElement(str)
223228
alias escape_element escapeElement
224229

225-
# Synonym for CGI.unescapeElement(str)
226230
alias unescape_element unescapeElement
227231

228232
end

0 commit comments

Comments
 (0)