Skip to content

Instantly share code, notes, and snippets.

@kaspth
Last active December 18, 2015 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaspth/5728856 to your computer and use it in GitHub Desktop.
Save kaspth/5728856 to your computer and use it in GitHub Desktop.
A new API proposal for custom HTML scrubbing.
# By switching out the html-scanner lib with Loofah, we can make use of the custom HTML scrubbers within Loofah to get more control over what gets sanitized.
# This could be useful in apps where users submit text content.
# Say Twitter in an alternate universe allows users to format their tweets using some HTML tags. They then need a way to specify what tags are black- and/or whitelisted.
# This is an example of how it could work in a model.
class Comment < ActiveRecord::Base
# block based
# block takes a node
scrubs :body do |node|
node.remove if node.name == "script"
end
# method based
# method is last argument and has a node parameter
scrubs :name, :body, :remove_style_blocks
# list based via a kind option
# options are based on the available scrubbers in Loofah
scrubs :name, kind: :whitelist
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment