moved local_html_escape into utils, thanks Simone
This commit is contained in:
@@ -359,3 +359,28 @@ def getipaddrinfo(host):
|
||||
and isinstance(addrinfo[4][0], basestring)]
|
||||
except socket.error:
|
||||
return []
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user