我爱Sanitize .这是一个了不起的实用程序。我遇到的唯一问题是,它需要永远准备一个开发环境,因为它使用 Nokogiri
,这对编译时间来说是一种痛苦。是否有任何程序可以在不使用 Nokogiri
的情况下执行 Sanitize 的操作(如果没有别的,只是温和地执行它的操作)?这将以指数方式提供帮助!
最佳答案
Rails 有自己的 SanitizeHelper
。
根据 http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html , 它将
This sanitize helper will html encode all tags and strip all attributes that aren’t specifically allowed.
It also strips href/src tags with invalid protocols, like javascript: especially. It does its best to counter any tricks that hackers may use, like throwing in unicode/ascii/hex values to get past the javascript: filters. Check out the extensive test suite.
你可以像这样在 View 中使用它
<%= sanitize @article.body %>
您可以访问链接以查看更多自定义选项,例如:
自定义使用(只允许使用提到的标签和属性,其他都不允许)
<%= sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style) %>
关于ruby-on-rails - 对于 Ruby 应用程序,是否有比 Sanitize 更好的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341061/