From 6f000535111f77d01fb76ee9b3d2c5e6199ca7c8 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 11 Mar 2019 05:08:46 +0100 Subject: [PATCH] Add stubs for bleach (#2709) * Add stubs for bleach * Support Python 2 for bleach * Add missing imports --- third_party/2and3/bleach/__init__.pyi | 29 +++++++++++++++++++++ third_party/2and3/bleach/callbacks.pyi | 6 +++++ third_party/2and3/bleach/linkifier.pyi | 30 ++++++++++++++++++++++ third_party/2and3/bleach/sanitizer.pyi | 35 ++++++++++++++++++++++++++ third_party/2and3/bleach/utils.pyi | 8 ++++++ 5 files changed, 108 insertions(+) create mode 100644 third_party/2and3/bleach/__init__.pyi create mode 100644 third_party/2and3/bleach/callbacks.pyi create mode 100644 third_party/2and3/bleach/linkifier.pyi create mode 100644 third_party/2and3/bleach/sanitizer.pyi create mode 100644 third_party/2and3/bleach/utils.pyi diff --git a/third_party/2and3/bleach/__init__.pyi b/third_party/2and3/bleach/__init__.pyi new file mode 100644 index 000000000..d90015c07 --- /dev/null +++ b/third_party/2and3/bleach/__init__.pyi @@ -0,0 +1,29 @@ +from typing import List, Any, Optional, Text + +from bleach.linkifier import DEFAULT_CALLBACKS as DEFAULT_CALLBACKS, Linker as Linker +from bleach.sanitizer import ( + ALLOWED_ATTRIBUTES as ALLOWED_ATTRIBUTES, + ALLOWED_PROTOCOLS as ALLOWED_PROTOCOLS, + ALLOWED_STYLES as ALLOWED_STYLES, + ALLOWED_TAGS as ALLOWED_TAGS, + Cleaner as Clear, +) + +from .linkifier import _Callback + +__releasedate__: Text +__version__: Text +VERSION: Any # packaging.version.Version + +def clean( + text: Text, + tags: List[Text] = ..., + attributes: Any = ..., + styles: List[Text] = ..., + protocols: List[Text] = ..., + strip: bool = ..., + strip_comments: bool = ..., +) -> Text: ... +def linkify( + text: Text, callbacks: List[_Callback] = ..., skip_tags: Optional[List[Text]] = ..., parse_email: bool = ... +) -> Text: ... diff --git a/third_party/2and3/bleach/callbacks.pyi b/third_party/2and3/bleach/callbacks.pyi new file mode 100644 index 000000000..25c5c01e3 --- /dev/null +++ b/third_party/2and3/bleach/callbacks.pyi @@ -0,0 +1,6 @@ +from typing import MutableMapping, Any, Text + +_Attrs = MutableMapping[Any, Text] + +def nofollow(attrs: _Attrs, new: bool = ...) -> _Attrs: ... +def target_blank(attrs: _Attrs, new: bool = ...) -> _Attrs: ... diff --git a/third_party/2and3/bleach/linkifier.pyi b/third_party/2and3/bleach/linkifier.pyi new file mode 100644 index 000000000..f7e746ca2 --- /dev/null +++ b/third_party/2and3/bleach/linkifier.pyi @@ -0,0 +1,30 @@ +from typing import Any, List, MutableMapping, Protocol, Pattern, Iterable, Optional, Text + +_Attrs = MutableMapping[Any, Text] + +class _Callback(Protocol): + def __call__(self, attrs: _Attrs, new: bool = ...) -> _Attrs: ... + +DEFAULT_CALLBACKS: List[_Callback] + +TLDS: List[Text] + +def build_url_re(tlds: Iterable[Text] = ..., protocols: Iterable[Text] = ...) -> Pattern[Text]: ... + +URL_RE: Pattern[Text] +PHOTO_RE: Pattern[Text] +EMAIL_RE: Pattern[Text] + +class Linker(object): + def __init__( + self, + callbacks: List[_Callback] = ..., + skip_tags: Optional[List[Text]] = ..., + parse_email: bool = ..., + url_re: Pattern[Text] = ..., + email_re: Pattern[Text] = ..., + ) -> None: ... + def linkify(self, text: Text) -> Text: ... + +class LinkifyFilter(object): # TODO: derives from html5lib.Filter + def __getattr__(self, item: str) -> Any: ... # incomplete diff --git a/third_party/2and3/bleach/sanitizer.pyi b/third_party/2and3/bleach/sanitizer.pyi new file mode 100644 index 000000000..9f21e951d --- /dev/null +++ b/third_party/2and3/bleach/sanitizer.pyi @@ -0,0 +1,35 @@ +from typing import List, Dict, Any, Optional, Type, Pattern, Union, Callable, Container, Text + +ALLOWED_TAGS: List[Text] +ALLOWED_ATTRIBUTES: Dict[Text, List[Text]] +ALLOWED_STYLES: List[Text] +ALLOWED_PROTOCOLS: List[Text] + +INVISIBLE_CHARACTERS: Text +INVISIBLE_CHARACTERS_RE: Pattern[Text] +INVISIBLE_REPLACEMENT_CHAR: Text + +# A html5lib Filter class +_Filter = Any + +class Cleaner(object): + def __init__( + self, + tags: List[Text] = ..., + attributes: Any = ..., + styles: List[Text] = ..., + protocols: List[Text] = ..., + strip: bool = ..., + strip_comments: bool = ..., + filters: Optional[List[Type[_Filter]]] = ..., + ) -> None: ... + def clean(self, text: Text) -> Text: ... + +_AttributeFilter = Callable[[Text, Text, Text], bool] +_AttributeDict = Dict[Text, Union[Container[Text], _AttributeFilter]] + +def attribute_filter_factory(attributes: Union[_AttributeFilter, _AttributeDict, List[Text]]) -> _AttributeFilter: ... + +class BleachSanitizerFilter(object): # TODO: derives from html5lib.sanitizer.Filter + + def __getattr__(self, item: str) -> Any: ... # incomplete diff --git a/third_party/2and3/bleach/utils.pyi b/third_party/2and3/bleach/utils.pyi new file mode 100644 index 000000000..984c554bd --- /dev/null +++ b/third_party/2and3/bleach/utils.pyi @@ -0,0 +1,8 @@ +from collections import OrderedDict +from typing import overload, Mapping, Any, Text + +@overload +def alphabetize_attributes(attrs: None) -> None: ... +@overload +def alphabetize_attributes(attrs: Mapping[Any, Text]) -> OrderedDict[Any, Text]: ... +def force_unicode(text: Text) -> Text: ...