Skip to content

Commit

Permalink
Lazily import bleach HTML sanitizer (#6179)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jan 10, 2024
1 parent aa6dd53 commit b8c8c34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 17 additions & 4 deletions panel/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from importlib import import_module
from typing import Any, AnyStr

import bleach
import bokeh
import numpy as np
import param
Expand Down Expand Up @@ -51,7 +50,23 @@

PARAM_NAME_PATTERN = re.compile(r'^.*\d{5}$')

HTML_SANITIZER = bleach.sanitizer.Cleaner(strip=True)
class LazyHTMLSanitizer:
"""
Wraps bleach.sanitizer.Cleaner lazily importing it on the first
call to the clean method.
"""

def __init__(self, **kwargs):
self._cleaner = None
self._kwargs = kwargs

def clean(self, text):
if self._cleaner is None:
import bleach
self._cleaner = bleach.sanitizer.Cleaner(**self._kwargs)
return self._cleaner.clean(text)

HTML_SANITIZER = LazyHTMLSanitizer(strip=True)


def hashable(x):
Expand Down Expand Up @@ -87,8 +102,6 @@ def param_name(name: str) -> str:
return name[:name.index(match[0])] if match else name




def abbreviated_repr(value, max_length=25, natural_breaks=(',', ' ')):
"""
Returns an abbreviated repr for the supplied object. Attempts to
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ requires = [
"bokeh >=3.3.0,<3.4.0",
"pyviz_comms >=0.7.4",
"requests",
"bleach",
"packaging",
"tqdm >=4.48.0",
"markdown",
Expand Down

0 comments on commit b8c8c34

Please sign in to comment.