Problem/Motivation

Following the !placeholder replacement in PHP code, we also should perform the same replacement for JavaScript.
This keeps placeholder usage in PHP and JavaScript consistent, which is good for DX.

Scripts use:

egrep -r '\.t\(.*![a-zA-Z]' core
egrep -r '\.formatPlural\(.*![a-zA-Z]' core
egrep -r '\.formatString\(.*![a-zA-Z]' core

Proposed resolution

  • Replace !placeholder by @placeholder for both URLs and non-URLs (this issue)
  • Decide if we need a :placeholder for URLs. According to discussion with alexpott in IRC there is currently no use case for this. (separate issue?)
  • #2570101: Remove !placeholder support from Drupal.formatString
  • Manually test for double escaping

Remaining tasks

t.b.d.

User interface changes

none

API changes

t.b.d.

Data model changes

none

Issue fork drupal-2570093

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Sutharsan created an issue. See original summary.

alexpott’s picture

I'm not 100% certain that can or need to remove the !placholder in javascript. The thing is we have nothing equivalent of auto escaping and safeness.

sutharsan’s picture

Status: Active » Needs review
StatusFileSize
new4.48 KB

Replacing all !placeholders in JS by @placeholder, both non-url and url. I can not oversee the consequences for URLs yet, but we need to start somewhere.

sutharsan’s picture

Issue summary: View changes
sutharsan’s picture

Issue summary: View changes
dawehner’s picture

+++ b/core/misc/ajax.js
@@ -140,9 +140,9 @@
 
-    customMessage = customMessage ? ("\n" + Drupal.t("CustomMessage: !customMessage", {'!customMessage': customMessage})) : "";
+    customMessage = customMessage ? ("\n" + Drupal.t("CustomMessage: @customMessage", {'@customMessage': customMessage})) : "";

Does that mean that customMessage can never contain HTML in the first place at all? Not sure whether this is a right assumption, I could totally imagine that its passed through t(). Do we have a tool in JS to ensure something is not double escaped?

sutharsan’s picture

The way Drupal.Ajax is used in core, the CustomMessage is always empty. But technically, yes it may contain HTML. As far as I know we don't have a JS tool for double escape.

dawehner’s picture

Maybe we should directly get http://phpjs.org/functions/htmlspecialchars/ and use that.

effulgentsia’s picture

I agree with #2. What makes it possible to remove '!' in PHP is that '@' can conditionally escape based on the safeness of the input. In JS, '@' escapes always, so we need '!' if the value already has (hopefully safe) HTML, just like in Drupal 7.

xjm’s picture

pwolanin’s picture

So, maybe we need to close this issue if the JS api can't be the same as PHP?

nod_’s picture

Status: Needs review » Closed (won't fix)

I agree with #2 and #9. Closing related issue as well.

nod_’s picture

Title: Replace !placeholder with @placeholder in JavaScript » Replace !placeholder with @placeholder where needed in JavaScript
Status: Closed (won't fix) » Needs work

So there are definitely places where we should properly use @ instead of !, reopening for those. We don't have to get rid of !placeholder but we can fix some strings.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

chernous_dn’s picture

Status: Needs work » Needs review
StatusFileSize
new5.67 KB

Use

egrep -r '\.t\(.*![a-zA-Z]' core
egrep -r '\.formatPlural\(.*![a-zA-Z]' core
egrep -r '\.formatString\(.*![a-zA-Z]' core

Create patch.

chernous_dn’s picture

StatusFileSize
new6.29 KB

Update patch.

The last submitted patch, 15: replace_placeholder-2570093-15.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 16: replace_placeholder-2570093-16.patch, failed testing.

chernous_dn’s picture

StatusFileSize
new4.91 KB

Update patch again.

chernous_dn’s picture

Status: Needs work » Needs review

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kwoxer’s picture

Status: Needs review » Needs work

Just tested the current patch and it needs a reroll. Please create a new patch. Thanks.

cburschka’s picture

Assigned: Unassigned » cburschka
cburschka’s picture

The ES6 change and some other major changes basically require doing this one from scratch.

Quick overview of the files that potentially need changing.

$ find . -name *.js|xargs grep -l 'Drupal.t(.*!'
./core/misc/ajax.es6.js
./core/misc/ajax.js
./core/modules/ckeditor/js/ckeditor.es6.js
./core/modules/ckeditor/js/ckeditor.js
./core/modules/locale/tests/locale_test.es6.js
./core/modules/locale/tests/locale_test.js
./core/modules/quickedit/js/models/EntityModel.es6.js
./core/modules/quickedit/js/models/EntityModel.js
./core/modules/quickedit/js/util.es6.js
./core/modules/quickedit/js/util.js
./core/modules/quickedit/js/views/EntityToolbarView.js
./core/modules/system/js/system.modules.js
./core/modules/tour/js/tour.es6.js
./core/modules/tour/js/tour.js
cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new11.25 KB

The above list doesn't include multi-line calls, but the converter turns those into single-lines so the compiled JS file will be matched even if the ES6 one isn't. See system.modules.js.

It looks like the whole thing basically is the same as from the old patch, with one additional one in locale_test.js.

Status: Needs review » Needs work

The last submitted patch, 27: drupal-2570093-27.patch, failed testing. View results

cburschka’s picture

The bad line is the !total here.

@@ -255,7 +255,7 @@
           .find('li')
           // Rebuild the progress data.
           .each(function (index) {
-            const progress = Drupal.t('!tour_item of !total', { '!tour_item': index + 1, '!total': total });
+            const progress = Drupal.t('@tour_item of !total', { '@tour_item': index + 1, '@total': total });
             $(this).find('.tour-progress').text(progress);

On phone now; will fix later.

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new11.25 KB

At the airport; my final contribution to the DrupalCon sprint. :)

cburschka’s picture

StatusFileSize
new11.25 KB

Forgot to rebuild the JS of course.

cburschka’s picture

StatusFileSize
new12.61 KB

...and forgot to also look for formatPlural (one more occurrence) and formatString (nothing found).

The last submitted patch, 30: drupal-2570093-30.patch, failed testing. View results

The last submitted patch, 31: drupal-2570093-31.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 32: drupal-2570093-32.patch, failed testing. View results

cburschka’s picture

Status: Needs work » Needs review
StatusFileSize
new12.64 KB

Reroll for 8.5.x because of JS const codestyle.

Status: Needs review » Needs work

The last submitted patch, 36: drupal-2570093-36.patch, failed testing. View results

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
catch’s picture

Issue tags: +Needs reroll

Still valid, needs a re-roll.

karishmaamin’s picture

Version: 9.3.x-dev » 10.0.x-dev
Status: Needs work » Needs review
StatusFileSize
new11.55 KB

Re-rolled against 10.x. Please review

aarti zikre’s picture

Will provide the patch tomorrow

aarti zikre’s picture

StatusFileSize
new393.94 KB
aarti zikre’s picture

StatusFileSize
new12.65 KB
aarti zikre’s picture

StatusFileSize
new12.27 KB

Status: Needs review » Needs work

The last submitted patch, 49: 2570093_49.patch, failed testing. View results

Vighneshh made their first commit to this issue’s fork.

aarti zikre’s picture

Status: Needs work » Needs review
StatusFileSize
new13.63 KB

Status: Needs review » Needs work

The last submitted patch, 52: 2570093_50.patch, failed testing. View results

aarti zikre’s picture

Status: Needs work » Needs review
StatusFileSize
new40.29 KB
aarti zikre’s picture

StatusFileSize
new13.51 KB
aarti zikre’s picture

Review require

ameymudras’s picture

The above patch LGTM, Moving to RTBC unless we are missing any other files.

  1. Patch applies cleanly.
  2. Tests pass.
  3. Title and issue summary are clear.
  4. Change is simple and addresses the issue from the issue summary.
  5. Not expected that tests are needed.
  6. Issue metadata looks okay.
  7. Manually tested and patch works as expected per above.
ameymudras’s picture

Assigned: cburschka » Unassigned
ameymudras’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 55: 2570093_54.patch, failed testing. View results

sahil.goyal’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll
xjm’s picture

Version: 10.0.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

@sahil.goyal, this does not apply to 10.1.x, so the "Needs reroll" tag was correct.

@aarti zikre, when you supply new patches for an issue, please pay attention to the current status of the issue, and include both an issue comment explaining your intentions and an interdiff for any changes you made to the patch. For example, in #45, a new 10.0.x patch was already supplied, yet you commented saying you would create patch without making note of what, if anything, needed to be fixed with the previous patch. Thanks!

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new5.33 KB
new10.53 KB
new12.58 KB

Rerolled the patch against 10.1.x.

Changes in the new patch:

  • Drupal 10 doesn't need to have the es6 code transpiled, so we can just copy the contents from .es6.js into .js file from the D9 patch and remove the .es6.js.
  • Compared the patch in #45 and #54, it included a change in core/modules/locale/tests/src/Functional/LocaleJavascriptTranslationTest.php file. Removed that file in the new patch, as this issue focuses on only .js files (as mentioned in the issue title).

Thanks!

Status: Needs review » Needs work

The last submitted patch, 63: 2570093-63.patch, failed testing. View results

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.