我如何纺织和消毒HTML?

前端之家收集整理的这篇文章主要介绍了我如何纺织和消毒HTML?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
现在我遇到了一些愚蠢的情况.我希望用户能够使用纺织品,但他们不应该在他们的条目周围乱用我的有效HTML.所以我必须以某种方式逃避HTML.

> html_escape(textilize(“< / body> Foo”))会破坏纺织品
> textilize(html_escape(“< / body> Foo”))可以工作,但会打破各种纺织品功能,如链接(写作“Linkname”:http://www.wheretogo.com/),因为引号会被转换进入& quot;因此不再被纺织品检测到.
>消毒不会做得更好.

有关那个的任何建议吗?我宁愿不使用Tidy来解决这个问题.
提前致谢.

解决方法

对于那些遇到同样问题的人:如果您正在使用RedCloth gem,您可以定义自己的方法(在您的一个帮助程序中).
def safe_textilize( s )
  if s && s.respond_to?(:to_s)
    doc = RedCloth.new( s.to_s )
    doc.filter_html = true
    doc.to_html
  end
end

摘自文档:

Accessors for setting security restrictions.

This is a nice thing if you‘re using RedCloth for formatting in
public places (e.g. Wikis) where you don‘t want users to abuse HTML for bad things.

If filter_html is set,HTML which wasn‘t created by the Textile processor will be
escaped. Alternatively,if sanitize_html is set,HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

原文链接:https://www.f2er.com/html/231757.html

猜你在找的HTML相关文章