Skip to content

Instantly share code, notes, and snippets.

@sjaveed
Created April 23, 2014 07: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 sjaveed/11206222 to your computer and use it in GitHub Desktop.
Save sjaveed/11206222 to your computer and use it in GitHub Desktop.
# Setup HTML::WhiteListSanitizer
ActionView::Base.sanitized_allowed_attributes.add('style')
%w/margin padding padding-left padding-right padding-top padding-bottom list-style/.each do |prop|
ActionView::Base.sanitized_allowed_css_properties.add(prop)
end
original_string = %Q|<h1 style="color: #333 !important; font-family: arial !important; font-size: 45px !important; font-style: normal !important; font-weight: normal !important; margin: 0 0 10px 0 !important; line-height:-0.5px !important;">Test heading;<a href="https://www.google.com" style="color: #F3724E   !important; font-family: arial !important; text-decoration: none !important;"> link text</a>&nbsp;:&nbsp;</h1>|
sanitized_string = ActionView::Base.white_list_sanitizer.sanitize(original_string)
# sanitized_string strips out all the css tags from within the h1. Why? All the properties specified
# in the h1 tag's style attribute are marked as allowed. At the very least the color should've shown
# Turns out it doesn't like getting a negative line-height property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment