现在我遇到了一些愚蠢的情况。我希望用户能够使用 textile,但他们不应该在他们的条目周围乱用我的有效 HTML。所以我必须以某种方式转义 HTML。
html_escape(textilize("</body>Foo"))
会破坏纺织品,而textilize(html_escape("</body>Foo"))
会工作,但会破坏各种 Textile 功能,例如链接(写成"Linkname":http://www.wheretogo.com/
),因为引号会转换为"
因此不再被纺织品检测到。sanitize
没有做得更好。
有什么建议吗?对于这个问题,我宁愿不使用 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, ifsanitize_html
is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.
关于html - 我如何编织和清理 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/501737/