Class: Rails::Html::LinkSanitizer

Inherits:
Sanitizer
  • Object
show all
Defined in:
lib/rails/html/sanitizer.rb

Overview

Rails::Html::LinkSanitizer

Removes a tags and href attributes leaving only the link text

link_sanitizer = Rails::Html::LinkSanitizer.new link_sanitizer.sanitize(‘<a href=“example.com”>Only the link text will be kept.</a>’) # => Only the link text will be kept.

Constant Summary

Constants inherited from Sanitizer

Sanitizer::VERSION

Instance Method Summary collapse

Methods inherited from Sanitizer

full_sanitizer, link_sanitizer, white_list_sanitizer

Constructor Details

#initializeLinkSanitizer

Returns a new instance of LinkSanitizer.



42
43
44
45
46
# File 'lib/rails/html/sanitizer.rb', line 42

def initialize
  @link_scrubber = TargetScrubber.new
  @link_scrubber.tags = %w(a href)
  @link_scrubber.attributes = %w(href)
end

Instance Method Details

#sanitize(html, options = {}) ⇒ Object



48
49
50
# File 'lib/rails/html/sanitizer.rb', line 48

def sanitize(html, options = {})
  Loofah.scrub_fragment(html, @link_scrubber).to_s
end