diff options
| author | Unai Zalakain <unai@gisa-elkartea.org> | 2013-10-15 00:40:52 +0200 |
|---|---|---|
| committer | Unai Zalakain <unai@gisa-elkartea.org> | 2013-10-15 00:42:42 +0200 |
| commit | af64429b991471b7a441e133b5b7d29070984f24 (patch) | |
| tree | c425d99ae3e8ee7c4b1d856010c577ae27021b27 /django | |
| parent | ef22d512b54cbf0b5f76151bc760391d839fbf72 (diff) | |
Fixed #7261 -- support for __html__ for library interoperability
The idea is that if an object implements __html__ which returns a string this is
used as HTML representation (eg: on escaping). If the object is a str or unicode
subclass and returns itself the object is a safe string type.
This is an updated patch based on jbalogh and ivank patches.
Diffstat (limited to 'django')
| -rw-r--r-- | django/utils/html.py | 4 | ||||
| -rw-r--r-- | django/utils/safestring.py | 8 |
2 files changed, 9 insertions, 3 deletions
diff --git a/django/utils/html.py b/django/utils/html.py index 75eff0083d..825f139070 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -65,8 +65,8 @@ def conditional_escape(text): """ Similar to escape(), except that it doesn't operate on pre-escaped strings. """ - if isinstance(text, SafeData): - return text + if hasattr(text, '__html__'): + return text.__html__() else: return escape(text) diff --git a/django/utils/safestring.py b/django/utils/safestring.py index bec59385f7..aee6427b3c 100644 --- a/django/utils/safestring.py +++ b/django/utils/safestring.py @@ -30,7 +30,13 @@ else: EscapeUnicode = EscapeText class SafeData(object): - pass + def __html__(self): + """ + Returns the html representation of a string. + + Allows interoperability with other template engines. + """ + return self class SafeBytes(bytes, SafeData): """ |
