Postponed by #3113649: Remove drupal.tabbingmanager's jQueryUI dependency

Problem/Motivation

The :tabbable selector is part of jQuery UI, which has reached end of life. A shim for this selector was added in #3113649: Remove drupal.tabbingmanager's jQueryUI dependency. Now that this is available, we should deprecate :tabbable and refactor all existing uses to use methods from the newly added Tabbable library (and here's where I acknowledge it's confusing to replace the :tabbable selector with methods from a new library called Tabbable).

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3179734

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

bnjmnm created an issue. See original summary.

bnjmnm’s picture

StatusFileSize
new44.53 KB

This includes the most recent patch from #3113649: Remove drupal.tabbingmanager's jQueryUI dependency which isn't committed yet but appears close.

This includes the addition of a new AjaxCommand to get around f Media Library using InvokeCommand to find the first focusable element in a form. The Ajax commands expect jQuery, but with :tabbable deprecated the first focusable needs to be found + focused differently. This may need to be scoped to it's own issue but adding here for now as seeing it in a patch is easier than a long explanation.

xjm’s picture

bnjmnm’s picture

Title: [PP-1] refactor uses of the :tabbable selector and deprecate it » [PP-2] refactor uses of the :tabbable selector and deprecate it
Related issues: +#3188938: Create AjaxCommand for focusing that does not require :tabbable selector

The AJAX command is being added in #3188938: Create AjaxCommand for focusing that does not require :tabbable selector instead of here, so this is now postponed on that issue as well.

nod_’s picture

Title: [PP-2] refactor uses of the :tabbable selector and deprecate it » [PP-1] refactor uses of the :tabbable selector and deprecate it
bnjmnm’s picture

Title: [PP-1] refactor uses of the :tabbable selector and deprecate it » Refactor uses of the :tabbable selector and deprecate it
Version: 9.1.x-dev » 9.2.x-dev
Status: Active » Needs review
StatusFileSize
new14.73 KB

Adding patch because the merge request was opened against 9.1 and I'm not sure how to update that + DA people are busy with Drupalcon right now.

bnjmnm’s picture

Issue tags: +NorthAmerica2021
bnjmnm’s picture

Status: Needs review » Active
bnjmnm’s picture

Status: Active » Needs review
zrpnr’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.65 KB

The _focusTabbable function in core/misc/dialog/dialog.jquery-ui.es6.js
overrides the original in core/assets/vendor/jquery.ui/ui/widgets/dialog.js

+++ b/core/misc/dialog/dialog.jquery-ui.es6.js
@@ -30,5 +30,35 @@
+      if (!hasFocus) {
...
+      if (!hasFocus) {
...
+      if (!hasFocus) {
...
+      if (!hasFocus) {

In the original function after the first check for !hasFocus the following if statements check for !hasFocus.length
Seems like your version is more correct however since this._focusedElement could be null.


+++ b/core/misc/dialog/dialog.jquery-ui.es6.js
@@ -30,5 +30,35 @@
+      $(hasFocus).eq(0).trigger('focus');

This fooled me for a minute because I thought that some of these conditions return a jQuery object already, while others return a DOM node, but then I noticed the get(0) wherever a jQuery find is used, which ensures you always get the element directly.

This also helped me understand why in the original function they had hasFocus.length - since after the first if statement it could be an empty jQuery object with length 0. In your version it it will just be undefined until there's a match.

Your version is an improvement IMO!

The override in tabbingmanager.es6.js is more straightforward- but

+++ b/core/misc/tabbingmanager.es6.js
@@ -136,13 +136,19 @@
-        const $elements = $(elements).find(':tabbable').addBack(':tabbable');
+        let tabbableElements = [];
+        elements.each((index, rootElement) => {
+          tabbableElements = [...tabbableElements, ...tabbable(rootElement)];
+          if (isTabbable(rootElement)) {
+            tabbableElements = [...tabbableElements, rootElement];

I was thrown off by the addBack. It seems like that's covered here by checking for the rootElement tabbable descendents and then checking if rootElement itself isTabbable. Smart!

+++ b/core/modules/media_library/js/media_library.ui.es6.js
@@ -103,8 +103,9 @@
-            $('#media-library-content :tabbable:first').focus();
-
+            tabbable(
+              document.getElementById('media-library-content'),
+            )[0].focus();

Manually tested this by opening a media library dialog, clicking another tab. The first input was correctly focused.

+++ b/core/modules/media_library/src/Form/AddFormBase.php
@@ -608,7 +609,7 @@ public function updateFormCallback(array &$form, FormStateInterface $form_state)
-        $response->addCommand(new InvokeCommand('#media-library-add-form-wrapper :tabbable', 'focus'));
+        $response->addCommand(new FocusFirstCommand('#media-library-add-form-wrapper'));

The InvokeCommand was previously calling focus on any :tabbable children of #media-library-add-form-wrapper, FocusFirstCommand uses focusFirst in ajax.es6.js to grab the first tabbable element, added in #3188938: Create AjaxCommand for focusing that does not require :tabbable selector. Perfect follow-up use of that method!

The only nits I found were 2 places where comments would now be outdated, referring to ":tabbable"
core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
(see interdiff)

I don't feel strongly about those, IMHO this is ready. Really nice work @bnjmnm !

The last submitted patch, 6: 3179734-6.patch, failed testing. View results

zrpnr’s picture

Status: Reviewed & tested by the community » Needs work

Bumping back to NW for the test failure that came through after.

bnjmnm’s picture

Status: Needs work » Needs review
StatusFileSize
new16.6 KB
new1.65 KB

The test failure is from CKEditorIntegrationTest, one of those "unrelated and known to intermittently fail" tests. Used this as an opportunity to update those ":tabbable" uses mentioned in #10

bnjmnm’s picture

StatusFileSize
new16.38 KB

There were file permissions changes in #13 that shouldn't have been there.

zrpnr’s picture

Status: Needs review » Reviewed & tested by the community

Apparently the test failure in #11 is intermittent and unrelated.
The patch in #14 makes the 2 comment changes I suggested, no other changes.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests
+++ b/core/misc/dialog/dialog.jquery-ui.es6.js
@@ -30,5 +30,35 @@
+      if (!hasFocus) {
+        hasFocus = tabbable(this.element.get(0));
+      }
+      if (!hasFocus) {
+        hasFocus = tabbable(this.uiDialogButtonPane);
+      }

Based on https://github.com/focus-trap/tabbable#tabbable, tabbable() returns always an array. After that we are testing [] which in JavaScript is a truthy value.

Based on that I did some testing and found out that this is broken in cases where the first tabbable() call didn't find tabbbable values. 😅 That leads into some problems because the dialog doesn't end up focused and therefore tabbing results in focusing items outside the dialog until the user reaches the dialog which could be in the very end of the page.

This points out that we are lacking test coverage in some pretty critical parts of the code. Based on this experience, I think we should add some more tests before committing this.

zrpnr’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new26.6 KB
new27.22 KB
new12.29 KB

@lauriii is correct about the truthy array preventing the correct element from being focused. I also found that tabbable errors out if you try to pass it a non-element.

Added FunctionalJavascript tests for the different focus scenarios:

  • an input with autofocus with a tabbable item before it
  • the tabbable item in content before the input
  • a button in the dialog buttonpane
  • the dialog close button
  • the dialog itself

Updated the code in the overridden function to pass these tests.

The last submitted patch, 17: 3179734-17-test-only.patch, failed testing. View results

bnjmnm’s picture

Status: Needs review » Needs work
  1. +++ b/core/misc/dialog/dialog.jquery-ui.es6.js
    @@ -35,26 +35,32 @@
    +      // 3. Tabbable element inside the content element
    +      // 4. Tabbable element inside the buttonpane.
    

    I like the steps being attached to the relevant code like this! Easier to understand. A few nits that accompanied it:
    - Some of the steps end with punctuation, while others don't. The way it's formatted now, I think it works best with punctuation.

    -There should be a line of whitespace preceding each inline comment (not the case in all of core but I've had stuff kicked back for that).

  2. +++ b/core/modules/system/tests/modules/dialog_renderer_test/dialog_renderer_test.routing.yml
    @@ -13,3 +13,19 @@ dialog_renderer_test.modal_content:
    +    _title: 'Thing 2'
    

    The most inconsequential nit: could this be a 'Thing 3' since there's already a 'Thing 2' above?

  3. +++ b/core/modules/system/tests/modules/dialog_renderer_test/src/Controller/TestController.php
    @@ -22,6 +23,49 @@ public function modalContent() {
    +        '#markup' => 'Look at me in a modal!<br><a href="#">And a link!</a></a>',
    

    Extra closing tag here and in modalContentInput()

  4. +++ b/core/modules/system/tests/modules/dialog_renderer_test/src/Controller/TestController.php
    @@ -74,6 +118,74 @@ public function linksDisplay() {
    +      'no_close_modal' => [
    +        '#title' => 'Hidden close button modal!',
    +        '#type' => 'link',
    +        '#url' => Url::fromRoute('dialog_renderer_test.modal_content'),
    

    Nice that all the possibilities are covered, even unlikely ones like no close button!

  5. +++ b/core/modules/system/tests/src/FunctionalJavascript/ModalRendererTest.php
    @@ -32,12 +32,50 @@ public function testModalRenderer() {
    +    // Tabbable should focus the close button when it is the only tabbable item.
    +    $this->assertJsCondition('jQuery(".ui-dialog .ui-dialog-titlebar-close").is(":focus")');
    

    Similar to prior nit, this and a few other inline comments in the test can get a whitespace line above them.

  6. The tests look good, what follows is pretty much nitty.

  7. +++ b/core/modules/system/tests/src/FunctionalJavascript/ModalRendererTest.php
    @@ -32,12 +32,50 @@ public function testModalRenderer() {
    +    $this->assertJsCondition('jQuery(".ui-dialog").is(":focus")');
    

    Could these focus assertions remove the jQuery dependency? Something like document.activeElement === document.querySelector('.ui-dialog')? It will probably be a long time before dialogs have no jQuery dependency, but the vanilla approach may prevent some later debt.

zrpnr’s picture

Status: Needs work » Needs review
StatusFileSize
new27.54 KB
new7.53 KB

Thanks @bnjmnm - I added the punctuation and spacing you suggested, and fixed the extra closing tag in the test content.

could this be a 'Thing 3' since there's already a 'Thing 2'

I didn't want to spoil the Dr Seuss joke :) but also because that markup was identical to the previous except for the autofocus attribute, to prove that it took precedence. Totally fine with changing it though!

Could these focus assertions remove the jQuery dependency?

Good call, I had left these as close to what you had in other tests, such as MediaLibraryTestBase, where the test change was really minimal.
Changed to your suggestion in ModalRendererTest,
I didn't change the similar assertions in EditModeTest, MediaLibraryTestBase or EntityReferenceWidgetTest since those tests had existing use of jQuery and it's more clear that the assertion hasn't changed if it isn't rewritten to remove the jQuery usage.

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

Re #20

Changed to your suggestion in ModalRendererTest,
I didn't change the similar assertions in EditModeTest, MediaLibraryTestBase or EntityReferenceWidgetTest since those tests had existing use of jQuery and it's more clear that the assertion hasn't changed if it isn't rewritten to remove the jQuery usage.

That's in line with what I was thinking. There are existing uses that would be out of scope to change in this issue, I just wanted to avoid adding more jQuery here when a no-dependency option was available.

This was RTBC at #15, I feel comfortable switching back to RTBC as @zrpnr has done the work since the previous RTBC to address #16, which I've reviewed and it looks good!

bnjmnm’s picture

Status: Reviewed & tested by the community » Needs work

There's a bit of code duplication that needs to be addressed, I suspect @zrpnr has a new patch on the way, and I'll give it another look then.

zrpnr’s picture

Status: Needs work » Needs review
StatusFileSize
new27.08 KB
new1.67 KB

This removes the duplicate test and fixes a comment.

lauriii’s picture

Title: Refactor uses of the :tabbable selector and deprecate it » Refactor uses of the :tabbable selector and deprecate it
  1. Based on the issue summary, this issue should deprecate the :tabbable selector. If that's not supposed to be in the scope of this issue, we should retitle and open follow-up for deprecating it.
  2. Shouldn't this remove dependency on core/tabbable.jquery.shim from core/drupal.tabbingmanager library?
  3. I think that the change record could still use some more work since it seems like the description field is still empty.
bnjmnm’s picture

#24 is correct, the selector should be deprecated (can be added to core/misc/jquery.tabbable.shim.es6.js), the core/tabbable.jquery.shim dependency can be removed, and the change record needs creating.

bnjmnm’s picture

Status: Needs review » Needs work
zrpnr’s picture

Status: Needs work » Needs review
StatusFileSize
new29 KB
new1.96 KB

Added the deprecation message to jquery.tabbable.shim.es6.js, and added core/drupal as a dependency so that the message appears.
Removed the shim dependency from tabbing manager

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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
StatusFileSize
new214.46 KB

Confirmed the deprecation is triggered.

I spotted a few more things, but this is close.

  1. +++ b/core/misc/tabbingmanager.es6.js
    @@ -136,13 +136,19 @@
             // The "active tabbing set" are the elements tabbing should be constrained
             // to.
    -        const $elements = $(elements).find(':tabbable').addBack(':tabbable');
    +        let tabbableElements = [];
    +        elements.each((index, rootElement) => {
    

    The 'elements' argument can be an element set OR a jQuery-compatible selector string. This approach currently won't work with a selector string.

  2. +++ b/core/modules/contextual/tests/src/FunctionalJavascript/EditModeTest.php
    @@ -131,7 +131,7 @@ protected function assertAnnounceLeaveEditMode() {
    +    $this->getSession()->executeScript("jQuery(window.tabbable.tabbable(document.body)).attr('data-marked', '');");
    

    Not an item that needs addressing, but just highlighting that although refactoring away :tabbable makes this script string longer, keeping the jQuery in there keeps it from becoming an even longer "window.tabbable.tabbable(document.body).forEach((element) => element.setAttribute('data-marked', ''));"

  3. +++ b/core/modules/media_library/js/media_library.ui.es6.js
    @@ -103,8 +103,9 @@
    +            tabbable(
    +              document.getElementById('media-library-content'),
    +            )[0].focus();
    

    Given that it's VERY improbable #media-library-content would not have a tabbable element. But, were that the case, this would error out and break the JS that accompanies it.

  4. +++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php
    @@ -342,7 +342,7 @@ protected function assertMediaAdded($index = 0) {
    -    $this->assertJsCondition('jQuery("#media-library-add-form-wrapper :tabbable").is(":focus")');
    +    $this->assertJsCondition('jQuery(tabbable.tabbable(document.getElementById("media-library-content"))[0]).is(":focus")');
    

    This is looking in #media-library-content, but the previous assertion was looking in #media-library-add-form-wrapper. This still passes since the first tabbable element in #media-library-content happens to also be the first tabbable element in #media-library-add-form-wrapper, but this should continue to check within the same selector.

  5. +++ b/core/misc/jquery.tabbable.shim.es6.js
    @@ -6,6 +6,10 @@
    +          'The :tabbable selector is deprecated in Drupal 9.2.0 and will be removed in Drupal 10.0.0. Use the core/tabbable library instead. See https://www.drupal.org/node/3179734',
    

    This should link to the CR instead of this issue

  6. +++ b/core/misc/jquery.tabbable.shim.es6.js
    @@ -6,6 +6,10 @@
           // The tabbable library considers the summary element tabbable, and also
    

    Add a WS line before this comment

zrpnr’s picture

Status: Needs work » Needs review
StatusFileSize
new29.95 KB
new6.19 KB

#29.1 Wrapped this in a jQuery function to ensure if a selector or other arg was passed in that it would be a jQuery object before .each is called. Also updated the argument list in the jsdoc to be more thorough.
In core there's only one place I found and it is definitely passing a jQuery object, in core/modules/contextual/js/toolbar/views/AuralView.es6.js.

#29.2 👍

#29.3 Just to be safe I added some extra checks so that a null value isn't passed to tabbable and to make sure the array is not empty.

#29.4 Good catch! Restored the previous selector

#29.5 Corrected the link to the CR

#29.6 Added extra linebreak

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

#30 successfully addresses all my review feedback, so RTBC.
One thing to note:

+++ b/core/misc/tabbingmanager.es6.js
@@ -116,9 +116,9 @@
        * the tab key.
        *
-       * @param {jQuery} elements
+       * @param {jQuery|Selector|Element|ElementArray|object|selection} elements
        *   The set of elements to which tabbing should be constrained. Can also
-       *   be a jQuery-compatible selector string.
+       *   be any jQuery-compatible argument.
        *

To clarify for any other reviewers, the change @zrpnr made to the docs here is not due to changed functionality. The prior documentation was not accurate. Correcting this makes the changes within the function easier to understand.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
  1. Would it be possible to add test coverage for the deprecation message?
  2. +++ b/core/core.libraries.yml
    @@ -791,6 +791,7 @@ tabbable.jquery.shim:
    +    - core/drupal
    

    What is this change for?

  3. Can we expand the change record to include libraries that has dropped dependency on the shim because some code could depend on those?
bnjmnm’s picture

#32.2, the addition of core/drupal to the shim is so the shim can call Drupal.deprecationError when the :tabbable selector is used.

#32.3

Can we expand the change record to include libraries that has dropped dependency on the shim because some code could depend on those?

The dependency being removed in this patch is one that was added in the dev branch for 9.2 (the same branch this is intended to go in). I'm concerned mentioning this in the CR would be confusing as it wouldn't be documenting the difference between releases. I do think this would be needed if landing this gets postponed to 9.3.

zrpnr’s picture

Status: Needs work » Needs review
StatusFileSize
new30.65 KB
new650 bytes

#32.1 Added a js deprecation check in the nightwatch test
#32.2 I added that dependency since Drupal.deprecationError is in drupal.js

zrpnr’s picture

StatusFileSize
new30.65 KB
new634 bytes

🤦‍♂️missing a semicolon

zrpnr’s picture

StatusFileSize
new32.14 KB
new1.71 KB

@bnjmnm and I discovered a bug in the Nightwatch test, it was no longer testing the tabbable shim after removing that dependency from core/drupal.tabbingmanager in #27.
The tests were still passing due to the error messages being equal ... 😬

Added additional assertions to prove the values should be numbers and not errors, updated the test module library to be core/jquery.ui instead - which has a dependency on the tabbable shim.

Added an additional deprecation check, the tabbable shim test should have triggered the warning, the fact that it didn't proved the test was not working properly. Thanks @lauriii for pointing us in the right direction to find this problem!

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

Since we've been through a few rounds of near-rtbc, I'm focusing on the diff and it's looking good:

  1. +++ b/core/modules/system/tests/modules/tabbable_shim_test/src/Controller/TabbableShimTestController.php
    @@ -23,7 +23,7 @@ public function build() {
    -      '#attached' => ['library' => ['core/drupal.tabbingmanager']],
    +      '#attached' => ['library' => ['core/jquery.ui']],
    

    This should have been the library I attached to begin with, as it is the library directly providing the tabbable selector. Good change.

  2. +++ b/core/tests/Drupal/Nightwatch/Tests/tabbableShimTest.js
    @@ -294,6 +294,8 @@ module.exports = {
             (result) => {
    +          browser.assert.ok(typeof result.value.actual === 'number');
    +          browser.assert.ok(typeof result.value.expected === 'number');
    

    Locally, I switched the library back to core/drupal.tabbingmanager and confirmed these assertions were sufficient to fail a test where the :tabbable selector wasn't available.

  3. +++ b/core/tests/Drupal/Nightwatch/Tests/tabbableShimTest.js
    @@ -302,6 +304,9 @@ module.exports = {
    +    browser.assert.deprecationErrorExists(
    +      'The :tabbable selector is deprecated in Drupal 9.2.0 and will be removed in Drupal 10.0.0. Use the core/tabbable library instead. See https://www.drupal.org/node/3183730',
    +    );
    

    This existing Nightwatch test already triggers this deprecation all over the place, so this is a nice spot to add the deprecation assertion.

lauriii’s picture

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

I had to run yarn build:js before committing because we have updated some yarn dependencies since the patch was generated.

Committed b48ad90 and pushed to 9.3.x. Thanks!

  • lauriii committed b48ad90 on 9.3.x
    Issue #3179734 by zrpnr, bnjmnm, lauriii: Refactor uses of the :tabbable...

  • lauriii committed 1a7ce02 on 9.2.x
    Issue #3179734 by zrpnr, bnjmnm, lauriii: Refactor uses of the :tabbable...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Now also cherry-picked to 9.2.x. ✌️

Status: Fixed » Closed (fixed)

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