Added a new hook to add new element rules to the Whitelister.

This commit is contained in:
jmiguelv 2014-06-19 10:48:51 +01:00 committed by Matt Westcott
parent 0ea934566f
commit 2afb13667f

View file

@ -5,6 +5,8 @@ specific rules.
from bs4 import BeautifulSoup, NavigableString, Tag
from urlparse import urlparse
from wagtail.wagtailadmin import hooks
ALLOWED_URL_SCHEMES = ['', 'http', 'https', 'ftp', 'mailto', 'tel']
@ -65,7 +67,8 @@ class Whitelister(object):
'h6': allow_without_attributes,
'hr': allow_without_attributes,
'i': allow_without_attributes,
'img': attribute_rule({'src': check_url, 'width': True, 'height': True, 'alt': True}),
'img': attribute_rule({'src': check_url, 'width': True, 'height': True,
'alt': True}),
'li': allow_without_attributes,
'ol': allow_without_attributes,
'p': allow_without_attributes,
@ -77,7 +80,12 @@ class Whitelister(object):
@classmethod
def clean(cls, html):
"""Clean up an HTML string to contain just the allowed elements / attributes"""
"""Clean up an HTML string to contain just the allowed elements /
attributes"""
for fn in hooks.get_hooks('construct_whitelister_element_rules'):
cls.element_rules = dict(
cls.element_rules.items() + fn().items())
doc = BeautifulSoup(html, 'lxml')
cls.clean_node(doc, doc)
return unicode(doc)