1
0
mirror of https://github.com/django/django.git synced 2025-01-27 10:39:40 +00:00

[4.2.x] Fixed #33405, Refs #7177 -- Clarified docs for filter escapejs regarding safe and unsafe usages.

Backport of adfb3dfa89b62ee0c838a64d3d480c03dd3ec869 from main
This commit is contained in:
Jon Ribbens 2023-05-25 16:39:15 +01:00 committed by Mariusz Felisiak
parent 047844270b
commit e54f711d42
2 changed files with 9 additions and 7 deletions

View File

@ -1865,18 +1865,19 @@ For example, you can apply ``escape`` to fields when :ttag:`autoescape` is off:
``escapejs`` ``escapejs``
------------ ------------
Escapes characters for use in JavaScript strings. This does *not* make the Escapes characters for use as a whole JavaScript string literal, within single
string safe for use in HTML or JavaScript template literals, but does protect or double quotes, as below. This filter does not make the string safe for use
you from syntax errors when using templates to generate JavaScript/JSON. in *"JavaScript template literals"* (the JavaScript backtick syntax). Any other
uses not listed above are not supported. It is generally recommended that data
should be passed using HTML ``data-`` attributes, or the :tfilter:`json_script`
filter, rather than in embedded JavaScript.
For example: For example:
.. code-block:: html+django .. code-block:: html+django
{{ value|escapejs }} <script>
let myValue = '{{ value|escapejs }}'
If ``value`` is ``"testing\r\njavascript 'string\" <b>escaping</b>"``,
the output will be ``"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E"``.
.. templatefilter:: filesizeformat .. templatefilter:: filesizeformat

View File

@ -40,6 +40,7 @@ backends
backport backport
backported backported
backports backports
backtick
backtraces backtraces
balancer balancer
basename basename