Skip to content

Instantly share code, notes, and snippets.

@AleksandrKudashkin
Created September 6, 2017 20:06
Show Gist options
  • Save AleksandrKudashkin/00dcec3654cd398929f035d12b0f07db to your computer and use it in GitHub Desktop.
Save AleksandrKudashkin/00dcec3654cd398929f035d12b0f07db 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