mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Wed Jul 18 07:59:29 2012 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
* lib/cgi/util.rb (CGI.escapeHTML,unescapeHTML): Add ' for HTML5 escaping. [Feature #6620] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ba2ed2edeb
commit
c47cca2f85
3 changed files with 19 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Wed Jul 18 07:59:29 2012 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/cgi/util.rb (CGI.escapeHTML,unescapeHTML): Add ' for HTML5 escaping.
|
||||||
|
[Feature #6620]
|
||||||
|
|
||||||
Tue Jul 17 22:17:13 2012 Tanaka Akira <akr@fsij.org>
|
Tue Jul 17 22:17:13 2012 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* lib/open-uri.rb: call io.close! for Tempfile.
|
* lib/open-uri.rb: call io.close! for Tempfile.
|
||||||
|
|
|
@ -22,6 +22,7 @@ class CGI
|
||||||
|
|
||||||
# The set of special characters and their escaped values
|
# The set of special characters and their escaped values
|
||||||
TABLE_FOR_ESCAPE_HTML__ = {
|
TABLE_FOR_ESCAPE_HTML__ = {
|
||||||
|
"'" => ''',
|
||||||
'&' => '&',
|
'&' => '&',
|
||||||
'"' => '"',
|
'"' => '"',
|
||||||
'<' => '<',
|
'<' => '<',
|
||||||
|
@ -32,7 +33,7 @@ class CGI
|
||||||
# CGI::escapeHTML('Usage: foo "bar" <baz>')
|
# CGI::escapeHTML('Usage: foo "bar" <baz>')
|
||||||
# # => "Usage: foo "bar" <baz>"
|
# # => "Usage: foo "bar" <baz>"
|
||||||
def CGI::escapeHTML(string)
|
def CGI::escapeHTML(string)
|
||||||
string.gsub(/[&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
|
string.gsub(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Unescape a string that has been HTML-escaped
|
# Unescape a string that has been HTML-escaped
|
||||||
|
@ -41,8 +42,9 @@ class CGI
|
||||||
def CGI::unescapeHTML(string)
|
def CGI::unescapeHTML(string)
|
||||||
enc = string.encoding
|
enc = string.encoding
|
||||||
if [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(enc)
|
if [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(enc)
|
||||||
return string.gsub(Regexp.new('&(amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
|
return string.gsub(Regexp.new('&(apos|amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
|
||||||
case $1.encode("US-ASCII")
|
case $1.encode("US-ASCII")
|
||||||
|
when 'apos' then "'".encode(enc)
|
||||||
when 'amp' then '&'.encode(enc)
|
when 'amp' then '&'.encode(enc)
|
||||||
when 'quot' then '"'.encode(enc)
|
when 'quot' then '"'.encode(enc)
|
||||||
when 'gt' then '>'.encode(enc)
|
when 'gt' then '>'.encode(enc)
|
||||||
|
@ -53,9 +55,10 @@ class CGI
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
asciicompat = Encoding.compatible?(string, "a")
|
asciicompat = Encoding.compatible?(string, "a")
|
||||||
string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do
|
string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do
|
||||||
match = $1.dup
|
match = $1.dup
|
||||||
case match
|
case match
|
||||||
|
when 'apos' then "'"
|
||||||
when 'amp' then '&'
|
when 'amp' then '&'
|
||||||
when 'quot' then '"'
|
when 'quot' then '"'
|
||||||
when 'gt' then '>'
|
when 'gt' then '>'
|
||||||
|
|
|
@ -53,4 +53,12 @@ class CGIUtilTest < Test::Unit::TestCase
|
||||||
assert_equal("<HTML>\n\t<BODY>\n\t</BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>","\t"))
|
assert_equal("<HTML>\n\t<BODY>\n\t</BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>","\t"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_cgi_escapeHTML
|
||||||
|
assert_equal(CGI::escapeHTML("'&\"><"),"'&"><")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_cgi_unescapeHTML
|
||||||
|
assert_equal(CGI::unescapeHTML("'&"><"),"'&\"><")
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue