Usering
Usering

Reputation: 143

sanitize gem issue with < and >

I am using the sanitize gem https://github.com/rgrove/sanitize to remove some HTML tags from a string.

However, before sanitizing the string in my controller, the string is being set as follows:

<p>This is &lt;b&gt;bold&lt;/b&gt; and this <span style="text-decoration: underline;">is</span> &lt;i&gt;italics&lt;/i&gt; ok? This <em>is not </em>a problem.</p>

meaning that < and > are being replaced by &lt; and &gt;.

How can I use the sanitize gem to remove for example and when these tags are being represented as &lt;i&gt; and &lt;/i&gt; in the controller?

Upvotes: 0

Views: 3275

Answers (1)

Richard Hulse
Richard Hulse

Reputation: 10493

If you want the escaped HTML tags (< and >) to be treated as HTML for the purposes of sanitizing, then you'll have to unescape them first:

require 'cgi'
Sanitize.clean(CGI.unescapeHTML(your_string))

Upvotes: 6

Related Questions