Did I find the right examples for you? yes no

All Samples(2)  |  Call(1)  |  Derive(0)  |  Import(1)

        def bleach(text, allowed_tags, allowed_attributes={}, allowed_styles={}, strip=False):
    def wildcard_match(patterns, string):
        return any([re.match(pattern.replace('*', '.*'), string) for pattern in patterns if '*' in pattern])

    doc = html.fragment_fromstring(text, create_parent=True)
    for element in doc.iter():
        if not isinstance(element.tag, basestring):
        tag = element.tag.lower()
        if tag in allowed_tags:
            for attrib in element.attrib.keys():
                attrib = attrib.lower()
                if isinstance(allowed_attributes, dict):
                    attribs = allowed_attributes.get('*', []) + allowed_attributes.get(tag, [])
                    attribs = allowed_attributes
                if attrib not in attribs and not wildcard_match(attribs, attrib):
                    del element.attrib[attrib]
                elif attrib == 'style':
                    style = element.attrib['style']
                    lines = [line for line in style.split(';') if ':' in line]
                    properties = {}
                    for line in lines:
                        property, value = [word.strip().lower() for word in line.split(':')]
                        if isinstance(allowed_styles, dict):
                            props = allowed_styles.get('*', []) + allowed_styles.get(tag, [])
                            props = allowed_styles
                        if property in props or wildcard_match(props, property):
                            properties[property] = value
                    styles = [u'%s: %s;' % (property, value) for property, value in properties.items()]
                    if styles:
                        element.attrib['style'] = ' '.join(styles)
                        del element.attrib['style']
        elif element.getparent() is not None:
            if strip:
                attribs = "".join(u' %s="%s"' % (attrib, value) for attrib, value in element.attrib.items())
                text = element.text.strip() if element.text else ''
                tail = element.tail.strip() if element.tail else ''
                text_dict = {'tag': tag, 'attribs': attribs, 'text': text, 'tail': tail}
                if tag in SELF_CLOSING_TAGS:
                    element.text = u'<%(tag)s%(attribs)s />' % text_dict
                    element.text = u'<%(tag)s%(attribs)s>%(text)s' % text_dict
                    element.tail = u'%(tail)s' % text_dict
    return _tostring(doc)

src/d/j/django-form-extensions-0.1.2b4/form_extensions/fields.py   django-form-extensions(Download)
from django.core.exceptions import ValidationError
from django.core import validators
from form_extensions.utils import bleach, replace_elements, remove_empty_paragraphs
import re
    def clean(self, value):
        value = replace_elements(value, self.element_replacements)
        value = remove_empty_paragraphs(value)
        value = bleach(value, self.allowed_tags, self.allowed_attributes, self.allowed_styles)
        return super(HTMLField, self).clean(value)