mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/cgi.rb (CGI::escape): m17nized.
(CGI::unescape): ditto. (CGI::escapeHTML): ditto. (CGI::unescapeHTML): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14614 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f9b894273f
commit
78fd550840
2 changed files with 19 additions and 10 deletions
|
@ -1,3 +1,10 @@
|
|||
Mon Dec 24 23:55:29 2007 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* lib/cgi.rb (CGI::escape): m17nized.
|
||||
(CGI::unescape): ditto.
|
||||
(CGI::escapeHTML): ditto.
|
||||
(CGI::unescapeHTML): ditto.
|
||||
|
||||
Mon Dec 24 23:32:24 2007 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* transcode_data_japanese.c (select_iso_2022_mode): '\e' is not valid.
|
||||
|
|
22
lib/cgi.rb
22
lib/cgi.rb
|
@ -339,8 +339,8 @@ class CGI
|
|||
# url_encoded_string = CGI::escape("'Stop!' said Fred")
|
||||
# # => "%27Stop%21%27+said+Fred"
|
||||
def CGI::escape(string)
|
||||
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
|
||||
'%' + $1.unpack('H2' * $1.size).join('%').upcase
|
||||
string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
|
||||
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
|
||||
end.tr(' ', '+')
|
||||
end
|
||||
|
||||
|
@ -349,8 +349,9 @@ class CGI
|
|||
# string = CGI::unescape("%27Stop%21%27+said+Fred")
|
||||
# # => "'Stop!' said Fred"
|
||||
def CGI::unescape(string)
|
||||
string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
|
||||
[$1.delete('%')].pack('H*')
|
||||
enc = string.encoding
|
||||
string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
|
||||
[$1.delete('%')].pack('H*').force_encoding(enc)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -359,7 +360,7 @@ class CGI
|
|||
# CGI::escapeHTML('Usage: foo "bar" <baz>')
|
||||
# # => "Usage: foo "bar" <baz>"
|
||||
def CGI::escapeHTML(string)
|
||||
string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(/</n, '<')
|
||||
string.gsub(/&/, '&').gsub(/\"/, '"').gsub(/>/, '>').gsub(/</, '<')
|
||||
end
|
||||
|
||||
|
||||
|
@ -367,22 +368,23 @@ class CGI
|
|||
# CGI::unescapeHTML("Usage: foo "bar" <baz>")
|
||||
# # => "Usage: foo \"bar\" <baz>"
|
||||
def CGI::unescapeHTML(string)
|
||||
string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/n) do
|
||||
enc = string.encoding
|
||||
string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do
|
||||
match = $1.dup
|
||||
case match
|
||||
when 'amp' then '&'
|
||||
when 'quot' then '"'
|
||||
when 'gt' then '>'
|
||||
when 'lt' then '<'
|
||||
when /\A#0*(\d+)\z/n then
|
||||
when /\A#0*(\d+)\z/ then
|
||||
if Integer($1) < 256
|
||||
Integer($1).chr
|
||||
Integer($1).chr.force_encoding(enc)
|
||||
else
|
||||
"&##{$1};"
|
||||
end
|
||||
when /\A#x([0-9a-f]+)\z/ni then
|
||||
when /\A#x([0-9a-f]+)\z/i then
|
||||
if $1.hex < 256
|
||||
$1.hex.chr
|
||||
$1.hex.chr.force_encoding(enc)
|
||||
else
|
||||
"&#x#{$1};"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue