From ae2382fcb3df39f69b78e841fefc44e7f2f596a7 Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Sat, 6 Aug 2022 00:12:34 +0300
Subject: [PATCH] Issue #3301631 by nod_, Taran2L, xjm: Regression with
 CKEditor 35.0.1 and modal dialogs

(cherry picked from commit 4efef25d164efc15ddba2fd81c2696840efd7809)
---
 .../ckeditor5/js/ckeditor5.dialog.fix.es6.js  | 19 ++++---------------
 .../ckeditor5/js/ckeditor5.dialog.fix.js      |  9 ++-------
 .../CKEditor5DialogTest.php                   |  4 ----
 3 files changed, 6 insertions(+), 26 deletions(-)

diff --git a/core/modules/ckeditor5/js/ckeditor5.dialog.fix.es6.js b/core/modules/ckeditor5/js/ckeditor5.dialog.fix.es6.js
index 14e388ca6ace..1f4ed1d7feb5 100644
--- a/core/modules/ckeditor5/js/ckeditor5.dialog.fix.es6.js
+++ b/core/modules/ckeditor5/js/ckeditor5.dialog.fix.es6.js
@@ -7,23 +7,12 @@
  */
 
 (($) => {
-  // Get core version of the _focusTabbable method.
-  const oldFocusTabbable = $.ui.dialog._proto._focusTabbable;
-
   $.widget('ui.dialog', $.ui.dialog, {
-    // Override core override of jQuery UI's `_focusTabbable()` so that
+    // Override core override of jQuery UI's `_allowInteraction()` so that
     // CKEditor 5 in modals can work as expected.
-    _focusTabbable() {
-      // When the focused element is a CKEditor 5 instance, disable jQuery UI
-      // focus trap and delegate focus trap to CKEditor 5.
-      const hasFocus = this._focusedElement
-        ? this._focusedElement.get(0)
-        : null;
-      // In case the element is a CKEditor 5 instance, do not change focus
-      // management.
-      if (!(hasFocus && hasFocus.ckeditorInstance)) {
-        oldFocusTabbable.call(this);
-      }
+    // @see https://api.jqueryui.com/dialog/#method-_allowInteraction
+    _allowInteraction(event) {
+      return event.target.classList.contains('ck') || this._super(event);
     },
   });
 })(jQuery);
diff --git a/core/modules/ckeditor5/js/ckeditor5.dialog.fix.js b/core/modules/ckeditor5/js/ckeditor5.dialog.fix.js
index 355e776e755b..e0b9531cab53 100644
--- a/core/modules/ckeditor5/js/ckeditor5.dialog.fix.js
+++ b/core/modules/ckeditor5/js/ckeditor5.dialog.fix.js
@@ -6,14 +6,9 @@
 **/
 
 ($ => {
-  const oldFocusTabbable = $.ui.dialog._proto._focusTabbable;
   $.widget('ui.dialog', $.ui.dialog, {
-    _focusTabbable() {
-      const hasFocus = this._focusedElement ? this._focusedElement.get(0) : null;
-
-      if (!(hasFocus && hasFocus.ckeditorInstance)) {
-        oldFocusTabbable.call(this);
-      }
+    _allowInteraction(event) {
+      return event.target.classList.contains('ck') || this._super(event);
     }
 
   });
diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5DialogTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5DialogTest.php
index 1758b4014092..9f2f97536ae6 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5DialogTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5DialogTest.php
@@ -71,10 +71,6 @@ function (ConstraintViolation $v) {
     // Then press the button to add a link.
     $this->pressEditorButton('Link');
 
-    // @todo Un-skip this test when
-    //   https://www.drupal.org/project/drupal/issues/3301631 is resolved.
-    $this->markTestSkipped('Test skipped due to regression in CKEditor 35.0.0.');
-
     $link_url = '/ckeditor5_test/dialog';
     $input = $assert_session->waitForElementVisible('css', '.ck-balloon-panel input.ck-input-text');
     // Make sure the input field can have focus and we can type into it.
-- 
GitLab