From e52c23981add535fc8ce0ee010e144ae846dae86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Fredrik=20Ki=C3=A6r?= Date: Wed, 4 May 2022 11:57:52 +0200 Subject: [PATCH] Add compatibility with `bleach >= 5` --- CHANGELOG.md | 3 ++ setup.py | 3 +- webviz_config/generic_plugins/_markdown.py | 44 +++++++++++++++------- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5793a937..2c6ecc98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [UNRELEASED] - YYYY-MM-DD +### Fixed +- [#588](https://github.com/equinor/webviz-config/pull/588) - Added compatibility with upstream dependency `bleach >= 5`. + ## [0.3.9] - 2022-02-09 ### Added diff --git a/setup.py b/setup.py index 62033145..18d09b66 100644 --- a/setup.py +++ b/setup.py @@ -77,7 +77,8 @@ def get_long_description() -> str: ], }, install_requires=[ - "bleach>=3.1", + "bleach>=3.1; python_version<'3.7'", + "bleach[css]>=5; python_version>='3.7'", "cryptography>=2.4", "dash>=2.0", "dash-pivottable>=0.0.2", diff --git a/webviz_config/generic_plugins/_markdown.py b/webviz_config/generic_plugins/_markdown.py index 5dc196a4..4e59f687 100644 --- a/webviz_config/generic_plugins/_markdown.py +++ b/webviz_config/generic_plugins/_markdown.py @@ -166,21 +166,39 @@ def __init__(self, markdown_file: Path): self.markdown_file = markdown_file - self.html = bleach.clean( - markdown.markdown( - get_path(self.markdown_file).read_text(), - extensions=[ - "fenced_code", - "tables", - "sane_lists", - _WebvizMarkdownExtension(base_path=markdown_file.parent), - ], - ), - tags=Markdown.ALLOWED_TAGS, - attributes=Markdown.ALLOWED_ATTRIBUTES, - styles=Markdown.ALLOWED_STYLES, + html_from_markdown = markdown.markdown( + get_path(self.markdown_file).read_text(), + extensions=[ + "fenced_code", + "tables", + "sane_lists", + _WebvizMarkdownExtension(base_path=markdown_file.parent), + ], ) + try: + self.html = bleach.clean( # pylint: disable=unexpected-keyword-arg + html_from_markdown, + tags=Markdown.ALLOWED_TAGS, + attributes=Markdown.ALLOWED_ATTRIBUTES, + styles=Markdown.ALLOWED_STYLES, + ) + except TypeError: + # styles not present in bleach >= 5. We can remove + # this try/except when dropping Python 3.6 support. + from bleach.css_sanitizer import ( # pylint: disable=import-outside-toplevel + CSSSanitizer, + ) + + css_sanitizer = CSSSanitizer(allowed_css_properties=Markdown.ALLOWED_STYLES) + + self.html = bleach.clean( + html_from_markdown, + tags=Markdown.ALLOWED_TAGS, + attributes=Markdown.ALLOWED_ATTRIBUTES, + css_sanitizer=css_sanitizer, + ) + # Workaround for upstream issue https://github.com/plotly/dash-core-components/issues/746, # where we convert void html tags from to . self.html = re.sub("", r"", self.html)