Reputation: 10769
Let's say I have:
@string = "it is a <a href="#">string</a>"
I want to use it in different parts of my application in two ways:
The first one can be done using html_safe
:
@string.html_safe
It is a string
How can I achieve the second one?
It is a string.
Upvotes: 27
Views: 27842
Reputation: 13
Inspired by upstairs, I define this function in my project
def delete_html_markup(data)
return data if data.blank?
if data.is_a?(Array)
data.map{ | s | delete_html_markup(s) }
elsif data.is_a?(Hash)
data.each do | k, v |
data[k] = delete_html_markup(v)
end
else
ActionView::Base.full_sanitizer.sanitize(data)
end
end
Upvotes: 0
Reputation: 12456
For general-purpose use (e.g. web scraper):
puts Rails::Html::FullSanitizer.new.sanitize("<div>Hello</div><br>")
# Hello
Upvotes: 7
Reputation: 22948
You can try this:
ActionView::Base.full_sanitizer.sanitize(@string)
See strip_tags(html).
Upvotes: 54
Reputation: 291
In Rails, see also the strip_tags method. http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags
Upvotes: 1
Reputation: 23344
You can use nokogiri
to do the same.
This SO post tells the story.
Here in short:
This uses the XPath's starts-with
function:
You have to first define it like this:
require 'nokogiri'
item = Nokogiri::HTML('<a href="#">string</a>')
puts item.to_html
The above will give the html output. Then you can use XPath.
item.search('//a[not(starts-with(@href, "http://"))]').each do |a|
a.replace(a.content)
end
puts item.to_html
Upvotes: 3
Reputation: 37906
Rails provides a method called strip_links
, which seems to do what you want (looking at its name).
According to its APIDock page it is a bit limited. To make it applicable to a/any string you could extend the string class:
class String
def strip_links
ActionController::Base.helpers.strip_links(self)
end
end
So you can use:
@string.strip_links
Upvotes: 0