从Rails控制台:
> content = "Lorem ipsum dolor sit <span class='class'>amet</span>."
> helper.highlight(content, "dolor")
=> "Lorem ipsum <strong class=\"highlight\">dolor</strong> sit <span class=\"class\">amet</span>."
然而:
> content = "Lorem ipsum dolor sit <span id='id'>amet</span>."
> helper.highlight(content, "dolor")
=> "Lorem ipsum <strong class=\"highlight\">dolor</strong> sit <span>amet</span>."
事实上:
> content = "Lorem ipsum dolor sit <a href='http://127.0.0.1' class='class' id='id' style='color: #ffffff;' target='_blank' name='name' title='title'>amet</a>."
> helper.highlight(content, "dolor")
=> "Lorem ipsum <strong class=\"highlight\">dolor</strong> sit <a href=\"http://127.0.0.1\" class=\"class\" name=\"name\" title=\"title\">amet</a>."
因此,突出显示帮助程序似乎在其进程中选择性地擦除了一些HTML属性,但并非全部。并非我对target
或style
非常关心,但失去id
是相当不幸的。
我是否可以将highlight
传递给{{1}}以阻止这种情况发生?
答案 0 :(得分:0)
您需要传递sanitize: false
作为选项:
2.0.0-p247 :001 > content = "Lorem ipsum dolor sit <span id='id'>amet</span>."
=> "Lorem ipsum dolor sit <span id='id'>amet</span>."
2.0.0-p247 :002 > helper.highlight(content, 'dolor', sanitize: false)
=> "Lorem ipsum <mark>dolor</mark> sit <span id='id'>amet</span>."
清理帮助程序记录在案here,大多数文本助手都会默认清理内容。