Code

Ticket #10107: safestring.diff

File safestring.diff, 2.0 KB (added by matehat, 5 years ago)
Line 
1Index: django/utils/safestring.py
2===================================================================
3--- django/utils/safestring.py  (revision 9781)
4+++ django/utils/safestring.py  (working copy)
5@@ -4,7 +4,7 @@
6 that the producer of the string has already turned characters that should not
7 be interpreted by the HTML engine (e.g. '<') into the appropriate entities.
8 """
9-from django.utils.functional import curry, Promise
10+from django.utils.functional import curry, wraps, Promise
11 
12 class EscapeData(object):
13     pass
14@@ -86,13 +86,24 @@
15 
16     encode = curry(_proxy_method, method = unicode.encode)
17 
18-def mark_safe(s):
19+def _safety_decorator(safety_marker, func):
20+  @wraps(func)
21+  def wrapped(*args, **kwargs):
22+    return safety_marker(func(*args, **kwargs))
23+  return wrapped
24+
25+
26+def mark_safe(s=None):
27     """
28     Explicitly mark a string as safe for (HTML) output purposes. The returned
29     object can be used everywhere a string or unicode object is appropriate.
30 
31     Can be called multiple times on a single string.
32     """
33+    if s is None: # Used like: @mark_safe()
34+        return curry(_safety_decorator, mark_safe)
35+    if callable(s): # Used like: @mark_safe
36+        return _safety_decorator(mark_safe, s)
37     if isinstance(s, SafeData):
38         return s
39     if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str):
40@@ -101,7 +112,7 @@
41         return SafeUnicode(s)
42     return SafeString(str(s))
43 
44-def mark_for_escaping(s):
45+def mark_for_escaping(s=None):
46     """
47     Explicitly mark a string as requiring HTML escaping upon output. Has no
48     effect on SafeData subclasses.
49@@ -109,6 +120,11 @@
50     Can be called multiple times on a single string (the resulting escaping is
51     only applied once).
52     """
53+   
54+    if s is None:
55+        return curry(_safety_decorator, mark_for_escaping)
56+    if callable(s):
57+        return _safety_decorator(mark_for_escaping, s)
58     if isinstance(s, (SafeData, EscapeData)):
59         return s
60     if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str):