<s> """<STR_LIT>""" <EOL> from bleach import clean <EOL> from bleach_whitelist import all_styles <EOL> from bleach_whitelist import generally_xss_safe <EOL> from bleach_whitelist import markdown_attrs <EOL> from bleach_whitelist import markdown_tags <EOL> from bleach_whitelist import print_attrs <EOL> from bleach_whitelist import print_tags <EOL> from pyramid . util import DottedNameResolver <EOL> from six import string_types <EOL> from kotti import get_settings <EOL> from kotti . events import objectevent_listeners <EOL> from kotti . events import ObjectInsert <EOL> from kotti . events import ObjectUpdate <EOL> def sanitize ( html , sanitizer ) : <EOL> """<STR_LIT>""" <EOL> sanitized = get_settings ( ) [ '<STR_LIT>' ] [ sanitizer ] ( html ) <EOL> return sanitized <EOL> def xss_protection ( html ) : <EOL> """<STR_LIT>""" <EOL> sanitized = clean ( <EOL> html , <EOL> tags = generally_xss_safe , <EOL> attributes = lambda self , key , value : True , <EOL> styles = all_styles , <EOL> strip = True ) <EOL> return sanitized <EOL> def minimal_html ( html ) : <EOL> """<STR_LIT>""" <EOL> attributes = dict ( zip ( <EOL> markdown_attrs . keys ( ) + print_attrs . keys ( ) , <EOL> markdown_attrs . values ( ) + print_attrs . values ( ) ) ) <EOL> sanitized = clean ( <EOL> html , <EOL> tags = markdown_tags + print_tags , <EOL> attributes = attributes , <EOL> styles = [ ] , <EOL> strip = True ) <EOL> return sanitized <EOL> def no_html ( html ) : <EOL> """<STR_LIT>""" <EOL> sanitized = clean ( <EOL> html , <EOL> tags = [ ] , <EOL> attributes = { } , <EOL> styles = [ ] , <EOL> strip = True ) <EOL> return sanitized <EOL> def _setup_sanitizers ( settings ) : <EOL> if not isinstance ( settings [ '<STR_LIT>' ] , string_types ) : <EOL> return <EOL> sanitizers = { } <EOL> for s in settings [ '<STR_LIT>' ] . split ( ) : <EOL> name , dottedname = s . split ( '<STR_LIT::>' ) <EOL> sanitizers [ name . strip ( ) ] = DottedNameResolver ( ) . resolve ( dottedname ) <EOL> settings [ '<STR_LIT>' ] = sanitizers <EOL> def _setup_listeners ( settings ) : <EOL> for s in settings [ '<STR_LIT>' ] . split ( ) : <EOL> dotted , sanitizers = s . split ( '<STR_LIT::>' ) <EOL> classname , attributename = dotted . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> _class = DottedNameResolver ( ) . resolve ( classname ) <EOL> def _create_handler ( attributename , sanitizers ) : <EOL> def handler ( event ) : <EOL> value = getattr ( event . object , attributename ) <EOL> for sanitizer_name in sanitizers . split ( '<STR_LIT:U+002C>' ) : <EOL> value = settings [ '<STR_LIT>' ] [ sanitizer_name ] ( value ) <EOL> setattr ( event . object , attributename , value ) <EOL> return handler <EOL> objectevent_listeners [ ( ObjectInsert , _class ) ] . append ( <EOL> _create_handler ( attributename , sanitizers ) ) <EOL> objectevent_listeners [ ( ObjectUpdate , _class ) ] . append ( <EOL> _create_handler ( attributename , sanitizers ) ) <EOL> def includeme ( config ) : <EOL> """<STR_LIT>""" <EOL> _setup_sanitizers ( config . registry . settings ) <EOL> _setup_listeners ( config . registry . settings ) </s>
