Skip to content

Commit

Permalink
Add compatibility with bleach >= 5 (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer authored May 4, 2022
1 parent 8ef7ea9 commit ea7e2d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 33 additions & 13 deletions webviz_config/generic_plugins/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,41 @@ 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( # type: ignore # 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 <tag> to <tag/>.
self.html = re.sub("<img (.*?[^/])>", r"<img \1/>", self.html)
Expand Down

0 comments on commit ea7e2d0

Please sign in to comment.