Skip to content Skip to sidebar Skip to footer

How To Sanitize Html String Except Image Url?

I'm trying to sanitize an html string, but I want to whitelist image urls. My code: ActionView::Base.full_sanitizer.sanitize(phrase.meaning, tags: %w(img), attributes: %w(src)) Th

Solution 1:

Perhaps it is easier to use the PermitScrubber from the same gem directly:

html = 'Foo <img src="foo" title="bar"> <a href="foo">bar</a> blob'
scrubber = Rails::Html::PermitScrubber.new
scrubber.tags = ['img']
html_fragment = Loofah.fragment(html)
html_fragment.scrub!(scrubber)
html_fragment.to_s
#=> "Foo <img src=\"foo\" title=\"bar\"> bar blob"

Post a Comment for "How To Sanitize Html String Except Image Url?"