Skip to content

Instantly share code, notes, and snippets.

@mwlang
Created May 1, 2015 03:46
Show Gist options
  • Save mwlang/5ac24295275242844511 to your computer and use it in GitHub Desktop.
Save mwlang/5ac24295275242844511 to your computer and use it in GitHub Desktop.
HTML sanitize while preserving entities
def sanitize_html text
escaped_text = text.gsub("&", "%AMP%")
sanitized = Sanitize.fragment(escaped_text, Sanitize::Config::BASIC).split("\n")
while sanitized.size > 0 && sanitized[0].blank?
sanitized.shift
end
while sanitized.size > 0 && sanitized[-1].blank?
sanitized.pop
end
return sanitized.join("\n").strip.gsub("%AMP%", '&')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment