2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
Functions for working with "safe strings": strings that can be displayed safely
|
|
|
|
without further escaping in HTML. Marking something as a "safe string" means
|
|
|
|
that the producer of the string has already turned characters that should not
|
|
|
|
be interpreted by the HTML engine (e.g. '<') into the appropriate entities.
|
|
|
|
"""
|
|
|
|
from django.utils.functional import curry, Promise
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
class EscapeData(object):
|
|
|
|
pass
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
class EscapeString(bytes, EscapeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
A string that should be HTML-escaped when output.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
class EscapeUnicode(six.text_type, EscapeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
A unicode object that should be HTML-escaped when output.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SafeData(object):
|
|
|
|
pass
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
class SafeString(bytes, SafeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
A string subclass that has been specifically marked as "safe" (requires no
|
|
|
|
further escaping) for HTML output purposes.
|
|
|
|
"""
|
|
|
|
def __add__(self, rhs):
|
|
|
|
"""
|
|
|
|
Concatenating a safe string with another safe string or safe unicode
|
|
|
|
object is safe. Otherwise, the result is no longer safe.
|
|
|
|
"""
|
2007-12-03 04:17:10 +08:00
|
|
|
t = super(SafeString, self).__add__(rhs)
|
2007-11-14 20:58:53 +08:00
|
|
|
if isinstance(rhs, SafeUnicode):
|
2007-12-03 04:17:10 +08:00
|
|
|
return SafeUnicode(t)
|
2007-11-14 20:58:53 +08:00
|
|
|
elif isinstance(rhs, SafeString):
|
2007-12-03 04:17:10 +08:00
|
|
|
return SafeString(t)
|
|
|
|
return t
|
2012-07-20 20:48:51 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def _proxy_method(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Wrap a call to a normal unicode method up so that we return safe
|
|
|
|
results. The method that is being wrapped is passed in the 'method'
|
|
|
|
argument.
|
|
|
|
"""
|
|
|
|
method = kwargs.pop('method')
|
|
|
|
data = method(self, *args, **kwargs)
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(data, bytes):
|
2007-11-14 20:58:53 +08:00
|
|
|
return SafeString(data)
|
|
|
|
else:
|
|
|
|
return SafeUnicode(data)
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
decode = curry(_proxy_method, method=bytes.decode)
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
class SafeUnicode(six.text_type, SafeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
A unicode subclass that has been specifically marked as "safe" for HTML
|
|
|
|
output purposes.
|
|
|
|
"""
|
|
|
|
def __add__(self, rhs):
|
|
|
|
"""
|
|
|
|
Concatenating a safe unicode object with another safe string or safe
|
|
|
|
unicode object is safe. Otherwise, the result is no longer safe.
|
|
|
|
"""
|
2007-12-03 04:17:10 +08:00
|
|
|
t = super(SafeUnicode, self).__add__(rhs)
|
2007-11-14 20:58:53 +08:00
|
|
|
if isinstance(rhs, SafeData):
|
2007-12-03 04:17:10 +08:00
|
|
|
return SafeUnicode(t)
|
|
|
|
return t
|
2012-07-20 20:48:51 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def _proxy_method(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Wrap a call to a normal unicode method up so that we return safe
|
|
|
|
results. The method that is being wrapped is passed in the 'method'
|
|
|
|
argument.
|
|
|
|
"""
|
|
|
|
method = kwargs.pop('method')
|
|
|
|
data = method(self, *args, **kwargs)
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(data, bytes):
|
2007-11-14 20:58:53 +08:00
|
|
|
return SafeString(data)
|
|
|
|
else:
|
|
|
|
return SafeUnicode(data)
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
encode = curry(_proxy_method, method=six.text_type.encode)
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
def mark_safe(s):
|
|
|
|
"""
|
|
|
|
Explicitly mark a string as safe for (HTML) output purposes. The returned
|
|
|
|
object can be used everywhere a string or unicode object is appropriate.
|
|
|
|
|
|
|
|
Can be called multiple times on a single string.
|
|
|
|
"""
|
|
|
|
if isinstance(s, SafeData):
|
|
|
|
return s
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_str):
|
2007-11-14 20:58:53 +08:00
|
|
|
return SafeString(s)
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(s, (six.text_type, Promise)):
|
2007-11-14 20:58:53 +08:00
|
|
|
return SafeUnicode(s)
|
2012-07-20 20:48:51 +08:00
|
|
|
return SafeString(bytes(s))
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
def mark_for_escaping(s):
|
|
|
|
"""
|
|
|
|
Explicitly mark a string as requiring HTML escaping upon output. Has no
|
|
|
|
effect on SafeData subclasses.
|
|
|
|
|
|
|
|
Can be called multiple times on a single string (the resulting escaping is
|
|
|
|
only applied once).
|
|
|
|
"""
|
|
|
|
if isinstance(s, (SafeData, EscapeData)):
|
|
|
|
return s
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_str):
|
2007-11-14 20:58:53 +08:00
|
|
|
return EscapeString(s)
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(s, (six.text_type, Promise)):
|
2007-11-14 20:58:53 +08:00
|
|
|
return EscapeUnicode(s)
|
2012-07-20 20:48:51 +08:00
|
|
|
return EscapeString(bytes(s))
|
2007-11-14 20:58:53 +08:00
|
|
|
|