Skip to content

Instantly share code, notes, and snippets.

@dstollie
Created March 27, 2015 10:40
Show Gist options
  • Save dstollie/ef8131cbfac2600441a0 to your computer and use it in GitHub Desktop.
Save dstollie/ef8131cbfac2600441a0 to your computer and use it in GitHub Desktop.
Simple purify method for meteor which uses cristo-rabani's package: https://github.com/cristo-rabani/meteor-universe-html-purifier
purify = function(text, allowedTags, allowedAttributes) {
if(!allowedTags) allowedTags = ['b', 'i', 'strong', 'em', 'ol', 'ul', 'li', 'p', 'span', 'a', 'img', 'br'];
if(!allowedAttributes) allowedAttributes = ['href', 'src'];
var disallowedTags = ['b', 'i', 'strong', 'em', 'blockquote', 'ol', 'ul', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'p', 'span', 'pre', 'a', 'u', 'img', 'br', 'table'];
// Check the difference between the default enabled tags and the allowed tags
var disallowedTags = _.difference(disallowedTags, allowedTags);
// Don't allow extra attributes to an element
UniHTML.setNewAllowedAttributes(allowedAttributes, 'all_elements');
// Purify the answer text and stripe disallowed tags
var purified = UniHTML.purify(text, {
withoutTags: disallowedTags
});
return purified;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment