Created
May 1, 2015 03:46
-
-
Save mwlang/5ac24295275242844511 to your computer and use it in GitHub Desktop.
HTML sanitize while preserving entities
This file contains 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
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