28 lines
852 B
Python
28 lines
852 B
Python
import sys
|
|
|
|
PY2 = sys.version_info[0] == 2
|
|
|
|
def local_html_escape(data, quote=False):
|
|
"""
|
|
Works with bytes.
|
|
Replace special characters "&", "<" and ">" to HTML-safe sequences.
|
|
If the optional flag quote is true (the default), the quotation mark
|
|
characters, both double quote (") and single quote (') characters are also
|
|
translated.
|
|
"""
|
|
if PY2:
|
|
import cgi
|
|
s = cgi.escape(data, quote)
|
|
return s.replace("'", "'") if quote else s
|
|
else:
|
|
import html
|
|
if isinstance(s, str):
|
|
return html.escape(s, quote=quote)
|
|
s = s.replace(b"&", b"&") # Must be done first!
|
|
s = s.replace(b"<", b"<")
|
|
s = s.replace(b">", b">")
|
|
if quote:
|
|
s = s.replace(b'"', b""")
|
|
s = s.replace(b'\'', b"'")
|
|
return s
|