A str subclass that has been specifically marked as "safe" for HTML output purposes.
| 23 | |
| 24 | |
| 25 | class 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 | |
| 53 | SafeText = SafeString # For backwards compatibility since Django 2.0. |
no outgoing calls