Created
September 6, 2017 20:06
-
-
Save AleksandrKudashkin/00dcec3654cd398929f035d12b0f07db to your computer and use it in GitHub Desktop.
Loofah custom scrubber for youtube and vimeo iframes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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