Is there a better HTML escaping and unescaping tool than CGI for Ruby?

后端 未结 2 1414
慢半拍i
慢半拍i 2021-02-05 08:12

CGI.escapeHTML is pretty bad, but CGI.unescapeHTML is completely borked. For example:

require \'cgi\'

CGI.unescapeHTML(\'…\         


        
相关标签:
2条回答
  • 2021-02-05 08:44

    The htmlentities gem should do the trick:

    require 'rubygems'
    require 'htmlentities'
    
    coder = HTMLEntities.new
    
    coder.decode('…') # => "…"
    coder.decode('…') # => "…"
    coder.decode('¢') # => "¢"
    coder.decode('¢') # => "¢"
    coder.encode("…", :named) # => "…"
    coder.encode("…", :decimal) # => "…"
    
    0 讨论(0)
  • 2021-02-05 08:53
    require 'rubygems'
    require 'hpricot'
    
    Hpricot('…', :xhtml_strict => true).to_plain_text
    

    Though you might have to fiddle around with the character encoding.

    0 讨论(0)
提交回复
热议问题