__init__.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import functools
  2. import re
  3. import string
  4. import sys
  5. import typing as t
  6. if t.TYPE_CHECKING:
  7. import typing_extensions as te
  8. class HasHTML(te.Protocol):
  9. def __html__(self) -> str:
  10. pass
  11. _P = te.ParamSpec("_P")
  12. __version__ = "2.1.3"
  13. _strip_comments_re = re.compile(r"<!--.*?-->", re.DOTALL)
  14. _strip_tags_re = re.compile(r"<.*?>", re.DOTALL)
  15. def _simple_escaping_wrapper(func: "t.Callable[_P, str]") -> "t.Callable[_P, Markup]":
  16. @functools.wraps(func)
  17. def wrapped(self: "Markup", *args: "_P.args", **kwargs: "_P.kwargs") -> "Markup":
  18. arg_list = _escape_argspec(list(args), enumerate(args), self.escape)
  19. _escape_argspec(kwargs, kwargs.items(), self.escape)
  20. return self.__class__(func(self, *arg_list, **kwargs)) # type: ignore[arg-type]
  21. return wrapped # type: ignore[return-value]
  22. class Markup(str):
  23. """A string that is ready to be safely inserted into an HTML or XML
  24. document, either because it was escaped or because it was marked
  25. safe.
  26. Passing an object to the constructor converts it to text and wraps
  27. it to mark it safe without escaping. To escape the text, use the
  28. :meth:`escape` class method instead.
  29. >>> Markup("Hello, <em>World</em>!")
  30. Markup('Hello, <em>World</em>!')
  31. >>> Markup(42)
  32. Markup('42')
  33. >>> Markup.escape("Hello, <em>World</em>!")
  34. Markup('Hello &lt;em&gt;World&lt;/em&gt;!')
  35. This implements the ``__html__()`` interface that some frameworks
  36. use. Passing an object that implements ``__html__()`` will wrap the
  37. output of that method, marking it safe.
  38. >>> class Foo:
  39. ... def __html__(self):
  40. ... return '<a href="/foo">foo</a>'
  41. ...
  42. >>> Markup(Foo())
  43. Markup('<a href="/foo">foo</a>')
  44. This is a subclass of :class:`str`. It has the same methods, but
  45. escapes their arguments and returns a ``Markup`` instance.
  46. >>> Markup("<em>%s</em>") % ("foo & bar",)
  47. Markup('<em>foo &amp; bar</em>')
  48. >>> Markup("<em>Hello</em> ") + "<foo>"
  49. Markup('<em>Hello</em> &lt;foo&gt;')
  50. """
  51. __slots__ = ()
  52. def __new__(
  53. cls, base: t.Any = "", encoding: t.Optional[str] = None, errors: str = "strict"
  54. ) -> "te.Self":
  55. if hasattr(base, "__html__"):
  56. base = base.__html__()
  57. if encoding is None:
  58. return super().__new__(cls, base)
  59. return super().__new__(cls, base, encoding, errors)
  60. def __html__(self) -> "te.Self":
  61. return self
  62. def __add__(self, other: t.Union[str, "HasHTML"]) -> "te.Self":
  63. if isinstance(other, str) or hasattr(other, "__html__"):
  64. return self.__class__(super().__add__(self.escape(other)))
  65. return NotImplemented
  66. def __radd__(self, other: t.Union[str, "HasHTML"]) -> "te.Self":
  67. if isinstance(other, str) or hasattr(other, "__html__"):
  68. return self.escape(other).__add__(self)
  69. return NotImplemented
  70. def __mul__(self, num: "te.SupportsIndex") -> "te.Self":
  71. if isinstance(num, int):
  72. return self.__class__(super().__mul__(num))
  73. return NotImplemented
  74. __rmul__ = __mul__
  75. def __mod__(self, arg: t.Any) -> "te.Self":
  76. if isinstance(arg, tuple):
  77. # a tuple of arguments, each wrapped
  78. arg = tuple(_MarkupEscapeHelper(x, self.escape) for x in arg)
  79. elif hasattr(type(arg), "__getitem__") and not isinstance(arg, str):
  80. # a mapping of arguments, wrapped
  81. arg = _MarkupEscapeHelper(arg, self.escape)
  82. else:
  83. # a single argument, wrapped with the helper and a tuple
  84. arg = (_MarkupEscapeHelper(arg, self.escape),)
  85. return self.__class__(super().__mod__(arg))
  86. def __repr__(self) -> str:
  87. return f"{self.__class__.__name__}({super().__repr__()})"
  88. def join(self, seq: t.Iterable[t.Union[str, "HasHTML"]]) -> "te.Self":
  89. return self.__class__(super().join(map(self.escape, seq)))
  90. join.__doc__ = str.join.__doc__
  91. def split( # type: ignore[override]
  92. self, sep: t.Optional[str] = None, maxsplit: int = -1
  93. ) -> t.List["te.Self"]:
  94. return [self.__class__(v) for v in super().split(sep, maxsplit)]
  95. split.__doc__ = str.split.__doc__
  96. def rsplit( # type: ignore[override]
  97. self, sep: t.Optional[str] = None, maxsplit: int = -1
  98. ) -> t.List["te.Self"]:
  99. return [self.__class__(v) for v in super().rsplit(sep, maxsplit)]
  100. rsplit.__doc__ = str.rsplit.__doc__
  101. def splitlines( # type: ignore[override]
  102. self, keepends: bool = False
  103. ) -> t.List["te.Self"]:
  104. return [self.__class__(v) for v in super().splitlines(keepends)]
  105. splitlines.__doc__ = str.splitlines.__doc__
  106. def unescape(self) -> str:
  107. """Convert escaped markup back into a text string. This replaces
  108. HTML entities with the characters they represent.
  109. >>> Markup("Main &raquo; <em>About</em>").unescape()
  110. 'Main » <em>About</em>'
  111. """
  112. from html import unescape
  113. return unescape(str(self))
  114. def striptags(self) -> str:
  115. """:meth:`unescape` the markup, remove tags, and normalize
  116. whitespace to single spaces.
  117. >>> Markup("Main &raquo;\t<em>About</em>").striptags()
  118. 'Main » About'
  119. """
  120. # Use two regexes to avoid ambiguous matches.
  121. value = _strip_comments_re.sub("", self)
  122. value = _strip_tags_re.sub("", value)
  123. value = " ".join(value.split())
  124. return self.__class__(value).unescape()
  125. @classmethod
  126. def escape(cls, s: t.Any) -> "te.Self":
  127. """Escape a string. Calls :func:`escape` and ensures that for
  128. subclasses the correct type is returned.
  129. """
  130. rv = escape(s)
  131. if rv.__class__ is not cls:
  132. return cls(rv)
  133. return rv # type: ignore[return-value]
  134. __getitem__ = _simple_escaping_wrapper(str.__getitem__)
  135. capitalize = _simple_escaping_wrapper(str.capitalize)
  136. title = _simple_escaping_wrapper(str.title)
  137. lower = _simple_escaping_wrapper(str.lower)
  138. upper = _simple_escaping_wrapper(str.upper)
  139. replace = _simple_escaping_wrapper(str.replace)
  140. ljust = _simple_escaping_wrapper(str.ljust)
  141. rjust = _simple_escaping_wrapper(str.rjust)
  142. lstrip = _simple_escaping_wrapper(str.lstrip)
  143. rstrip = _simple_escaping_wrapper(str.rstrip)
  144. center = _simple_escaping_wrapper(str.center)
  145. strip = _simple_escaping_wrapper(str.strip)
  146. translate = _simple_escaping_wrapper(str.translate)
  147. expandtabs = _simple_escaping_wrapper(str.expandtabs)
  148. swapcase = _simple_escaping_wrapper(str.swapcase)
  149. zfill = _simple_escaping_wrapper(str.zfill)
  150. casefold = _simple_escaping_wrapper(str.casefold)
  151. if sys.version_info >= (3, 9):
  152. removeprefix = _simple_escaping_wrapper(str.removeprefix)
  153. removesuffix = _simple_escaping_wrapper(str.removesuffix)
  154. def partition(self, sep: str) -> t.Tuple["te.Self", "te.Self", "te.Self"]:
  155. l, s, r = super().partition(self.escape(sep))
  156. cls = self.__class__
  157. return cls(l), cls(s), cls(r)
  158. def rpartition(self, sep: str) -> t.Tuple["te.Self", "te.Self", "te.Self"]:
  159. l, s, r = super().rpartition(self.escape(sep))
  160. cls = self.__class__
  161. return cls(l), cls(s), cls(r)
  162. def format(self, *args: t.Any, **kwargs: t.Any) -> "te.Self":
  163. formatter = EscapeFormatter(self.escape)
  164. return self.__class__(formatter.vformat(self, args, kwargs))
  165. def format_map( # type: ignore[override]
  166. self, map: t.Mapping[str, t.Any]
  167. ) -> "te.Self":
  168. formatter = EscapeFormatter(self.escape)
  169. return self.__class__(formatter.vformat(self, (), map))
  170. def __html_format__(self, format_spec: str) -> "te.Self":
  171. if format_spec:
  172. raise ValueError("Unsupported format specification for Markup.")
  173. return self
  174. class EscapeFormatter(string.Formatter):
  175. __slots__ = ("escape",)
  176. def __init__(self, escape: t.Callable[[t.Any], Markup]) -> None:
  177. self.escape = escape
  178. super().__init__()
  179. def format_field(self, value: t.Any, format_spec: str) -> str:
  180. if hasattr(value, "__html_format__"):
  181. rv = value.__html_format__(format_spec)
  182. elif hasattr(value, "__html__"):
  183. if format_spec:
  184. raise ValueError(
  185. f"Format specifier {format_spec} given, but {type(value)} does not"
  186. " define __html_format__. A class that defines __html__ must define"
  187. " __html_format__ to work with format specifiers."
  188. )
  189. rv = value.__html__()
  190. else:
  191. # We need to make sure the format spec is str here as
  192. # otherwise the wrong callback methods are invoked.
  193. rv = string.Formatter.format_field(self, value, str(format_spec))
  194. return str(self.escape(rv))
  195. _ListOrDict = t.TypeVar("_ListOrDict", list, dict)
  196. def _escape_argspec(
  197. obj: _ListOrDict, iterable: t.Iterable[t.Any], escape: t.Callable[[t.Any], Markup]
  198. ) -> _ListOrDict:
  199. """Helper for various string-wrapped functions."""
  200. for key, value in iterable:
  201. if isinstance(value, str) or hasattr(value, "__html__"):
  202. obj[key] = escape(value)
  203. return obj
  204. class _MarkupEscapeHelper:
  205. """Helper for :meth:`Markup.__mod__`."""
  206. __slots__ = ("obj", "escape")
  207. def __init__(self, obj: t.Any, escape: t.Callable[[t.Any], Markup]) -> None:
  208. self.obj = obj
  209. self.escape = escape
  210. def __getitem__(self, item: t.Any) -> "te.Self":
  211. return self.__class__(self.obj[item], self.escape)
  212. def __str__(self) -> str:
  213. return str(self.escape(self.obj))
  214. def __repr__(self) -> str:
  215. return str(self.escape(repr(self.obj)))
  216. def __int__(self) -> int:
  217. return int(self.obj)
  218. def __float__(self) -> float:
  219. return float(self.obj)
  220. # circular import
  221. try:
  222. from ._speedups import escape as escape
  223. from ._speedups import escape_silent as escape_silent
  224. from ._speedups import soft_str as soft_str
  225. except ImportError:
  226. from ._native import escape as escape
  227. from ._native import escape_silent as escape_silent # noqa: F401
  228. from ._native import soft_str as soft_str # noqa: F401