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
data = cgi.escape(data, quote)
return data.replace("'", "'") if quote else data
else:
import html
if isinstance(data, str):
return html.escape(data, quote=quote)
data = data.replace(b"&", b"&") # Must be done first!
data = data.replace(b"<", b"<")
data = data.replace(b">", b">")
if quote:
data = data.replace(b'"', b""")
data = data.replace(b'\'', b"'")
return data
评论列表
文章目录