"""
NBConvert Preprocessor for sanitizing HTML rendering of notebooks.
"""
import warnings
from bleach import ALLOWED_ATTRIBUTES, ALLOWED_TAGS, clean
from traitlets import Any, Bool, List, Set, Unicode
from .base import Preprocessor
_USE_BLEACH_CSS_SANITIZER = False
_USE_BLEACH_STYLES = False
try:
# bleach[css] >=5.0
from bleach.css_sanitizer import ALLOWED_CSS_PROPERTIES as ALLOWED_STYLES
from bleach.css_sanitizer import CSSSanitizer
_USE_BLEACH_CSS_SANITIZER = True
_USE_BLEACH_STYLES = False
except ImportError:
try:
# bleach <5
from bleach import ALLOWED_STYLES # type:ignore[attr-defined, no-redef]
_USE_BLEACH_CSS_SANITIZER = False
_USE_BLEACH_STYLES = True
warnings.warn(
"Support for bleach <5 will be removed in a future version of nbconvert",
DeprecationWarning,
stacklevel=2,
)
except ImportError:
warnings.warn(
"The installed bleach/tinycss2 do not provide CSS sanitization, "
"please upgrade to bleach >=5",
UserWarning,
stacklevel=2,
)
__all__ = ["SanitizeHTML"]
class SanitizeHTML(Preprocessor):
"""A preprocessor to sanitize html."""
# Bleach config.
attributes = Any(
config=True,
default_value=ALLOWED_ATTRIBUTES,
help="Allowed HTML tag attributes",
)
tags = List(
Unicode(),
config=True,
default_value=ALLOWED_TAGS, # type:ignore[arg-type]
help="List of HTML tags to allow",
)
styles = List(
Unicode(),
config=True,
default_value=ALLOWED_STYLES, # type:ignore[arg-type]
help="Allowed CSS styles if