Skip to content

Instantly share code, notes, and snippets.

@ahx
Created January 20, 2010 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahx/281893 to your computer and use it in GitHub Desktop.
Save ahx/281893 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]
return unless node.name.downcase == 'script'
node.children.each(&:remove)
{ :node => node }
}
))
# => "<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