Class: ReeText::SanitizeHtml

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_text/package/ree_text/functions/sanitize_html.rb

Constant Summary collapse

ALLOWED_TAGS =
Set.new(
  %w(
    strong em b i p code pre tt samp kbd var sub
    sup dfn cite big small address hr br div span
    h1 h2 h3 h4 h5 h6 ul ol li dl dt dd abbr
    acronym a img blockquote del ins
  )
)
ALLOWED_ATTRIBUTES =
Set.new(
  %w(
    href src width height alt cite datetime
    title class name xml:lang abbr
  )
)
DEFAULTS =
{
  tags: nil,
  attributes: nil
}

Instance Method Summary collapse

Instance Method Details

#call(html, prune: false, **opts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ree_lib/packages/ree_text/package/ree_text/functions/sanitize_html.rb', line 51

def call(html, prune: false, **opts)
  options = DEFAULTS.merge(opts)

  tags = if options[:tags]
    remove_safelist_tag_combinations(Set.new(options[:tags]))
  else
    ALLOWED_TAGS
  end

  attributes = options[:attributes] ? Set.new(options[:attributes]) : ALLOWED_ATTRIBUTES

  loofah_fragment = Loofah.fragment(html)

  permit_scrubber = PermitScrubber.new(
    prune: prune,
    tags: tags, 
    attributes: attributes
  )

  loofah_fragment.scrub!(permit_scrubber)

  properly_encode(loofah_fragment, encoding: 'UTF-8')
end