MCPcopy
hub / github.com/django/django / SafeString

Class SafeString

django/utils/safestring.py:25–50  ·  view source on GitHub ↗

A str subclass that has been specifically marked as "safe" for HTML output purposes.

Source from the content-addressed store, hash-verified

23
24
25class SafeString(str, SafeData):
26 """
27 A str subclass that has been specifically marked as "safe" for HTML output
28 purposes.
29 """
30
31 __slots__ = ()
32
33 def __add__(self, rhs):
34 """
35 Concatenating a safe string with another safe bytestring or
36 safe string is safe. Otherwise, the result is no longer safe.
37 """
38 if isinstance(rhs, str):
39 t = super().__add__(rhs)
40 if isinstance(rhs, SafeData):
41 t = SafeString(t)
42 return t
43
44 # Give the rhs object a chance to handle the addition, for example if
45 # the rhs object's class implements `__radd__`. More details:
46 # https://docs.python.org/3/reference/datamodel.html#object.__radd__
47 return NotImplemented
48
49 def __str__(self):
50 return self
51
52
53SafeText = SafeString # For backwards compatibility since Django 2.0.

Callers 12

result_headersFunction · 0.90
items_for_resultFunction · 0.90
escapeFunction · 0.90
handle_wordMethod · 0.90
renderMethod · 0.90
renderMethod · 0.90
test_safe_statusMethod · 0.90
test_safestrMethod · 0.90
test_add_strMethod · 0.90
__add__Method · 0.85
mark_safeFunction · 0.85

Calls

no outgoing calls

Tested by 4

test_safe_statusMethod · 0.72
test_safestrMethod · 0.72
test_add_strMethod · 0.72