Unverified Commit b48ad909 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3179734 by zrpnr, bnjmnm, lauriii: Refactor uses of the :tabbable selector and deprecate it

parent 09c38b44
Loading
Loading
Loading
Loading
+2 −1
Changes for core/core.libraries.yml: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -258,6 +258,7 @@ drupal.dialog.ajax:
    - core/drupalSettings
    - core/drupal.ajax
    - core/drupal.dialog
    - core/tabbable

drupal.displace:
  version: VERSION
@@ -366,7 +367,6 @@ drupal.tabbingmanager:
    - core/jquery
    - core/drupal
    - core/tabbable
    - core/tabbable.jquery.shim

drupal.tabledrag:
  version: VERSION
@@ -791,6 +791,7 @@ tabbable.jquery.shim:
  js:
    misc/jquery.tabbable.shim.js: {}
  dependencies:
    - core/drupal
    - core/tabbable
    - core/jquery

+43 −2
Changes for core/misc/dialog/dialog.jquery-ui.es6.js: 43 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
 * Adds default classes to buttons for styling purposes.
 */

(function ($) {
(function ($, { tabbable, isTabbable }) {
  $.widget('ui.dialog', $.ui.dialog, {
    options: {
      buttonClass: 'button',
@@ -30,5 +30,46 @@
        $buttons.eq(index).addClass(opts.buttonPrimaryClass);
      }
    },
    // Override jQuery UI's `_focusTabbable()` so finding tabbable elements uses
    // the core/tabbable library instead of jQuery UI's `:tabbable` selector.
    _focusTabbable() {
      // Set focus to the first match:

      // 1. An element that was focused previously.
      let hasFocus = this._focusedElement ? this._focusedElement.get(0) : null;

      // 2. First element inside the dialog matching [autofocus].
      if (!hasFocus) {
        hasFocus = this.element.find('[autofocus]').get(0);
      }

      // 3. Tabbable element inside the content element.
      // 4. Tabbable element inside the buttonpane.
      if (!hasFocus) {
        const $elements = [this.element, this.uiDialogButtonPane];
        for (let i = 0; i < $elements.length; i++) {
          const element = $elements[i].get(0);
          if (element) {
            const elementTabbable = tabbable(element);
            hasFocus = elementTabbable.length ? elementTabbable[0] : null;
          }
          if (hasFocus) {
            break;
          }
        }
      }

      // 5. The close button.
      if (!hasFocus) {
        const closeBtn = this.uiDialogTitlebarClose.get(0);
        hasFocus = closeBtn && isTabbable(closeBtn) ? closeBtn : null;
      }

      // 6. The dialog itself.
      if (!hasFocus) {
        hasFocus = this.uiDialog.get(0);
      }
      $(hasFocus).eq(0).trigger('focus');
    },
  });
})(jQuery);
})(jQuery, window.tabbable);
+39 −2
Changes for core/misc/dialog/dialog.jquery-ui.js: 39 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@
* @preserve
**/

(function ($) {
(function ($, _ref) {
  var tabbable = _ref.tabbable,
      isTabbable = _ref.isTabbable;
  $.widget('ui.dialog', $.ui.dialog, {
    options: {
      buttonClass: 'button',
@@ -32,6 +34,41 @@
      if (typeof primaryIndex !== 'undefined') {
        $buttons.eq(index).addClass(opts.buttonPrimaryClass);
      }
    },
    _focusTabbable: function _focusTabbable() {
      var hasFocus = this._focusedElement ? this._focusedElement.get(0) : null;

      if (!hasFocus) {
        hasFocus = this.element.find('[autofocus]').get(0);
      }

      if (!hasFocus) {
        var $elements = [this.element, this.uiDialogButtonPane];

        for (var i = 0; i < $elements.length; i++) {
          var element = $elements[i].get(0);

          if (element) {
            var elementTabbable = tabbable(element);
            hasFocus = elementTabbable.length ? elementTabbable[0] : null;
          }

          if (hasFocus) {
            break;
          }
        }
      }

      if (!hasFocus) {
        var closeBtn = this.uiDialogTitlebarClose.get(0);
        hasFocus = closeBtn && isTabbable(closeBtn) ? closeBtn : null;
      }

      if (!hasFocus) {
        hasFocus = this.uiDialog.get(0);
      }

      $(hasFocus).eq(0).trigger('focus');
    }
  });
})(jQuery);
 No newline at end of file
})(jQuery, window.tabbable);
 No newline at end of file
+5
Changes for core/misc/jquery.tabbable.shim.es6.js: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,11 @@
(($, Drupal, { isTabbable }) => {
  $.extend($.expr[':'], {
    tabbable(element) {
      Drupal.deprecationError({
        message:
          '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',
      });

      // The tabbable library considers the summary element tabbable, and also
      // considers a details element without a summary tabbable. The jQuery UI
      // :tabbable selector does not. This is due to those element types being
+4
Changes for core/misc/jquery.tabbable.shim.js: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,10 @@
  var isTabbable = _ref.isTabbable;
  $.extend($.expr[':'], {
    tabbable: function tabbable(element) {
      Drupal.deprecationError({
        message: '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'
      });

      if (element.tagName === 'SUMMARY' || element.tagName === 'DETAILS') {
        var tabIndex = element.getAttribute('tabIndex');

Loading