Problem/Motivation

As mentioned in the parent issue #3238306: [META] Where possible, refactor existing jQuery uses to vanillaJS to reduce jQuery footprint, we are working towards reducing our jQuery footprint. One of the ways to accomplish this is to reduce the number of jQuery features used in Drupal core. We have added eslint rules that identify specific features and fail tests when those features are in use.

There are (or will be) individual issues for each jQuery-use eslint rule. This one is specific to jquery/no-text, which targets the jQuery text function.

Steps to reproduce

Proposed resolution

Remaining tasks

  • In core/.eslintrc.jquery.json Change "jquery/no-text": 0, to "jquery/no-text": 2, to enable eslint checks for uses of jQuery .text(). With this change, you'll be able to see uses of the undesirable jQuery feature by running yarn lint:core-js-passing from the core directory
  • Add the following lines to core/scripts/dev/commit-code-check.sh so the DrupalCI testing script can catch this jQuery usage on all files, not just those which have changed
    # @todo Remove the next chunk of lines before committing. This script only lints
    #  JavaScript files that have changed, so we add this to check all files for
    #  jQuery-specific lint errors.
    cd "$TOP_LEVEL/core"
    node ./node_modules/eslint/bin/eslint.js --quiet --config=.eslintrc.passing.json .
    
    CORRECTJQS=$?
    if [ "$CORRECTJQS" -ne "0" ]; then
      # No need to write any output the node command will do this for us.
      printf "${red}FAILURE ${reset}: unsupported jQuery usage. See errors above."
      STATUS=1
      FINAL_STATUS=1
    fi
    cd $TOP_LEVEL
    # @todo end lines to remove

    Add the block about 10 lines before the end of the file, just before if [[ "$FINAL_STATUS" == "1" ]] && [[ "$DRUPALCI" == "1" ]]; then, then remove it once all the jQuery uses have been refactored.

  • If it's determined to be feasible, refactor those uses of jQuery .text() to use Vanilla (native) JavaScript instead.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3239123

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

hooroomoo created an issue. See original summary.

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

hooroomoo’s picture

Am aware that I used .includes() in core/modules/views_ui/js/views_ui.listing.js.

This is in the hope that #3239509: Add String.includes polyfill to support IE11 and Opera Mini would get committed to core by the time this was reviewed since it was already committed once but de-committed due to the most recent code freeze.

larowlan’s picture

I've asked if #3239509: Add String.includes polyfill to support IE11 and Opera Mini is still 9.3 eligible, thanks for the reminder

hooroomoo’s picture

Status: Active » Needs review
bnjmnm’s picture

Status: Needs review » Needs work

Every change has been reviewed. Even those that don't need changes have a (resolved) comment in Gitlab stating that so nobody has to re-review parts already confirmed to be fine.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hooroomoo’s picture

Status: Needs work » Needs review
bnjmnm’s picture

Status: Needs review » Needs work

Setting to "needs work" - multiple test failures, but apparently didn't automatically set the issue to NW

Menu.Drupal\Tests\system\Functional\Menu\LocalTasksTest.Drupal\Tests\system\Functional\Menu\LocalTasksTest
Node.Drupal\Tests\node\Functional\NodeTypeTest.Drupal\Tests\node\Functional\NodeTypeTest
Node.Drupal\Tests\node\Functional\NodeTypeTest.Unknown
User.Drupal\Tests\user\Functional\UserPermissionsTest.Drupal\Tests\user\Functional\UserPermissionsTest
User.Drupal\Tests\user\Functional\UserPermissionsTest.Unknown
hooroomoo’s picture

Status: Needs work » Needs review
hooroomoo’s picture

Checked that nodelist.foreach polyfill dependecy is added where necessary.

nod_’s picture

Status: Needs review » Needs work

few things :)

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Needs work

Couple of things, 4-5 bugs, one regression few extra changes that are not necessary for this issue.

Looking good!

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Needs work

Just one little comment and tests fail to fix (need to check if element exists before getting the textContent) and we're good to go! Probably need a D10 patch once it's RTBC too

Great work, thanks you!

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community

And it is RTBC!

Everything looks good now. Thanks so much for going through that again :) We only need a D10 patch.

(marking rtbc because the issue is against 9.4.x and that code is RTBC)

bnjmnm’s picture

StatusFileSize
new66.08 KB

Drupal 10 patch

nod_’s picture

D10 patch is good except for the changes to the core.libraries.yml file

bnjmnm’s picture

StatusFileSize
new57.41 KB

In a patch with that many changes, I'd be shocked if I didn't miss something. Here's the patch without core.libraries

nod_’s picture

Unfortunately the eslint rule isn't bullet-proof and we missed a few instances of .text() usage

core/misc/tableresponsive.es6.js
          this.$link.show().text(this.showText);
          this.$link.hide().text(this.hideText);
          this.$link.text(this.hideText).data('pegged', 1);
          this.$link.text(this.showText).data('pegged', 0);
core/modules/ckeditor/js/views/VisualView.es6.js
          .text(
core/modules/contextual/js/views/AuralView.es6.js
          .text(
core/modules/quickedit/js/views/ContextualLinkView.es6.js
        this.$el.find('a').text(options.strings.quickEdit);

I think it would be fine to have a follow-up though this will avoid having to be careful of the 60+ files that are changed by the current patch.

Leaving RTBC because the patch does fix the issue and eslint rules are passing.

nod_’s picture

in any case MR + D10 patch are RTBC for me.

hooroomoo’s picture

Status: Reviewed & tested by the community » Active

There are some .text() usages that were not caught by the linter that nod_ pointed out. Will address.

hooroomoo’s picture

Status: Active » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community

Fixes are working as expected and don't see anymore jQuery .text usage!

Thank you :)

lauriii’s picture

Status: Reviewed & tested by the community » Needs review

I don't see any discussion about whether using Node.textContent is appropriate because it isn't exactly same as jQuery.text().

Based on jQuery documentation, jQuery.text() is escaping any nodes. This means that jQuery.text( '<p>This is a test.</p>' ) would result in &lt;p&gt;This is a test.&lt;/p&gt;, whereas Node.textContent = '<p>This is a test.</p>' would result in This is a test..

nod_’s picture

Status: Needs review » Reviewed & tested by the community

I believe their documentation is outdated.

In the version of jquery we're using there it no createTextNode involved when using the text function: https://git.drupalcode.org/project/drupal/-/blob/9.4.x/core/assets/vendo...

The code in this patch is doing the same thing as jQuery :)

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

Posted few comments on the MR

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community

Last problems have been addressed, all good for me

lauriii’s picture

This needs a new Drupal 10 patch

hooroomoo’s picture

StatusFileSize
new34.45 KB

d10 patch

hooroomoo’s picture

StatusFileSize
new66.09 KB

  • lauriii committed 8c62a32 on 10.0.x
    Issue #3239123 by hooroomoo, bnjmnm, nod_: Refactor (if feasible) uses...

  • lauriii committed 804a0a1 on 9.4.x
    Issue #3239123 by hooroomoo, bnjmnm, nod_: Refactor (if feasible) uses...

lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8c62a32 and pushed to 10.0.x. Also committed the MR in 9.4.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.