PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/Ruby/StdLib/ruby/1.9.1/cgi/util.rb

http://github.com/IronLanguages/main
Ruby | 188 lines | 175 code | 4 blank | 9 comment | 0 complexity | 4ae0f937f9473c4aae781c8e39bc6082 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. class CGI
  2. @@accept_charset="UTF-8" unless defined?(@@accept_charset)
  3. # URL-encode a string.
  4. # url_encoded_string = CGI::escape("'Stop!' said Fred")
  5. # # => "%27Stop%21%27+said+Fred"
  6. def CGI::escape(string)
  7. string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
  8. '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
  9. end.tr(' ', '+')
  10. end
  11. # URL-decode a string with encoding(optional).
  12. # string = CGI::unescape("%27Stop%21%27+said+Fred")
  13. # # => "'Stop!' said Fred"
  14. def CGI::unescape(string,encoding=@@accept_charset)
  15. str=string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
  16. [$1.delete('%')].pack('H*')
  17. end.force_encoding(encoding)
  18. str.valid_encoding? ? str : str.force_encoding(string.encoding)
  19. end
  20. TABLE_FOR_ESCAPE_HTML__ = {
  21. '&' => '&',
  22. '"' => '"',
  23. '<' => '&lt;',
  24. '>' => '&gt;',
  25. }
  26. # Escape special characters in HTML, namely &\"<>
  27. # CGI::escapeHTML('Usage: foo "bar" <baz>')
  28. # # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
  29. def CGI::escapeHTML(string)
  30. string.gsub(/[&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
  31. end
  32. # Unescape a string that has been HTML-escaped
  33. # CGI::unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
  34. # # => "Usage: foo \"bar\" <baz>"
  35. def CGI::unescapeHTML(string)
  36. enc = string.encoding
  37. if [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(enc)
  38. return string.gsub(Regexp.new('&(amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
  39. case $1.encode("US-ASCII")
  40. when 'amp' then '&'.encode(enc)
  41. when 'quot' then '"'.encode(enc)
  42. when 'gt' then '>'.encode(enc)
  43. when 'lt' then '<'.encode(enc)
  44. when /\A#0*(\d+)\z/ then $1.to_i.chr(enc)
  45. when /\A#x([0-9a-f]+)\z/i then $1.hex.chr(enc)
  46. end
  47. end
  48. end
  49. asciicompat = Encoding.compatible?(string, "a")
  50. string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do
  51. match = $1.dup
  52. case match
  53. when 'amp' then '&'
  54. when 'quot' then '"'
  55. when 'gt' then '>'
  56. when 'lt' then '<'
  57. when /\A#0*(\d+)\z/
  58. n = $1.to_i
  59. if enc == Encoding::UTF_8 or
  60. enc == Encoding::ISO_8859_1 && n < 256 or
  61. asciicompat && n < 128
  62. n.chr(enc)
  63. else
  64. "&##{$1};"
  65. end
  66. when /\A#x([0-9a-f]+)\z/i
  67. n = $1.hex
  68. if enc == Encoding::UTF_8 or
  69. enc == Encoding::ISO_8859_1 && n < 256 or
  70. asciicompat && n < 128
  71. n.chr(enc)
  72. else
  73. "&#x#{$1};"
  74. end
  75. else
  76. "&#{match};"
  77. end
  78. end
  79. end
  80. def CGI::escape_html(str)
  81. escapeHTML(str)
  82. end
  83. def CGI::unescape_html(str)
  84. unescapeHTML(str)
  85. end
  86. # Escape only the tags of certain HTML elements in +string+.
  87. #
  88. # Takes an element or elements or array of elements. Each element
  89. # is specified by the name of the element, without angle brackets.
  90. # This matches both the start and the end tag of that element.
  91. # The attribute list of the open tag will also be escaped (for
  92. # instance, the double-quotes surrounding attribute values).
  93. #
  94. # print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
  95. # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
  96. #
  97. # print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
  98. # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
  99. def CGI::escapeElement(string, *elements)
  100. elements = elements[0] if elements[0].kind_of?(Array)
  101. unless elements.empty?
  102. string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/i) do
  103. CGI::escapeHTML($&)
  104. end
  105. else
  106. string
  107. end
  108. end
  109. # Undo escaping such as that done by CGI::escapeElement()
  110. #
  111. # print CGI::unescapeElement(
  112. # CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
  113. # # "&lt;BR&gt;<A HREF="url"></A>"
  114. #
  115. # print CGI::unescapeElement(
  116. # CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
  117. # # "&lt;BR&gt;<A HREF="url"></A>"
  118. def CGI::unescapeElement(string, *elements)
  119. elements = elements[0] if elements[0].kind_of?(Array)
  120. unless elements.empty?
  121. string.gsub(/&lt;\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?&gt;/i) do
  122. CGI::unescapeHTML($&)
  123. end
  124. else
  125. string
  126. end
  127. end
  128. def CGI::escape_element(str)
  129. escapeElement(str)
  130. end
  131. def CGI::unescape_element(str)
  132. unescapeElement(str)
  133. end
  134. # Abbreviated day-of-week names specified by RFC 822
  135. RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
  136. # Abbreviated month names specified by RFC 822
  137. RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
  138. # Format a +Time+ object as a String using the format specified by RFC 1123.
  139. #
  140. # CGI::rfc1123_date(Time.now)
  141. # # Sat, 01 Jan 2000 00:00:00 GMT
  142. def CGI::rfc1123_date(time)
  143. t = time.clone.gmtime
  144. return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
  145. RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
  146. t.hour, t.min, t.sec)
  147. end
  148. # Prettify (indent) an HTML string.
  149. #
  150. # +string+ is the HTML string to indent. +shift+ is the indentation
  151. # unit to use; it defaults to two spaces.
  152. #
  153. # print CGI::pretty("<HTML><BODY></BODY></HTML>")
  154. # # <HTML>
  155. # # <BODY>
  156. # # </BODY>
  157. # # </HTML>
  158. #
  159. # print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
  160. # # <HTML>
  161. # # <BODY>
  162. # # </BODY>
  163. # # </HTML>
  164. #
  165. def CGI::pretty(string, shift = " ")
  166. lines = string.gsub(/(?!\A)<.*?>/m, "\n\\0").gsub(/<.*?>(?!\n)/m, "\\0\n")
  167. end_pos = 0
  168. while end_pos = lines.index(/^<\/(\w+)/, end_pos)
  169. element = $1.dup
  170. start_pos = lines.rindex(/^\s*<#{element}/i, end_pos)
  171. lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/, "\n" + shift) + "__"
  172. end
  173. lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/, '\1')
  174. end
  175. end