Skip to content

Instantly share code, notes, and snippets.

@scratchoo
Forked from AleksandrKudashkin/sanitize.rb
Created November 30, 2020 11:29
Show Gist options
  • Save scratchoo/f0043902a67c74cef3b366b756ef1afb to your computer and use it in GitHub Desktop.
Save scratchoo/f0043902a67c74cef3b366b756ef1afb to your computer and use it in GitHub Desktop.
Loofah custom scrubber for youtube and vimeo iframes
class CustomScrubber < Loofah::Scrubber
ALLOWED_IFRAME_ATTRS = %w[allowfullscreen frameborder height src width].freeze
ALLOWED_VIDEO_REGEX = %r{\A(?:https?:)?//(?:www\.)?youtube|vimeo(?:-nocookie)?\.com/}
def scrub(node)
if node.name == 'iframe' && node['src'] =~ ALLOWED_VIDEO_REGEX
node.attribute_nodes.each { |a| a.remove unless ALLOWED_IFRAME_ATTRS.include?(a.name) }
return CONTINUE
end
return CONTINUE if html5lib_sanitize(node) == CONTINUE
node.before node.children
node.remove
end
end
# usage
Loofah.fragment(resource).scrub!(CustomScrubber.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment