__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.4"
  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. # collapse spaces
  118. value = " ".join(self.split())
  119. # Look for comments then tags separately. Otherwise, a comment that
  120. # contains a tag would end early, leaving some of the comment behind.
  121. while True:
  122. # keep finding comment start marks
  123. start = value.find("<!--")
  124. if start == -1:
  125. break
  126. # find a comment end mark beyond the start, otherwise stop
  127. end = value.find("-->", start)
  128. if end == -1:
  129. break
  130. value = f"{value[:start]}{value[end + 3:]}"
  131. # remove tags using the same method
  132. while True:
  133. start = value.find("<")
  134. if start == -1:
  135. break
  136. end = value.find(">", start)
  137. if end == -1:
  138. break
  139. value = f"{value[:start]}{value[end + 1:]}"
  140. return self.__class__(value).unescape()
  141. @classmethod
  142. def escape(cls, s: t.Any) -> "te.Self":
  143. """Escape a string. Calls :func:`escape` and ensures that for
  144. subclasses the correct type is returned.
  145. """
  146. rv = escape(s)
  147. if rv.__class__ is not cls:
  148. return cls(rv)
  149. return rv # type: ignore[return-value]
  150. __getitem__ = _simple_escaping_wrapper(str.__getitem__)
  151. capitalize = _simple_escaping_wrapper(str.capitalize)
  152. title = _simple_escaping_wrapper(str.title)
  153. lower = _simple_escaping_wrapper(str.lower)
  154. upper = _simple_escaping_wrapper(str.upper)
  155. replace = _simple_escaping_wrapper(str.replace)
  156. ljust = _simple_escaping_wrapper(str.ljust)
  157. rjust = _simple_escaping_wrapper(str.rjust)
  158. lstrip = _simple_escaping_wrapper(str.lstrip)
  159. rstrip = _simple_escaping_wrapper(str.rstrip)
  160. center = _simple_escaping_wrapper(str.center)
  161. strip = _simple_escaping_wrapper(str.strip)
  162. translate = _simple_escaping_wrapper(str.translate)
  163. expandtabs = _simple_escaping_wrapper(str.expandtabs)
  164. swapcase = _simple_escaping_wrapper(str.swapcase)
  165. zfill = _simple_escaping_wrapper(str.zfill)
  166. casefold = _simple_escaping_wrapper(str.casefold)
  167. if sys.version_info >= (3, 9):
  168. removeprefix = _simple_escaping_wrapper(str.removeprefix)
  169. removesuffix = _simple_escaping_wrapper(str.removesuffix)
  170. def partition(self, sep: str) -> t.Tuple["te.Self", "te.Self", "te.Self"]:
  171. l, s, r = super().partition(self.escape(sep))
  172. cls = self.__class__
  173. return cls(l), cls(s), cls(r)
  174. def rpartition(self, sep: str) -> t.Tuple["te.Self", "te.Self", "te.Self"]:
  175. l, s, r = super().rpartition(self.escape(sep))
  176. cls = self.__class__
  177. return cls(l), cls(s), cls(r)
  178. def format(self, *args: t.Any, **kwargs: t.Any) -> "te.Self":
  179. formatter = EscapeFormatter(self.escape)
  180. return self.__class__(formatter.vformat(self, args, kwargs))
  181. def format_map( # type: ignore[override]
  182. self, map: t.Mapping[str, t.Any]
  183. ) -> "te.Self":
  184. formatter = EscapeFormatter(self.escape)
  185. return self.__class__(formatter.vformat(self, (), map))
  186. def __html_format__(self, format_spec: str) -> "te.Self":
  187. if format_spec:
  188. raise ValueError("Unsupported format specification for Markup.")
  189. return self
  190. class EscapeFormatter(string.Formatter):
  191. __slots__ = ("escape",)
  192. def __init__(self, escape: t.Callable[[t.Any], Markup]) -> None:
  193. self.escape = escape
  194. super().__init__()
  195. def format_field(self, value: t.Any, format_spec: str) -> str:
  196. if hasattr(value, "__html_format__"):
  197. rv = value.__html_format__(format_spec)
  198. elif hasattr(value, "__html__"):
  199. if format_spec:
  200. raise ValueError(
  201. f"Format specifier {format_spec} given, but {type(value)} does not"
  202. " define __html_format__. A class that defines __html__ must define"
  203. " __html_format__ to work with format specifiers."
  204. )
  205. rv = value.__html__()
  206. else:
  207. # We need to make sure the format spec is str here as
  208. # otherwise the wrong callback methods are invoked.
  209. rv = string.Formatter.format_field(self, value, str(format_spec))
  210. return str(self.escape(rv))
  211. _ListOrDict = t.TypeVar("_ListOrDict", list, dict)
  212. def _escape_argspec(
  213. obj: _ListOrDict, iterable: t.Iterable[t.Any], escape: t.Callable[[t.Any], Markup]
  214. ) -> _ListOrDict:
  215. """Helper for various string-wrapped functions."""
  216. for key, value in iterable:
  217. if isinstance(value, str) or hasattr(value, "__html__"):
  218. obj[key] = escape(value)
  219. return obj
  220. class _MarkupEscapeHelper:
  221. """Helper for :meth:`Markup.__mod__`."""
  222. __slots__ = ("obj", "escape")
  223. def __init__(self, obj: t.Any, escape: t.Callable[[t.Any], Markup]) -> None:
  224. self.obj = obj
  225. self.escape = escape
  226. def __getitem__(self, item: t.Any) -> "te.Self":
  227. return self.__class__(self.obj[item], self.escape)
  228. def __str__(self) -> str:
  229. return str(self.escape(self.obj))
  230. def __repr__(self) -> str:
  231. return str(self.escape(repr(self.obj)))
  232. def __int__(self) -> int:
  233. return int(self.obj)
  234. def __float__(self) -> float:
  235. return float(self.obj)
  236. # circular import
  237. try:
  238. from ._speedups import escape as escape
  239. from ._speedups import escape_silent as escape_silent
  240. from ._speedups import soft_str as soft_str
  241. except ImportError:
  242. from ._native import escape as escape
  243. from ._native import escape_silent as escape_silent # noqa: F401
  244. from ._native import soft_str as soft_str # noqa: F401