首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

CGI::Util

Constants

RFC822_DAYS

由RFC 822指定的缩写星期几名称

RFC822_MONTHS

由RFC 822指定的缩写月份名称

TABLE_FOR_ESCAPE_HTML__

一组特殊字符及其转义值

公共实例方法

escape(string) 显示源文件

URL编码一个字符串。

代码语言:javascript
复制
url_encoded_string = CGI::escape("'Stop!' said Fred")
   # => "%27Stop%21%27+said+Fred"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 7
def escape(string)
  encoding = string.encoding
  string.b.gsub(/([^ a-zA-Z0-9_.-]+)/) do |m|
    '%' + m.unpack('H2' * m.bytesize).join('%').upcase
  end.tr(' ', '+').force_encoding(encoding)
end

escapeElement(string, *elements)显示源文件

只退出某些HTML元素的标签string

采用一个或多个元素或元素数组。每个元素由元素的名称指定,没有尖括号。这匹配该元素的开始标记和结束标记。开放标签的属性列表也将被转义(例如,围绕属性值的双引号)。

代码语言:javascript
复制
print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
  # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"

print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
  # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 135
def escapeElement(string, *elements)
  elements = elements[0] if elements[0].kind_of?(Array)
  unless elements.empty?
    string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/i) do
      CGI::escapeHTML($&)
    end
  else
    string
  end
end

另外别名为:escape_element

escapeHTML(string) 显示源文件

转义HTML中的特殊字符,即'&“<>

代码语言:javascript
复制
CGI::escapeHTML('Usage: foo "bar" <baz>')
   # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 36
def escapeHTML(string)
  enc = string.encoding
  unless enc.ascii_compatible?
    if enc.dummy?
      origenc = enc
      enc = Encoding::Converter.asciicompat_encoding(enc)
      string = enc ? string.encode(enc) : string.b
    end
    table = Hash[TABLE_FOR_ESCAPE_HTML__.map {|pair|pair.map {|s|s.encode(enc)}}]
    string = string.gsub(/#{"['&\"<>]".encode(enc)}/, table)
    string.encode!(origenc) if origenc
    return string
  end
  string.gsub(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
end

另外别名为:escape_html,h

escape_element(string, *elements)

Synonym for CGI::escapeElement(str)

别名为:escapeElement

escape_html(string)

Synonym for CGI::escapeHTML(str)

别名为:escapeHTML

h(string)

别名为:escapeHTML

pretty(string, shift = " ") Show source

Prettify (indent) an HTML string.

string是要缩进的HTML字符串。shift是要使用的缩进单元; 它默认为两个空格。

代码语言:javascript
复制
print CGI::pretty("<HTML><BODY></BODY></HTML>")
  # <HTML>
  #   <BODY>
  #   </BODY>
  # </HTML>

print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
  # <HTML>
  #         <BODY>
  #         </BODY>
  # </HTML>
代码语言:javascript
复制
# File lib/cgi/util.rb, line 206
def pretty(string, shift = "  ")
  lines = string.gsub(/(?!\A)<.*?>/m, "\n\\0").gsub(/<.*?>(?!\n)/m, "\\0\n")
  end_pos = 0
  while end_pos = lines.index(/^<\/(\w+)/, end_pos)
    element = $1.dup
    start_pos = lines.rindex(/^\s*<#{element}/i, end_pos)
    lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/, "\n" + shift) + "__"
  end
  lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/, '\1')
end

rfc1123_date(time) 显示源文件

使用RFC 1123指定Time的格式将对象格式化为字符串。

unescape(string,encoding = @@ accept_charset)显示源文件

使用编码对URL进行URL解码(可选)。

代码语言:javascript
复制
string = CGI::unescape("%27Stop%21%27+said+Fred")
   # => "'Stop!' said Fred"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 17
def unescape(string,encoding=@@accept_charset)
  str=string.tr('+', ' ').b.gsub(/((?:%[0-9a-fA-F]{2})+)/) do |m|
    [m.delete('%')].pack('H*')
  end.force_encoding(encoding)
  str.valid_encoding? ? str : str.force_encoding(string.encoding)
end

unescapeElement(string, *elements) Show source

撤消转义,例如CGI :: escapeElement()完成的转义

代码语言:javascript
复制
print CGI::unescapeElement(
        CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
  # "&lt;BR&gt;<A HREF="url"></A>"

print CGI::unescapeElement(
        CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
  # "&lt;BR&gt;<A HREF="url"></A>"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 155
def unescapeElement(string, *elements)
  elements = elements[0] if elements[0].kind_of?(Array)
  unless elements.empty?
    string.gsub(/&lt;\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?&gt;/i) do
      unescapeHTML($&)
    end
  else
    string
  end
end

另外别名为:unescape_element

unescapeHTML(string) 显示源文件

Unescape已被HTML转义的字符串

代码语言:javascript
复制
CGI::unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
   # => "Usage: foo \"bar\" <baz>"
代码语言:javascript
复制
# File lib/cgi/util.rb, line 60
def unescapeHTML(string)
  enc = string.encoding
  unless enc.ascii_compatible?
    if enc.dummy?
      origenc = enc
      enc = Encoding::Converter.asciicompat_encoding(enc)
      string = enc ? string.encode(enc) : string.b
    end
    string = string.gsub(Regexp.new('&(apos|amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
      case $1.encode(Encoding::US_ASCII)
      when 'apos'                then "'".encode(enc)
      when 'amp'                 then '&'.encode(enc)
      when 'quot'                then '"'.encode(enc)
      when 'gt'                  then '>'.encode(enc)
      when 'lt'                  then '<'.encode(enc)
      when /\A#0*(\d+)\z/        then $1.to_i.chr(enc)
      when /\A#x([0-9a-f]+)\z/i  then $1.hex.chr(enc)
      end
    end
    string.encode!(origenc) if origenc
    return string
  end
  return string unless string.include? '&'
  charlimit = case enc
              when Encoding::UTF_8; 0x10ffff
              when Encoding::ISO_8859_1; 256
              else 128
              end
  string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
    match = $1.dup
    case match
    when 'apos'                then "'"
    when 'amp'                 then '&'
    when 'quot'                then '"'
    when 'gt'                  then '>'
    when 'lt'                  then '<'
    when /\A#0*(\d+)\z/
      n = $1.to_i
      if n < charlimit
        n.chr(enc)
      else
        "&##{$1};"
      end
    when /\A#x([0-9a-f]+)\z/i
      n = $1.hex
      if n < charlimit
        n.chr(enc)
      else
        "&#x#{$1};"
      end
    else
      "&#{match};"
    end
  end
end

另外别名为:unescape_html

unescape_element(string, *elements)

CGI :: unescapeElement(str)的同义词

别名为:unescapeElement

unescape_html(string)

CGI :: unescapeHTML(str)的同义词

别名为:unescapeHTML

扫码关注腾讯云开发者

领取腾讯云代金券