From af2e95b0fae0437c1cc6c08adfe4ce9c68991a50 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 24 Jun 2020 11:41:10 +0200 Subject: [PATCH] [3.0.x] Refs #31493 -- Replaced var with const/let in documentation JS. Backport of 2afa61e7d99b2ff2656dc64b6e28db88baf786a4 from master Co-authored-by: Carlton Gibson --- .../contributing/writing-code/javascript.txt | 6 ++-- docs/ref/csrf.txt | 14 ++++---- docs/ref/templates/builtins.txt | 2 +- docs/topics/i18n/translation.txt | 35 ++++++++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/docs/internals/contributing/writing-code/javascript.txt b/docs/internals/contributing/writing-code/javascript.txt index 09017af63a..07bea197a2 100644 --- a/docs/internals/contributing/writing-code/javascript.txt +++ b/docs/internals/contributing/writing-code/javascript.txt @@ -83,13 +83,13 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module: QUnit.module('magicTricks', { beforeEach: function() { - var $ = django.jQuery; + const $ = django.jQuery; $('#qunit-fixture').append(''); } }); QUnit.test('removeOnClick removes button on click', function(assert) { - var $ = django.jQuery; + const $ = django.jQuery; removeOnClick('.button'); assert.equal($('.button').length, 1); $('.button').click(); @@ -97,7 +97,7 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module: }); QUnit.test('copyOnClick adds button on click', function(assert) { - var $ = django.jQuery; + const $ = django.jQuery; copyOnClick('.button'); assert.equal($('.button').length, 1); $('.button').click(); diff --git a/docs/ref/csrf.txt b/docs/ref/csrf.txt index 20a8ddb433..0e4423248d 100644 --- a/docs/ref/csrf.txt +++ b/docs/ref/csrf.txt @@ -85,11 +85,11 @@ You can acquire the token like this: .. code-block:: javascript function getCookie(name) { - var cookieValue = null; + let cookieValue = null; if (document.cookie && document.cookie !== '') { - var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { - var cookie = cookies[i].trim(); + const cookies = document.cookie.split(';'); + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim(); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); @@ -99,14 +99,14 @@ You can acquire the token like this: } return cookieValue; } - var csrftoken = getCookie('csrftoken'); + const csrftoken = getCookie('csrftoken'); The above code could be simplified by using the `JavaScript Cookie library `_ to replace ``getCookie``: .. code-block:: javascript - var csrftoken = Cookies.get('csrftoken'); + const csrftoken = Cookies.get('csrftoken'); .. note:: @@ -138,7 +138,7 @@ and read the token from the DOM with JavaScript: {% csrf_token %} Setting the token on the AJAX request diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 387ad09a83..ae62727266 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -1816,7 +1816,7 @@ The resulting data can be accessed in JavaScript like this: .. code-block:: javascript - var value = JSON.parse(document.getElementById('hello-data').textContent); + const value = JSON.parse(document.getElementById('hello-data').textContent); XSS attacks are mitigated by escaping the characters "<", ">" and "&". For example if ``value`` is ``{'hello': 'world&'}``, the output is: diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 53f2f99205..441085b497 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -1067,9 +1067,12 @@ interface within your Python code:: The ``ngettext`` function provides an interface to pluralize words and phrases:: - var object_count = 1 // or 0, or 2, or 3, ... - s = ngettext('literal for the singular case', - 'literal for the plural case', object_count); + const objectCount = 1 // or 0, or 2, or 3, ... + const string = ngettext( + 'literal for the singular case', + 'literal for the plural case', + objectCount + ); ``interpolate`` ~~~~~~~~~~~~~~~ @@ -1083,23 +1086,29 @@ function supports both positional and named interpolation: corresponding ``fmt`` placeholders in the same order they appear. For example:: - fmts = ngettext('There is %s object. Remaining: %s', - 'There are %s objects. Remaining: %s', 11); - s = interpolate(fmts, [11, 20]); - // s is 'There are 11 objects. Remaining: 20' + const formats = ngettext( + 'There is %s object. Remaining: %s', + 'There are %s objects. Remaining: %s', + 11 + ); + const string = interpolate(formats, [11, 20]); + // string is 'There are 11 objects. Remaining: 20' * Named interpolation: This mode is selected by passing the optional boolean ``named`` parameter as ``true``. ``obj`` contains a JavaScript object or associative array. For example:: - d = { - count: 10, - total: 50 + const data = { + count: 10, + total: 50 }; - fmts = ngettext('Total: %(total)s, there is %(count)s object', - 'there are %(count)s of a total of %(total)s objects', d.count); - s = interpolate(fmts, d, true); + const formats = ngettext( + 'Total: %(total)s, there is %(count)s object', + 'there are %(count)s of a total of %(total)s objects', + data.count + ); + const string = interpolate(formats, data, true); You shouldn't go over the top with string interpolation, though: this is still JavaScript, so the code has to make repeated regular-expression substitutions.