Ticket #10107: patch.diff
File patch.diff, 5.5 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 _safety_decorator(safety_marker, func): 90 def wrapped(*args, **kwargs): 91 return safety_marker(func(*args, **kwargs)) 92 return wraps(func)(wrapped) 93 94 89 95 def mark_safe(s): 90 96 """ 91 97 Explicitly mark a string as safe for (HTML) output purposes. The returned 92 98 object can be used everywhere a string or unicode object is appropriate. 93 99 100 If used on a method as a decorator, mark the returned data as safe. 101 94 102 Can be called multiple times on a single string. 95 103 """ 104 if callable(s): 105 return _safety_decorator(mark_safe, s) 96 106 if isinstance(s, SafeData): 97 107 return s 98 108 if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str): … … 106 116 Explicitly mark a string as requiring HTML escaping upon output. Has no 107 117 effect on SafeData subclasses. 108 118 119 If used on a method as a decorator, mark the returned data as requiring HTML 120 escaping. 121 109 122 Can be called multiple times on a single string (the resulting escaping is 110 123 only applied once). 111 124 """ 125 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): -
docs/topics/templates.txt
486 486 This will be escaped: <b> 487 487 This will not be escaped: <b> 488 488 489 For methods 490 ~~~~~~~~~~~ 491 492 To control whether the data returned by a method is escaped or not when outputted 493 in a template, use 494 :func:`django.utils.safestring.mark_safe` 495 and 496 :func:`django.utils.safestring.mark_for_escaping`. 497 Here is an example :: 498 499 def nice_html_content(): 500 # Build up some html content 501 return mark_safe(content) 502 503 The :func:`mark_safe` and :func:`mark_for_escaping` methods can also be used as decorators 504 on methods to mark whatever data is returned by it as being safe or not. This can come in handy 505 when a method has many different return points in a quite complicated schema. 506 489 507 For template blocks 490 508 ~~~~~~~~~~~~~~~~~~~ 491 509 -
tests/regressiontests/decorators/tests.py
3 3 4 4 from django.http import HttpResponse 5 5 from django.utils.functional import allow_lazy, lazy, memoize 6 from django.utils.safestring import mark_safe, mark_for_escaping 6 7 from django.views.decorators.http import require_http_methods, require_GET, require_POST 7 8 from django.views.decorators.vary import vary_on_headers, vary_on_cookie 8 9 from django.views.decorators.cache import cache_page, never_cache, cache_control … … 42 43 fully_decorated = allow_lazy(fully_decorated) 43 44 fully_decorated = lazy(fully_decorated) 44 45 46 # django.utils.safestring 47 fully_decorated = mark_safe(fully_decorated) 48 fully_decorated = mark_for_escaping(fully_decorated) 49 45 50 class DecoratorsTest(TestCase): 46 51 47 52 def test_attributes(self): … … 56 61 self.assertEquals(fully_decorated.__doc__, 'Expected __doc__') 57 62 self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__') 58 63 64 def test_escaping(self): 65 """ 66 Tests that safety markers from django.utils.safestring 67 return the proper str or unicode subclass for use in templates. 68 69 Tests are done by comparing directly a sample rendered Template instance 70 with the unicode content it should contain. 71 """ 72 from django.template import Template, Context 73 template = Template("{{ data }}") 74 75 rendered = { 'safe': u'<html><body>dummy</body></html>', 76 'escaped': u'<html><body>dummy</body></html>' } 77 def clean_unicode_provider(): 78 return u'<html><body>dummy</body></html>' 79 80 def clean_string_provider(): 81 return '<html><body>dummy</body></html>' 82 83 escaped_unicode = mark_for_escaping(clean_unicode_provider)() 84 safe_unicode = mark_safe(clean_unicode_provider)() 85 escaped_str = mark_for_escaping(clean_string_provider)() 86 safe_str = mark_safe(clean_string_provider)() 87 88 self.assertEquals(template.render(Context({'data': escaped_unicode})), 89 rendered['escaped']) 90 self.assertEquals(template.render(Context({'data': safe_unicode})), 91 rendered['safe']) 92 self.assertEquals(template.render(Context({'data': escaped_str})), 93 rendered['escaped']) 94 self.assertEquals(template.render(Context({'data': safe_str})), 95 rendered['safe']) 96 59 97 def test_user_passes_test_composition(self): 60 98 """ 61 99 Test that the user_passes_test decorator can be applied multiple times