Module: CGI::Util
Constant Summary collapse
- TABLE_FOR_ESCAPE_HTML__ =
The set of special characters and their escaped values
{ "'" => ''', '&' => '&', '"' => '"', '<' => '<', '>' => '>', }
Instance Method Summary collapse
-
#escape(c) ⇒ Object
URL-encode a string into application/x-www-form-urlencoded.
-
#escapeHTML(string) ⇒ Object
(also: #escape_html, #h)
Escape special characters in HTML, namely '&\"<> CGI.escapeHTML('Usage: foo "bar"
') # => "Usage: foo "bar" <baz>". -
#escapeURIComponent(c) ⇒ Object
URL-encode a string following RFC 3986 Space characters (+" "+) are encoded with (+"%20"+) url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred") # => "%27Stop%21%27%20said%20Fred".
-
#unescape(c) ⇒ Object
URL-decode an application/x-www-form-urlencoded string with encoding(optional).
-
#unescapeHTML(string) ⇒ Object
(also: #unescape_html)
Unescape a string that has been HTML-escaped CGI.unescapeHTML("Usage: foo "bar" <baz>") # => "Usage: foo \"bar\"
". -
#unescapeURIComponent(c) ⇒ Object
URL-decode a string following RFC 3986 with encoding(optional).
Instance Method Details
#escape(c) ⇒ Object
URL-encode a string into application/x-www-form-urlencoded. Space characters (+" "+) are encoded with plus signs (+"+"+) url_encoded_string = CGI.escape("'Stop!' said Fred") # => "%27Stop%21%27+said+Fred"
12 13 14 |
# File 'opal/stdlib/cgi/util.rb', line 12 def escape(c) `encodeURI(c)` end |
#escapeHTML(string) ⇒ Object Also known as: escape_html, h
Escape special characters in HTML, namely '&\"<>
CGI.escapeHTML('Usage: foo "bar"
50 51 52 |
# File 'opal/stdlib/cgi/util.rb', line 50 def escapeHTML(string) string.gsub(/['&"<>]/, TABLE_FOR_ESCAPE_HTML__) end |
#escapeURIComponent(c) ⇒ Object
URL-encode a string following RFC 3986 Space characters (+" "+) are encoded with (+"%20"+) url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred") # => "%27Stop%21%27%20said%20Fred"
27 28 29 |
# File 'opal/stdlib/cgi/util.rb', line 27 def escapeURIComponent(c) `encodeURIComponent(c)` end |
#unescape(c) ⇒ Object
URL-decode an application/x-www-form-urlencoded string with encoding(optional). string = CGI.unescape("%27Stop%21%27+said+Fred") # => "'Stop!' said Fred"
19 20 21 |
# File 'opal/stdlib/cgi/util.rb', line 19 def unescape(c) `decodeURI(c)` end |
#unescapeHTML(string) ⇒ Object Also known as: unescape_html
Unescape a string that has been HTML-escaped
CGI.unescapeHTML("Usage: foo "bar" <baz>")
# => "Usage: foo \"bar\"
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'opal/stdlib/cgi/util.rb', line 57 def unescapeHTML(string) string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do match = ::Regexp.last_match(1) case match when 'apos' then "'" when 'amp' then '&' when 'quot' then '"' when 'gt' then '>' when 'lt' then '<' when /\A#0*(\d+)\z/ n = ::Regexp.last_match(1).to_i n.chr('utf-8') when /\A#x([0-9a-f]+)\z/i n = ::Regexp.last_match(1).hex n.chr('utf-8') else "&#{match};" end end end |
#unescapeURIComponent(c) ⇒ Object
URL-decode a string following RFC 3986 with encoding(optional). string = CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred") # => "'Stop!'+said Fred"
34 35 36 |
# File 'opal/stdlib/cgi/util.rb', line 34 def unescapeURIComponent(c) `decodeURIComponent(c)` end |