diff options
| author | Jonas Haag <jonas@lophus.org> | 2017-10-12 20:59:38 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-02-07 18:38:12 -0500 |
| commit | 8c709d79cbd1a7bb975f58090c17a1178a0efb80 (patch) | |
| tree | 79e6be4dac2289ca5e4943db9ae071d14f95cdc5 /tests/utils_tests/test_html.py | |
| parent | ef2512b2ffdb719e5c0fb82142f9ce8478282823 (diff) | |
Fixed #17419 -- Added json_tag template filter.
Diffstat (limited to 'tests/utils_tests/test_html.py')
| -rw-r--r-- | tests/utils_tests/test_html.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index e6d1fe9a59..e2ebce4556 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -4,8 +4,8 @@ from datetime import datetime from django.test import SimpleTestCase from django.utils.functional import lazystr from django.utils.html import ( - conditional_escape, escape, escapejs, format_html, html_safe, linebreaks, - smart_urlquote, strip_spaces_between_tags, strip_tags, + conditional_escape, escape, escapejs, format_html, html_safe, json_script, + linebreaks, smart_urlquote, strip_spaces_between_tags, strip_tags, ) from django.utils.safestring import mark_safe @@ -147,6 +147,28 @@ class TestUtilsHtml(SimpleTestCase): self.check_output(escapejs, value, output) self.check_output(escapejs, lazystr(value), output) + def test_json_script(self): + tests = ( + # "<", ">" and "&" are quoted inside JSON strings + (('&<>', '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>')), + # "<", ">" and "&" are quoted inside JSON objects + ( + {'a': '<script>test&ing</script>'}, + '<script id="test_id" type="application/json">' + '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>' + ), + # Lazy strings are quoted + (lazystr('&<>'), '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>'), + ( + {'a': lazystr('<script>test&ing</script>')}, + '<script id="test_id" type="application/json">' + '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>' + ), + ) + for arg, expected in tests: + with self.subTest(arg=arg): + self.assertEqual(json_script(arg, 'test_id'), expected) + def test_smart_urlquote(self): items = ( ('http://öäü.com/', 'http://xn--4ca9at.com/'), |
