index.rst 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. .. currentmodule:: markupsafe
  2. MarkupSafe
  3. ==========
  4. MarkupSafe escapes characters so text is safe to use in HTML and XML.
  5. Characters that have special meanings are replaced so that they display
  6. as the actual characters. This mitigates injection attacks, meaning
  7. untrusted user input can safely be displayed on a page.
  8. The :func:`escape` function escapes text and returns a :class:`Markup`
  9. object. The object won't be escaped anymore, but any text that is used
  10. with it will be, ensuring that the result remains safe to use in HTML.
  11. >>> from markupsafe import escape
  12. >>> hello = escape('<em>Hello</em>')
  13. >>> hello
  14. Markup('&lt;em&gt;Hello&lt;/em&gt;')
  15. >>> escape(hello)
  16. Markup('&lt;em&gt;Hello&lt;/em&gt;')
  17. >>> hello + ' <strong>World</strong>'
  18. Markup('&lt;em&gt;Hello&lt;/em&gt; &lt;strong&gt;World&lt;/strong&gt;')
  19. .. note::
  20. The docs assume you're using Python 3. The terms "text" and "string"
  21. refer to the :class:`str` class. In Python 2, this would be the
  22. ``unicode`` class instead.
  23. Installing
  24. ----------
  25. Install and update using `pip`_:
  26. .. code-block:: text
  27. pip install -U MarkupSafe
  28. .. _pip: https://pip.pypa.io/en/stable/quickstart/
  29. Table of Contents
  30. -----------------
  31. .. toctree::
  32. :maxdepth: 2
  33. escaping
  34. html
  35. formatting
  36. license
  37. changes