Ticket #10107: safestring.diff
File safestring.diff, 2.0 KB (added by , 16 years ago) |
---|
-
django/utils/safestring.py
4 4 that the producer of the string has already turned characters that should not 5 5 be interpreted by the HTML engine (e.g. '<') into the appropriate entities. 6 6 """ 7 from django.utils.functional import curry, Promise7 from django.utils.functional import curry, wraps, Promise 8 8 9 9 class EscapeData(object): 10 10 pass … … 86 86 87 87 encode = curry(_proxy_method, method = unicode.encode) 88 88 89 def mark_safe(s): 89 def _safety_decorator(safety_marker, func): 90 @wraps(func) 91 def wrapped(*args, **kwargs): 92 return safety_marker(func(*args, **kwargs)) 93 return wrapped 94 95 96 def mark_safe(s=None): 90 97 """ 91 98 Explicitly mark a string as safe for (HTML) output purposes. The returned 92 99 object can be used everywhere a string or unicode object is appropriate. 93 100 94 101 Can be called multiple times on a single string. 95 102 """ 103 if s is None: # Used like: @mark_safe() 104 return curry(_safety_decorator, mark_safe) 105 if callable(s): # Used like: @mark_safe 106 return _safety_decorator(mark_safe, s) 96 107 if isinstance(s, SafeData): 97 108 return s 98 109 if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str): … … 101 112 return SafeUnicode(s) 102 113 return SafeString(str(s)) 103 114 104 def mark_for_escaping(s ):115 def mark_for_escaping(s=None): 105 116 """ 106 117 Explicitly mark a string as requiring HTML escaping upon output. Has no 107 118 effect on SafeData subclasses. … … 109 120 Can be called multiple times on a single string (the resulting escaping is 110 121 only applied once). 111 122 """ 123 124 if s is None: 125 return curry(_safety_decorator, mark_for_escaping) 126 if callable(s): 127 return _safety_decorator(mark_for_escaping, s) 112 128 if isinstance(s, (SafeData, EscapeData)): 113 129 return s 114 130 if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str):