1 2 3 4 5 6 7 8 9 10 11 12 13 | from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
import html5lib
from html5lib import sanitizer
@register.filter
@stringfilter
def sanitize(value):
p = html5lib.HTMLParser(tokenizer=sanitizer.HTMLSanitizer)
return p.parseFragment(value).toxml()
|
More like this
- TinyMCE Widget by semente 3 years, 8 months ago
- Sanitize HTML filter with tag/attribute whitelist and XSS protection by harrym 3 years, 9 months ago
- Cleanup dirty HTML from a WYSIWYG editor by denis 3 years, 11 months ago
- Replace Paragraph Tags for Flash by blackbrrr 4 years, 9 months ago
- Contact Form by henrikv 6 years, 2 months ago
Comments
Once change I had to make - despite trying to add
after the last line you had, Django was still interpreting the results of this as unsafe and proceeding to escape all the special characters. Which somewhat defeats the purpose of having a smart library like html5lib handle it. I had to add one import
and change your return statement to this:
Thanks for the snippet!
#