HTML_SAFE允许除<span style="font-size:TAG 之外的所有内容



如何禁止在 html_safe 栏 3 中更改字体大小

我这里有一篇文章的截断描述,当用户使用 tinymce 编辑器输入大字体时,我想禁止在显示模式下使用大字体

= truncate(event.description.html_safe, :length => 110, :omission => "...")

我该怎么做?

您需要

在将sanitize帮助程序标记为html_safe之前使用它。不幸的是,在这种情况下,黑名单功能已被删除,因此除了默认值之外,您还需要列出您想要的所有属性。使用正则表达式删除有问题的属性可能更容易。

此外,就其价值而言,raw(event.description) 的作用与 event.description.html_safe 相同,但不会在 nil 值上爆炸(不确定您的验证规则是什么(,因此通常是首选。

编辑:

清理示例用法(来自 http://apidock.com/rails/v3.2.8/ActionView/Helpers/SanitizeHelper/sanitize(:

truncate( raw( sanitize(event.description, :tags => %w(table tr td), :attributes => %w(id class style) ) ), :length => 110, :omission => "...")

注意:像这样截断 HTML 可能会导致一些奇怪且难以追踪的错误,因为 END 标记会创建无效的 HTML。

最新更新