Refactor (if feasible) uses of the jQuery attr function to use Vanilla/native

Created on 24 September 2021, over 3 years ago
Updated 30 July 2024, 5 months ago

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

Steps to reproduce

Proposed resolution

Remaining tasks

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

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ“Œ Task
Status

Closed: won't fix

Version

11.0 ๐Ÿ”ฅ

Component
Javascriptย  โ†’

Last updated about 6 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @justafish
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance @nod_
Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States Theresa.Grannum Boston

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 1 year ago
    Total: 210s
    #62888
  • Issue was unassigned.
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States andy-blum Ohio, USA
  • Assigned to rahulrasgon
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rahulrasgon

    Below replacement needs to be done more.

    /core/misc/machine-name.js
      157:11  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/misc/tableresponsive.js
      40:28  error  Prefer Function#bind to $.proxy  jquery/no-proxy
    
    /core/misc/tableselect.js
      72:16  error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/block/js/block.admin.js
      26:24  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/comment/js/comment-new-indicator.js
      24:9   error  Prefer getAttribute to $.attr  jquery/no-attr
      28:22  error  Prefer getAttribute to $.attr  jquery/no-attr
      79:11  error  Prefer getAttribute to $.attr  jquery/no-attr
      82:24  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/comment/js/node-new-comments-link.js
       80:9   error  Prefer getAttribute to $.attr  jquery/no-attr
       83:19  error  Prefer getAttribute to $.attr  jquery/no-attr
       84:22  error  Prefer getAttribute to $.attr  jquery/no-attr
      125:11  error  Prefer setAttribute to $.attr  jquery/no-attr
      167:11  error  Prefer getAttribute to $.attr  jquery/no-attr
      170:24  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/contextual/js/views/AuralView.js
      43:9  error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/editor/js/editor.admin.js
      1022:31  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/editor/js/editor.js
      17:21  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/file/file.js
      258:30  error  Prefer getAttribute to $.attr  jquery/no-attr
      261:9   error  Prefer setAttribute to $.attr  jquery/no-attr
      268:11  error  Prefer setAttribute to $.attr  jquery/no-attr
      291:7   error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/layout_builder/js/layout-builder.js
       65:11  error  Prefer setAttribute to $.attr  jquery/no-attr
       70:11  error  Prefer setAttribute to $.attr  jquery/no-attr
      209:7   error  Prefer setAttribute to $.attr  jquery/no-attr
      238:18  error  Prefer getAttribute to $.attr  jquery/no-attr
      258:41  error  Prefer getAttribute to $.attr  jquery/no-attr
      374:51  error  Prefer getAttribute to $.attr  jquery/no-attr
      434:9   error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/menu_ui/menu_ui.admin.js
      63:9  error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/settings_tray/js/settings_tray.js
      168:76  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/system/js/system.modules.js
      66:11  error  Prefer setAttribute to $.attr  jquery/no-attr
      72:11  error  Prefer setAttribute to $.attr  jquery/no-attr
      86:11  error  Prefer setAttribute to $.attr  jquery/no-attr
    
    /core/modules/system/tests/modules/js_message_test/js/js_message_test.js
      44:24  error  Prefer getAttribute to $.attr  jquery/no-attr
      46:13  error  Prefer getAttribute to $.attr  jquery/no-attr
      50:26  error  Prefer getAttribute to $.attr  jquery/no-attr
    
    /core/modules/toolbar/js/views/ToolbarVisualView.js
      315:9  error  Prefer setAttribute to $.attr  jquery/no-attr
      319:9  error  Prefer setAttribute to $.attr  jquery/no-attr
    

    Meanwhile can someone please review the MR :- https://git.drupalcode.org/project/drupal/-/merge_requests/5791#note_242353

  • Pipeline finished with Failed
    about 1 year ago
    Total: 301s
    #63472
  • Pipeline finished with Canceled
    about 1 year ago
    Total: 9s
    #63949
  • Pipeline finished with Skipped
    about 1 year ago
    #63951
  • Pipeline finished with Skipped
    about 1 year ago
    #63952
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • Pipeline finished with Failed
    about 1 year ago
    Total: 659s
    #63950
  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    The remaining task around the code-committ still seems needed.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    There are several places that create a jquery object just to get the original dom element after. We can get rid of the creation of the jQuery object and use the dom directly

  • First commit to issue fork.
  • Status changed to Needs review 12 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia gauravvvv Delhi, India

    Addressed all feedbacks in MR. please review

  • Pipeline finished with Failed
    12 months ago
    Total: 1455s
    #73950
  • Status changed to Needs work 12 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Changes caused failures.

  • First commit to issue fork.
  • Pipeline finished with Failed
    10 months ago
    #101160
  • Pipeline finished with Failed
    10 months ago
    Total: 516s
    #101281
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ahsannazir

    In the above raised MR, the Funtional Javascript test cases are failing and there are console errors as well

  • Pipeline finished with Failed
    10 months ago
    Total: 555s
    #102036
  • Pipeline finished with Failed
    10 months ago
    Total: 543s
    #102071
  • Pipeline finished with Failed
    10 months ago
    Total: 464s
    #102173
  • Pipeline finished with Failed
    10 months ago
    Total: 539s
    #102208
  • Pipeline finished with Failed
    10 months ago
    Total: 501s
    #102233
  • Pipeline finished with Failed
    10 months ago
    Total: 2067s
    #102263
  • Pipeline finished with Failed
    10 months ago
    Total: 595s
    #103769
  • Pipeline finished with Failed
    10 months ago
    Total: 177s
    #103806
  • Pipeline finished with Failed
    10 months ago
    Total: 640s
    #103921
  • Pipeline finished with Success
    10 months ago
    Total: 606s
    #103953
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ahsannazir

    Resolved all failures. Please review

  • Status changed to Needs review 10 months ago
  • Status changed to Needs work 10 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    10 months ago
    Total: 522s
    #111980
  • Status changed to Needs review 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Will leave for sub-maintainer to review but personally find adding [0] makes the readability go down.

  • Status changed to Needs work 8 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511
  • Pipeline finished with Failed
    8 months ago
    Total: 185s
    #153086
  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Issues in MR.

  • Sam Phillemon โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    8 months ago
    #156179
  • Pipeline finished with Failed
    8 months ago
    Total: 988s
    #156186
  • Pipeline finished with Success
    8 months ago
    Total: 989s
    #157170
  • Pipeline finished with Success
    8 months ago
    Total: 482s
    #157191
  • Status changed to Needs review 8 months ago
  • I have fixed the remaining MR-related issues.

  • Pipeline finished with Success
    8 months ago
    Total: 562s
    #160403
  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    For good practice can the MRs be cleanup/hidden. There are currently 4 branche

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511

    shubh_ โ†’ changed the visibility of the branch 3239161-refactor-if-feasible to hidden.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511

    shubh_ โ†’ changed the visibility of the branch 3239161-10.1.x to hidden.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511

    shubh_ โ†’ changed the visibility of the branch 9.3.x to hidden.

  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511

    Done

  • Status changed to Needs work 7 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    7 months ago
    Total: 576s
    #176808
  • Pipeline finished with Success
    7 months ago
    Total: 576s
    #176846
  • Status changed to Needs review 7 months ago
  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States bnjmnm Ann Arbor, MI
  • Pipeline finished with Success
    7 months ago
    Total: 693s
    #177651
  • Status changed to Needs review 7 months ago
  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States bnjmnm Ann Arbor, MI
  • Pipeline finished with Success
    7 months ago
    Total: 664s
    #178805
  • Pipeline finished with Success
    7 months ago
    Total: 578s
    #178823
  • Pipeline finished with Success
    7 months ago
    Total: 619s
    #178915
  • Pipeline finished with Success
    7 months ago
    Total: 575s
    #178929
  • Pipeline finished with Success
    7 months ago
    Total: 850s
    #178950
  • Pipeline finished with Success
    7 months ago
    Total: 577s
    #179034
  • Pipeline finished with Success
    7 months ago
    Total: 603s
    #179043
  • Status changed to Needs review 7 months ago
  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    MR needs a manual rebase

  • Pipeline finished with Success
    6 months ago
    Total: 578s
    #208299
  • Status changed to Needs review 6 months ago
  • Status changed to Closed: won't fix 5 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    First of all thank you all for the hard work on this one, I've had to push a number of patches like this and I know how hard it is to keep up.

    I'm going to try and refocus the jQuery removal work and to do that I need to take a few decisions. I'm going to close this issue for a few reasons:

    • This MR is big, it impact a very big number of subsystems and make the code more brittle. jQuery is good at dealing with undefined elements, empty sets and so on, the DOM isn't. We already had regressions from a previous patch with undefined elements
    • The MR is too big to review and make sure there are no regressions (even with the tests we already have), it would create unstability that we don't have to endure
    • Every time I review this MR, I find a new bug
    • Sometimes the jQuery code is simply more readable, I do not think this change is a net positive:
                $details
                  .filter('[data-drupal-system-state="forced-open"]')
                  .removeAttr('data-drupal-system-state')
                  .attr('open', false);
                $details.forEach((detail) => {
                  if (
                    detail.getAttribute('data-drupal-system-state') === 'forced-open'
                  ) {
                    detail.removeAttribute('data-drupal-system-state');
                    detail.setAttribute('open', 'false');
                  }
                });
      
      

      There is one line to change and just noticed a bug (jQuery doesn't have a forEach method)

    I ported the credits to ๐Ÿ“Œ Credit for work on the reduce jQuery issues Active , which I will mark as fixed as soon as I go through all the others impacted jQuery issues.

Production build 0.71.5 2024