28

I need to turn HTML into plain text. There's a nice function that does that in ActionView's SanitizeHelper, but I have trouble understanding how I can reference it and use it in a simple test.rb file.

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

I would like to be able to call strip_tags("<b>lol</b>") => "lol"

8 Answers 8

33

The question is quite old, but I had the same problem recently. I found a simple solution: gem sanitize. It's light, works fine and has additional options if you need them.

Sanitize.clean("<b>lol</b>") #=> "lol"
25

ActiveSupport is the only Rails framework that supports cherry-picking individual components. The other frameworks, including ActionView, must be required en-masse:

require 'action_view'

Note that this require won't necessarily load all of ActionView. Barring situations where thread-safety requires that autoloads happen eagerly, it merely sets up autoloads and requires common dependencies. That means that following the require, if you reference, e.g. ActionView::Helpers::SanitizeHelper, it will cause action_view/helpers /sanitize_helper.rb to be required.

Therefore the correct, supported way to accomplish what you desire using ActionView is the following:

require 'action_view'

class Test < Test::Unit::TestCase # or whatever
  include ActionView::Helpers::SanitizeHelper

  def my_test
    assert_equal "lol", strip_tags("<b>lol</b>")
  end
end

This isn't well-documented; I based this answer primarily off of the discussion on this issue.

3
  • Do you need to explicitly require action_view? in which Rails version? I don't recall having needed at least for Rails 2.3.x
    – tokland
    Feb 14, 2012 at 9:53
  • 1
    The question is about using strip_tags outside of Rails, so, yes, you do need to include it.
    – John
    Feb 14, 2012 at 15:27
  • oh, I see, indeed the OP does not say he's using Rails.
    – tokland
    Feb 14, 2012 at 16:04
18

I believe this should be enough:

"<b>lol</b>".gsub(/<[^>]*>/ui,'') #=> lol

You can use Nokogiri as well:

require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML("<b>lol</b>")
doc.text #=> "lol"

You still can go with the Rails one by doing something like:

require 'rubygems'
require 'action_view'

class Foo
  include ActionView::Helpers::SanitizeHelper

  def test
    strip_tags("<b>lol</b>")
  end
end

f = Foo.new
puts f.test #=> lol
4
  • 1
    You never know what browsers will do if you just put an opening pointing bracket without ever closing it; I definitely wouldn't assume that the gsub solution is safe unless you put an extra .gsub(/[<>]/, '') at the end. (And even then I won't vouch for it.)
    – Jo Liss
    Dec 4, 2010 at 21:12
  • 2
    +1 for the Nokogiri comment. That works perfectly for my needs. Aug 7, 2012 at 16:03
  • Worth a note, nokogiri is a HUGE library that usually compiles from source...might be a little overkill to use the entire parsing library for just this, when Rails is shipping with plenty of tricks inside the box.
    – jahrichie
    Apr 3, 2015 at 18:18
  • Do not write you own sanitizers. They will always fail in some corner-cases and hilarity (i.e. hacking) ensues! Use proper libraries.
    – Chris
    Apr 23, 2019 at 13:21
15

If you don't use it very often, then you can use:

ActionView::Base.full_sanitizer.sanitize(your_html_string)

else you can define a method in test_helper.rb file like:

def strip_html_tags(string)
    ActionView::Base.full_sanitizer.sanitize(string)
end

And then in your test.rb file, use this like:

strip_html_tags(your_html_string)
8

The question is quite old, but you can call it in your test.rb like this:

ActionController::Base.helpers.strip_tags("<b>lol</b>") => "lol"
1

With this example:

"&lt;p&gt;<i>example</i>&lt;/p&gt;"

This helped me:

ActionView::Base.full_sanitizer.sanitize(Nokogiri::HTML(example).text)

Output:

example
2
-1

Ideally you would require and include ActionView::Helpers::SanitizeHelper but there are several dependencies that don't get included when you do that. You can require them yourself to be able to use strip_tags.

require 'erb'
require 'active_support'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/string/encoding'
require 'action_view/helpers/capture_helper'
require 'action_view/helpers/sanitize_helper'

include ActionView::Helpers::SanitizeHelper

strip_tags("<b>lol</b>") # => "lol"

This is assuming you have rails 3 gems installed.

1
  • ActionView doesn't support cherry-picking requires -- that's why this approach ends up being so gross. See my answer for further details.
    – John
    Dec 29, 2011 at 1:51
-1
HTML::FullSanitizer.new.sanitize('<b>lol</b>') # => "lol"

Your Answer

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

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