html - 我如何编织和清理 html?

标签 html ruby-on-rails escaping textile

现在我遇到了一些愚蠢的情况。我希望用户能够使用 textile,但他们不应该在他们的条目周围乱用我的有效 HTML。所以我必须以某种方式转义 HTML。

  • html_escape(textilize("</body>Foo"))会破坏纺织品,而

  • textilize(html_escape("</body>Foo"))会工作,但会破坏各种 Textile 功能,例如链接(写成 "Linkname":http://www.wheretogo.com/ ),因为引号会转换为 &quot;因此不再被纺织品检测到。

  • 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, if sanitize_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/

相关文章:

ruby-on-rails - 如何将文件上传添加到 Rails 应用程序?

ruby-on-rails - Rails 在设计中使用 link_to 的正确方法

html - 如何仅使用 HTML 和 CSS 创建子选项卡

PHP 页面未使用可见性 :none or display:none 隐藏 DIV

ruby-on-rails - 为什么 Rails 5 在模式文件中添加 nextval 方法?

javascript - 转义括号内的引号

php - 如何使用 PHP 通过 JSON 发送 HTML 元素?

sed - 匹配转义字符,反斜杠+双引号

javascript - 单击按钮时使用 JavaScript 或 JQuery 向下滚动

html - 如何在 Bootstrap 中获取跨越父级整个宽度的按钮组?