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

Created on 27 September 2021, over 3 years ago
Updated 26 July 2024, 8 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-closest, which targets the jQuery closest function.

Steps to reproduce

Proposed resolution

Remaining tasks

  • In core/.eslintrc.jquery.json Change "jquery/no-closest": 0, to "jquery/no-closest": 2, to enable eslint checks for uses of jQuery .closest(). 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 .closest() 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 21 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.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

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.

  • Merge request !6488Issue #3239531: Refactor closest โ†’ (Open) created by Unnamed author
  • Pipeline finished with Failed
    about 1 year ago
    Total: 550s
    #89678
  • Pipeline finished with Failed
    about 1 year ago
    Total: 502s
    #90377
  • Pipeline finished with Failed
    about 1 year ago
    Total: 579s
    #91241
  • Pipeline finished with Failed
    about 1 year ago
    Total: 497s
    #92867
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ahsannazir

    Can someone look into the MR and confirm why Tests Pipelines are failing. Seems like few test cases are failing but not sure why

  • Issue was unassigned.
  • Pipeline finished with Failed
    about 1 year ago
    Total: 785s
    #93602
  • Pipeline finished with Success
    about 1 year ago
    Total: 567s
    #93699
  • Pipeline finished with Success
    about 1 year ago
    Total: 520s
    #93707
  • Status changed to Needs review about 1 year ago
  • Pipeline finished with Success
    about 1 year ago
    Total: 476s
    #93724
  • Pipeline finished with Success
    about 1 year ago
    Total: 497s
    #93750
  • Pipeline finished with Success
    about 1 year ago
    Total: 482s
    #93791
  • Pipeline finished with Success
    about 1 year ago
    Total: 479s
    #93806
  • Pipeline finished with Success
    about 1 year ago
    Total: 479s
    #93821
  • Pipeline finished with Success
    about 1 year ago
    Total: 600s
    #93851
  • Pipeline finished with Success
    about 1 year ago
    Total: 475s
    #93963
  • Pipeline finished with Success
    about 1 year ago
    Total: 561s
    #93985
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    But moving to NW for the eslint rule change.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 817s
    #94491
  • Pipeline finished with Success
    about 1 year ago
    Total: 615s
    #94506
  • Pipeline finished with Success
    about 1 year ago
    Total: 726s
    #94519
  • Pipeline finished with Success
    about 1 year ago
    Total: 602s
    #94530
  • Pipeline finished with Success
    about 1 year ago
    Total: 534s
    #94540
  • Pipeline finished with Success
    about 1 year ago
    Total: 555s
    #94561
  • Pipeline finished with Success
    about 1 year ago
    Total: 516s
    #94577
  • Pipeline finished with Success
    about 1 year ago
    Total: 500s
    #94584
  • Pipeline finished with Success
    about 1 year ago
    Total: 497s
    #94595
  • Pipeline finished with Success
    about 1 year ago
    Total: 589s
    #94603
  • Pipeline finished with Success
    about 1 year ago
    Total: 484s
    #94611
  • Pipeline finished with Success
    about 1 year ago
    Total: 486s
    #94619
  • Pipeline finished with Failed
    about 1 year ago
    Total: 1098s
    #94631
  • Pipeline finished with Success
    about 1 year ago
    Total: 2844s
    #94646
  • Pipeline finished with Success
    about 1 year ago
    Total: 916s
    #94692
  • Pipeline finished with Success
    about 1 year ago
    Total: 495s
    #94706
  • Pipeline finished with Failed
    about 1 year ago
    Total: 853s
    #94713
  • Pipeline finished with Success
    about 1 year ago
    Total: 508s
    #94760
  • Pipeline finished with Success
    about 1 year ago
    Total: 508s
    #94819
  • Pipeline finished with Success
    about 1 year ago
    Total: 482s
    #94840
  • Pipeline finished with Success
    about 1 year ago
    Total: 527s
    #94866
  • Pipeline finished with Success
    about 1 year ago
    Total: 482s
    #94933
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ahsannazir

    ahsannazir โ†’ changed the visibility of the branch 3239531-refactor-closest to hidden.

  • Status changed to Needs review about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 3239531-refactor-if-feasible to hidden.

  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Left a comment.

  • Pipeline finished with Failed
    about 1 year ago
    #100090
  • Pipeline finished with Success
    about 1 year ago
    Total: 1329s
    #100097
  • Pipeline finished with Success
    about 1 year ago
    Total: 608s
    #100107
  • Pipeline finished with Success
    about 1 year ago
    Total: 494s
    #100126
  • Pipeline finished with Success
    about 1 year ago
    Total: 489s
    #100147
  • Pipeline finished with Success
    about 1 year ago
    Total: 547s
    #100169
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year 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
    about 1 year ago
    Total: 566s
    #104002
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    havent' finished but it'll need more work

  • Pipeline finished with Success
    about 1 year ago
    Total: 485s
    #107668
  • Pipeline finished with Success
    about 1 year ago
    Total: 567s
    #107691
  • Pipeline finished with Success
    about 1 year ago
    Total: 510s
    #107703
  • Status changed to Needs review 11 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shubh511
  • Status changed to Needs work 11 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
    11 months ago
    Total: 784s
    #137296
  • Status changed to Needs review 11 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille
  • Status changed to Needs work 11 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Appears to have javascript test failures.

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan tom konda Kanagawa, Japan

    Tom Konda โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Success
    10 months ago
    Total: 702s
    #164636
  • Pipeline finished with Failed
    10 months ago
    Total: 181s
    #165329
  • Pipeline finished with Failed
    10 months ago
    #165335
  • First commit to issue fork.
  • Pipeline finished with Failed
    9 months ago
    Total: 184s
    #193430
  • Pipeline finished with Failed
    9 months ago
    Total: 638s
    #193433
  • Pipeline finished with Success
    9 months ago
    Total: 509s
    #193506
  • Pipeline finished with Success
    9 months ago
    Total: 627s
    #195404
  • Status changed to Needs review 9 months ago
  • Status changed to RTBC 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Still needs subsystem thumbs up believe

    But applied the MR and did some light testing of checking random features (views, layout builder popups, etc) and didn't notice anything.
    The 2 threads appear to be addressed (I believe)

    So to keep from stalling going to mark as tests are also showing fine.

  • Status changed to Closed: won't fix 8 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
    • Sometimes the jQuery code is simply more readable, I do not think this change is a net positive:
            $(once('filter-guidelines', '.js-filter-guidelines', context))
              .find(':header')
              .hide()
              .closest('.js-filter-wrapper')
              .find('select.js-filter-list')
              .on('change.filterGuidelines', updateFilterGuidelines)
              // Need to trigger the namespaced event to avoid triggering formUpdated
              // when initializing the select.
              .trigger('change.filterGuidelines');
            // Define the closest function for finding the closest ancestor with a given selector
            function closest(element, selector) {
              while (element && !element.matches(selector)) {
                element = element.parentNode;
              }
              return element;
            }
      
            const contextElement = document.querySelector('.js-filter-guidelines');
            const closestWrapper = closest(contextElement, '.js-filter-wrapper');
            if (closestWrapper) {
              const $context = $(contextElement);
              const selectElement = $(closestWrapper).find('select.js-filter-list');
              $context.find(':header').hide();
              selectElement
                .on('change.filterGuidelines', updateFilterGuidelines)
                // Need to trigger the namespaced event to avoid triggering formUpdated
                // when initializing the select.
                .trigger('change.filterGuidelines');
            }
      

      There is one line to change and we have to make many, many other changes for this to work

    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