Skip to content

Instantly share code, notes, and snippets.

@rgrove
Forked from ahx/gist:281893
Created January 20, 2010 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrove/282168 to your computer and use it in GitHub Desktop.
Save rgrove/282168 to your computer and use it in GitHub Desktop.
# remove content of script tags using Sanitize
require 'sanitize'
html = '<p>Do not<script>fail();</script> kill the <a href="/cats/42">cat</a>.</p>'
Sanitize.clean(html, Sanitize::Config::BASIC.merge(
:transformers => lambda { |env|
node = env[:node]
if node.name.downcase == 'script'
node.unlink
return {}
end
}
))
# => "<p>Do not kill the <a href=\"/cat/42\" rel=\"nofollow\">cat</a>.</p>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment