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.
|
|
|
|
"""
|
2016-05-11 00:46:47 +08:00
|
|
|
|
2017-01-26 16:37:07 +08:00
|
|
|
from django.utils.functional import Promise, wraps
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class SafeData:
|
2013-10-15 06:40:52 +08:00
|
|
|
def __html__(self):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return the html representation of a string for interoperability.
|
2013-10-15 06:40:52 +08:00
|
|
|
|
2014-12-24 05:29:01 +08:00
|
|
|
This allows other template engines to understand Django's SafeData.
|
2013-10-15 06:40:52 +08:00
|
|
|
"""
|
|
|
|
return self
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2012-08-18 22:04:06 +08:00
|
|
|
class SafeBytes(bytes, SafeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
2012-08-18 22:04:06 +08:00
|
|
|
A bytes subclass that has been specifically marked as "safe" (requires no
|
2007-11-14 20:58:53 +08:00
|
|
|
further escaping) for HTML output purposes.
|
2017-01-26 16:37:07 +08:00
|
|
|
|
|
|
|
Kept in Django 2.0 for usage by apps supporting Python 2. Shouldn't be used
|
|
|
|
in Django anymore.
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
def __add__(self, rhs):
|
|
|
|
"""
|
2012-08-18 22:04:06 +08:00
|
|
|
Concatenating a safe byte string with another safe byte string or safe
|
2017-01-21 05:04:05 +08:00
|
|
|
string is safe. Otherwise, the result is no longer safe.
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
2017-01-21 21:13:44 +08:00
|
|
|
t = super().__add__(rhs)
|
2012-08-18 22:04:06 +08:00
|
|
|
if isinstance(rhs, SafeText):
|
|
|
|
return SafeText(t)
|
|
|
|
elif isinstance(rhs, SafeBytes):
|
|
|
|
return SafeBytes(t)
|
2007-12-03 04:17:10 +08:00
|
|
|
return t
|
2012-07-20 20:48:51 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
class SafeText(str, SafeData):
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
A str subclass that has been specifically marked as "safe" for HTML output
|
|
|
|
purposes.
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
|
|
|
def __add__(self, rhs):
|
|
|
|
"""
|
2017-01-21 05:04:05 +08:00
|
|
|
Concatenating a safe string with another safe byte string or
|
|
|
|
safe string is safe. Otherwise, the result is no longer safe.
|
2007-11-14 20:58:53 +08:00
|
|
|
"""
|
2017-01-21 21:13:44 +08:00
|
|
|
t = super().__add__(rhs)
|
2007-11-14 20:58:53 +08:00
|
|
|
if isinstance(rhs, SafeData):
|
2012-08-18 22:04:06 +08:00
|
|
|
return SafeText(t)
|
2007-12-03 04:17:10 +08:00
|
|
|
return t
|
2012-07-20 20:48:51 +08:00
|
|
|
|
2017-01-31 02:15:59 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2016-12-01 18:38:01 +08:00
|
|
|
SafeString = SafeText
|
2012-08-18 22:04:06 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2016-06-03 05:11:43 +08:00
|
|
|
def _safety_decorator(safety_marker, func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
return safety_marker(func(*args, **kwargs))
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
def mark_safe(s):
|
|
|
|
"""
|
|
|
|
Explicitly mark a string as safe for (HTML) output purposes. The returned
|
2017-01-20 17:20:53 +08:00
|
|
|
object can be used everywhere a string is appropriate.
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2016-06-03 05:11:43 +08:00
|
|
|
If used on a method as a decorator, mark the returned data as safe.
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
Can be called multiple times on a single string.
|
|
|
|
"""
|
2014-12-24 05:29:01 +08:00
|
|
|
if hasattr(s, '__html__'):
|
2007-11-14 20:58:53 +08:00
|
|
|
return s
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(s, (str, Promise)):
|
2012-08-18 22:04:06 +08:00
|
|
|
return SafeText(s)
|
2016-06-03 05:11:43 +08:00
|
|
|
if callable(s):
|
|
|
|
return _safety_decorator(mark_safe, s)
|
2017-01-26 16:37:07 +08:00
|
|
|
return SafeText(str(s))
|