Source code for anza.engine.slot.escape
"""HTML escaping, identical in all three engines: ``&`` ``<`` ``>`` ``"`` ``'``
become ``&`` ``<`` ``>`` ``"`` ``'``. Safe for element
content and quoted attribute values. (``html.escape`` writes ``'``.)"""
_TABLE = str.maketrans({"&": "&", "<": "<", ">": ">", '"': """, "'": "'"})
[docs]
def escape_html(text: str) -> str:
"""HTML-escape ``text`` for element content or a quoted attribute value.
``&`` ``<`` ``>`` ``"`` ``'`` become ``&`` ``<`` ``>``
``"`` ``'``, byte for byte like the Rust and TypeScript engines
(``html.escape`` writes ``'`` instead).
Args:
text: The text to escape.
Returns:
str: The escaped text.
Example:
>>> from anza import escape_html
>>> escape_html('''<a href="x">Tom & Jerry's</a>''')
'<a href="x">Tom & Jerry's</a>'
"""
return text.translate(_TABLE)