test_safestring.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from django.template import Context, Template
  2. from django.test import SimpleTestCase
  3. from django.utils import html
  4. from django.utils.functional import lazy, lazystr
  5. from django.utils.safestring import SafeData, mark_safe
  6. class customescape(str):
  7. def __html__(self):
  8. # Implement specific and wrong escaping in order to be able to detect
  9. # when it runs.
  10. return self.replace('<', '<<').replace('>', '>>')
  11. class SafeStringTest(SimpleTestCase):
  12. def assertRenderEqual(self, tpl, expected, **context):
  13. context = Context(context)
  14. tpl = Template(tpl)
  15. self.assertEqual(tpl.render(context), expected)
  16. def test_mark_safe(self):
  17. s = mark_safe('a&b')
  18. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  19. self.assertRenderEqual('{{ s|force_escape }}', 'a&amp;b', s=s)
  20. def test_mark_safe_str(self):
  21. """
  22. Calling str() on a SafeString instance doesn't lose the safe status.
  23. """
  24. s = mark_safe('a&b')
  25. self.assertIsInstance(str(s), type(s))
  26. def test_mark_safe_object_implementing_dunder_html(self):
  27. e = customescape('<a&b>')
  28. s = mark_safe(e)
  29. self.assertIs(s, e)
  30. self.assertRenderEqual('{{ s }}', '<<a&b>>', s=s)
  31. self.assertRenderEqual('{{ s|force_escape }}', '&lt;a&amp;b&gt;', s=s)
  32. def test_mark_safe_lazy(self):
  33. s = lazystr('a&b')
  34. self.assertIsInstance(mark_safe(s), SafeData)
  35. self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
  36. def test_mark_safe_object_implementing_dunder_str(self):
  37. class Obj:
  38. def __str__(self):
  39. return '<obj>'
  40. s = mark_safe(Obj())
  41. self.assertRenderEqual('{{ s }}', '<obj>', s=s)
  42. def test_mark_safe_result_implements_dunder_html(self):
  43. self.assertEqual(mark_safe('a&b').__html__(), 'a&b')
  44. def test_mark_safe_lazy_result_implements_dunder_html(self):
  45. self.assertEqual(mark_safe(lazystr('a&b')).__html__(), 'a&b')
  46. def test_add_lazy_safe_text_and_safe_text(self):
  47. s = html.escape(lazystr('a'))
  48. s += mark_safe('&b')
  49. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  50. s = html.escapejs(lazystr('a'))
  51. s += mark_safe('&b')
  52. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  53. def test_mark_safe_as_decorator(self):
  54. """
  55. mark_safe used as a decorator leaves the result of a function
  56. unchanged.
  57. """
  58. def clean_string_provider():
  59. return '<html><body>dummy</body></html>'
  60. self.assertEqual(mark_safe(clean_string_provider)(), clean_string_provider())
  61. def test_mark_safe_decorator_does_not_affect_dunder_html(self):
  62. """
  63. mark_safe doesn't affect a callable that has an __html__() method.
  64. """
  65. class SafeStringContainer:
  66. def __html__(self):
  67. return '<html></html>'
  68. self.assertIs(mark_safe(SafeStringContainer), SafeStringContainer)
  69. def test_mark_safe_decorator_does_not_affect_promises(self):
  70. """
  71. mark_safe doesn't affect lazy strings (Promise objects).
  72. """
  73. def html_str():
  74. return '<html></html>'
  75. lazy_str = lazy(html_str, str)()
  76. self.assertEqual(mark_safe(lazy_str), html_str())