__init__.py 11 KB

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