Module | Loofah::TextBehavior |
In: |
lib/loofah/instance_methods.rb
|
Overrides text in HTML::Document and HTML::DocumentFragment, and mixes in to_text.
Returns a plain-text version of the markup contained by the document, with HTML entities encoded.
This method is significantly faster than to_text, but isn‘t clever about whitespace around block elements.
Loofah.document("<h1>Title</h1><div>Content</div>").text # => "TitleContent"
By default, the returned text will have HTML entities escaped. If you want unescaped entities, and you understand that the result is unsafe to render in a browser, then you can pass an argument as shown:
frag = Loofah.fragment("<script>alert('EVIL');</script>") # ok for browser: frag.text # => "<script>alert('EVIL');</script>" # decidedly not ok for browser: frag.text(:encode_special_chars => false) # => "<script>alert('EVIL');</script>"