class Decontaminator::Fragment
Constants
- NON_CONTENT_TAGS
- WHITESPACE_CONTENT_TAGS
Attributes
html_fragment[R]
Public Class Methods
new(html_fragment)
click to toggle source
# File lib/decontaminator/fragment.rb, line 5 def initialize(html_fragment) @html_fragment = html_fragment end
Public Instance Methods
decontaminate(options = {})
click to toggle source
# File lib/decontaminator/fragment.rb, line 9 def decontaminate(options = {}) blacklisted_tags = NON_CONTENT_TAGS + options.fetch(:blacklist, []) sanitize(Oga.parse_html(html_fragment).children, blacklisted_tags) end
Private Instance Methods
comment?(node)
click to toggle source
# File lib/decontaminator/fragment.rb, line 64 def comment?(node) node.is_a?(Oga::XML::Comment) end
sanitize(node_set, blacklisted_tags)
click to toggle source
# File lib/decontaminator/fragment.rb, line 53 def sanitize(node_set, blacklisted_tags) node_set .reject { |node| comment?(node) || (!text?(node) && blacklisted_tags.include?(node.name)) } .flat_map { |node| [whitespace(node, :prefix), text(node, blacklisted_tags), whitespace(node, :suffix)] } .join end
text(node, blacklisted_tags)
click to toggle source
# File lib/decontaminator/fragment.rb, line 76 def text(node, blacklisted_tags) if text?(node) node.text else sanitize(node.children, blacklisted_tags) end end
text?(node)
click to toggle source
# File lib/decontaminator/fragment.rb, line 60 def text?(node) node.is_a?(Oga::XML::Text) end
whitespace(node, _position)
click to toggle source
# File lib/decontaminator/fragment.rb, line 68 def whitespace(node, _position) if !text?(node) && WHITESPACE_CONTENT_TAGS.include?(node.name) ' ' else '' end end