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-val, which targets the jQuery val function.

Steps to reproduce

Proposed resolution

Remaining tasks

  • In core/.eslintrc.jquery.json Change "jquery/no-val": 0, to "jquery/no-val": 2, to enable eslint checks for uses of jQuery .val(). 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 .val() to use Vanilla (native) JavaScript instead.

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#23 3239134-23-d10.patch98.23 KBhooroomoo

Issue fork drupal-3239134

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hooroomoo created an issue. See original summary.

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

Theresa.Grannum made their first commit to this issue’s fork.

hooroomoo’s picture

Status: Active » Needs review
hooroomoo’s picture

Status: Needs review » Active

set back to active due to failed tests

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

nod_’s picture

Status: Active » Needs work

Few things in the MR.

Generally we'll need to be careful because $(element).val() works even when element is undefined, this is not the case with dom elements. if the element is empty there is no .value property and things crash. Let's see what the tests says.

nod_’s picture

@yogeshmpawar please make sure not to undo another commit during a rebase. You removed part of #3239132: Refactor (if feasible) uses of the jQuery trim function to use vanillaJS in the process :)

yogeshmpawar’s picture

Thanks @nod_ for pointing this. actually while rebasing I have compared this issue code with this drupal core - https://git.drupalcode.org/issue/drupal-3239134/-/blob/9.3.x/core/.eslin... didn't recognise that this is not correct drupal core to compare. will correct it now.

hooroomoo’s picture

Status: Needs work » Needs review
larowlan’s picture

Left a couple of comments on the MR where I think we're replacing the val(something) setter with [0].value where assuming there is only one match may not be valid - e.g. class based selectors

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.

bnjmnm’s picture

Status: Needs review » Needs work

Down to fewer than 10 threads for this gigantic issue, which is incredible progress. I updated those with as helpful feedback as possible.

hooroomoo’s picture

Status: Needs work » Needs review

Looked through all usages of nodelist.forEach and added it to the libraries that didn't have it.

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

I provided a comment for every change made in the 100+ JS files impacted in this issue. Even if there were no requested changes, I documented that the change was reviewed and looks good, usually explaining why it's a safe change that accounts for the differences between jQuery and vanilla (namely jQUery automatically acting on multiple elements in a query, and how it casually reacts to calling a function on empty results). This is too large an MR to assume no-comment === no-problem.

I've run out of nits to pick and I'm making this big chungus RTBC.

nod_’s picture

Status: Reviewed & tested by the community » Needs work

Went through all the code & tested most changes, if I didn't comment I didn't find anything wrong with it :)

bunch of small things, 2 or 3 actual bugs/problems found. Which is great given the size of the MR :)

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Needs work

almost there :)

hooroomoo’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community

Changes looks good, simply removed an unnecessary eslint comment.

All good from me!

lauriii’s picture

Version: 9.4.x-dev » 10.0.x-dev
Status: Reviewed & tested by the community » Needs work

Reviewed the MR in detail and it looks good! Only problem is that to be able to commit this, we need a Drupal 10 version of the MR.

hooroomoo’s picture

Status: Needs work » Needs review
FileSize
98.23 KB

d10 patch uploaded

nod_’s picture

Status: Needs review » Reviewed & tested by the community

10.x patch is all good.

  • lauriii committed 6808108 on 10.0.x
    Issue #3239134 by hooroomoo, bnjmnm, yogeshmpawar, nod_, Theresa.Grannum...

  • lauriii committed 611b7d1 on 9.4.x
    Issue #3239134 by hooroomoo, bnjmnm, yogeshmpawar, nod_, Theresa.Grannum...
lauriii’s picture

Version: 10.0.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 6808108 and pushed to 10.0.x. Also committed the 9.4.x MR. Thanks!

nod_’s picture

Status: Fixed » Closed (fixed)

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