Pregunta

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:

  • With a clickable link
  • Without the clickable link (but not showing any HTML markup)

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.

¿Fue útil?

Solución

You can try this:

ActionView::Base.full_sanitizer.sanitize(@string)

See strip_tags(html).

Otros consejos

You can try this:

strip_tags(@string)

For general-purpose use (e.g. web scraper):

puts Rails::Html::FullSanitizer.new.sanitize("<div>Hello</div><br>")
# Hello

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

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

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top