misc: add support for bleach 5 (#63694)

This commit is contained in:
Frédéric Péters 2023-11-23 10:24:19 +01:00
parent deb59dbe85
commit 84ea060081
2 changed files with 14 additions and 2 deletions

View File

@ -178,7 +178,7 @@ setup(
'Quixote>=3.0,<3.2',
'django>=3.2',
'psycopg2',
'bleach<5',
'bleach',
'dnspython',
'gadjo>=0.53',
'django-ckeditor<4.5.4',

View File

@ -2615,13 +2615,25 @@ class WysiwygTextWidget(TextWidget):
all_tags.append('style')
if get_publisher().get_site_option('ckeditor-allow-script-tag'):
all_tags.append('script')
try:
from bleach.css_sanitizer import CSSSanitizer
css_sanitizer = CSSSanitizer(allowed_css_properties=self.ALL_STYLES)
kwargs = {
'css_sanitizer': css_sanitizer,
}
except ModuleNotFoundError:
# bleach < 5
kwargs = {'styles': self.ALL_STYLES}
cleaner = Cleaner(
tags=all_tags,
attributes=self.ALL_ATTRS,
styles=self.ALL_STYLES,
strip=True,
strip_comments=False,
filters=[partial(LinkifyFilter, skip_tags=['pre'], parse_email=True)],
**kwargs,
)
self.value = cleaner.clean(self.value)
if self.value.startswith('<br />'):