bleach_tags.py 755 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import unicode_literals, print_function, absolute_import

from bleach import clean
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter(is_safe=True)
@stringfilter
def bleach(value):
    """Bleach dangerous html from the input"""

    return mark_safe(
        clean(
            value,
            tags=['h2', 'h3', 'p', 'a', 'div',
                  'strong', 'em', 'i', 'b', 'ul', 'li', 'br', 'ol'],
            attributes={
                '*': ['class'],
                'a': ['href', 'rel', 'target', 'title'],
                'img': ['alt', 'title'],
            },
            strip=True
        )
    )