Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/utils/safestring.py: 53%
22 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2Functions for working with "safe strings": strings that can be displayed safely
3without further escaping in HTML. Marking something as a "safe string" means
4that the producer of the string has already turned characters that should not
5be interpreted by the HTML engine (e.g. '<') into the appropriate entities.
6"""
8from functools import wraps
11class SafeData:
12 def __html__(self):
13 """
14 Return the html representation of a string for interoperability.
16 This allows other template engines to understand Django's SafeData.
17 """
18 return self
21class SafeString(str, SafeData):
22 """
23 A str subclass that has been specifically marked as "safe" for HTML output
24 purposes.
25 """
27 def __add__(self, rhs):
28 """
29 Concatenating a safe string with another safe bytestring or
30 safe string is safe. Otherwise, the result is no longer safe.
31 """
32 t = super().__add__(rhs)
33 if isinstance(rhs, SafeData):
34 return SafeString(t)
35 return t
37 def __str__(self):
38 return self
41SafeText = SafeString # For backwards compatibility since Django 2.0.
44def _safety_decorator(safety_marker, func):
45 @wraps(func)
46 def wrapped(*args, **kwargs):
47 return safety_marker(func(*args, **kwargs))
49 return wrapped
52def mark_safe(s):
53 """
54 Explicitly mark a string as safe for (HTML) output purposes. The returned
55 object can be used everywhere a string is appropriate.
57 If used on a method as a decorator, mark the returned data as safe.
59 Can be called multiple times on a single string.
60 """
61 if hasattr(s, "__html__"): 61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never true
62 return s
63 if callable(s): 63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never true
64 return _safety_decorator(mark_safe, s)
65 return SafeString(s)