0

From the Rails console:

> 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>."

However:

> 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>."

As a matter of fact:

> 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>."

So the highlight helper seems to selectively wipe some HTML-attributes in its process, but not all. Not that I care so much for target or style, but losing ids is rather unfortunate.

Are there options I can pass into highlight to prevent this from happening?

1 Answer 1

0

You need to pass sanitize: false as an option:

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>."

The sanitize helper is documented here and most of the text helpers will sanitize the content by default.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.