Verified Commit 1bf09c29 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3328425 CKEditor 5 balloons invisible when CKEditor 5 is used inside a modal

By: cedewey
By: mherchel
By: hudri
By: realityloop
By: scott_euser
By: cecrs
By: alexpott
By: nod_
By: arousseau
By: lauriii
By: quietone
(cherry picked from commit 75a5600d)
parent e48bf570
Loading
Loading
Loading
Loading
Loading
+7 −2
Changes for core/modules/ckeditor5/css/ckeditor5.dialog.fix.css: 7 added lines, 2 removed lines.
Original line number Diff line number Diff line
.ui-dialog ~ .ck-body-wrapper {
  --ck-z-panel: 1261;
.ck-body-wrapper {
  --ck-z-panel: calc(var(--ck-z-default) + 1261);
  /* CKEditor 5 49.0.0 and later resolves these on :root, so they need to be
     redeclared here to use the value of --ck-z-panel above. */
  --ck-layer-panel: var(--ck-z-panel);
  --ck-layer-panel-above: calc(var(--ck-layer-panel) + 1);
  --ck-layer-panel-below: calc(var(--ck-layer-panel) - 1);
}
+7
Changes for core/modules/ckeditor5/tests/modules/ckeditor5_test/ckeditor5_test.routing.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -11,3 +11,10 @@ ckeditor5_test.dialog:
    _controller: '\Drupal\ckeditor5_test\Controller\CKEditor5DialogTestController::testDialog'
  requirements:
    _access: 'TRUE'

ckeditor5_test.dialog_with_editor:
  path: '/ckeditor5_test/dialog_with_editor'
  defaults:
    _form: '\Drupal\ckeditor5_test\Form\CKEditor5DialogTestForm'
  requirements:
    _access: 'TRUE'
+61
Changes for core/modules/ckeditor5/tests/modules/ckeditor5_test/src/Form/CKEditor5DialogTestForm.php: 61 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\ckeditor5_test\Form;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Provides a form with CKEditor 5 and a link that opens a modal dialog.
 *
 * The editor on this page is initialized before the dialog opens. This places
 * the CKEditor 5 body wrapper before the jQuery UI dialog in the DOM.
 */
class CKEditor5DialogTestForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'ckeditor5_test_dialog_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['text'] = [
      '#type' => 'text_format',
      '#title' => 'Text',
      '#format' => 'test_format',
      '#allowed_formats' => ['test_format'],
    ];
    $form['link'] = [
      '#type' => 'link',
      '#title' => 'Add Node',
      '#url' => Url::fromRoute('entity.node.add_form', ['node_type' => 'page']),
      '#attributes' => [
        'class' => ['use-ajax'],
        'data-dialog-type' => 'dialog',
        'data-dialog-options' => Json::encode([
          'width' => 700,
          'modal' => TRUE,
          'autoResize' => TRUE,
        ]),
      ],
    ];
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
  }

}
+76 −23
Changes for core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5DialogTest.php: 76 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -37,6 +37,82 @@ class CKEditor5DialogTest extends CKEditor5TestBase {
   * Tests if CKEditor 5 tooltips can be interacted with in dialogs.
   */
  public function testCKEditor5FocusInTooltipsInDialog(): void {
    $this->createLinkTextFormat();

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $this->drupalGet('/ckeditor5_test/dialog');
    $page->clickLink('Add Node');
    $assert_session->waitForElementVisible('css', '[role="dialog"]');
    $assert_session->assertWaitOnAjaxRequest();

    $content_area = $assert_session->waitForElementVisible('css', '.ck-editor__editable');
    // Focus the editable area first.
    $content_area->click();
    // Then press the button to add a link.
    $this->pressEditorButton('Link');

    $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.
    $input->setValue($link_url);
    // Save the new link.
    $page->find('xpath', "//button[span[text()='Insert']]")->click();
    // Make sure something was added to the text.
    $this->assertNotEmpty($content_area->getText());
  }

  /**
   * Tests balloons in a dialog opened after an editor exists on the page.
   *
   * CKEditor 5 appends a single shared body wrapper to the page when the first
   * editor is created. If that happens before a jQuery UI dialog is opened,
   * the wrapper is placed before the dialog in the DOM.
   */
  public function testBalloonVisibleInDialogOpenedAfterEditor(): void {
    // Stark does not raise the z-index of jQuery UI dialogs above CKEditor 5
    // balloons, so use a theme that does.
    \Drupal::service('theme_installer')->install(['default_admin']);
    $this->config('system.theme')->set('default', 'default_admin')->save();
    $this->createLinkTextFormat();

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $this->drupalGet('/ckeditor5_test/dialog_with_editor');
    $this->waitForEditor();
    $page->clickLink('Add Node');
    $dialog = $assert_session->waitForElementVisible('css', '.ui-dialog');
    $assert_session->assertWaitOnAjaxRequest();

    // Confirm the body wrapper was created before the dialog.
    $this->assertTrue($this->getSession()->evaluateScript(<<<JS
      document.querySelector('.ck-body-wrapper ~ .ui-dialog') !== null
    JS));

    $content_area = $assert_session->waitForElementVisible('css', '.ui-dialog .ck-editor__editable');
    $content_area->click();
    $dialog->find('xpath', "//button[span[text()='Link']]")->click();
    $assert_session->waitForElementVisible('css', '.ck-balloon-panel_visible input.ck-input-text');

    // Make sure the link balloon is the topmost element at its own center
    // point, and is not covered by the dialog. Other visible balloons, such as
    // the "Powered by" balloon, may be positioned off-screen.
    $this->assertSame('balloon', $this->getSession()->evaluateScript(<<<JS
      (function () {
        const balloon = document.querySelector('.ck-balloon-panel_visible input.ck-input-text').closest('.ck-balloon-panel');
        const rect = balloon.getBoundingClientRect();
        const element = document.elementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2);
        return balloon.contains(element) ? 'balloon' : (element ? element.className : 'none');
      })()
    JS));
  }

  /**
   * Creates a text format with CKEditor 5 and the link button.
   */
  protected function createLinkTextFormat(): void {
    FilterFormat::create([
      'format' => 'test_format',
      'name' => 'CKEditor 5 with link',
@@ -64,29 +140,6 @@ function (ConstraintViolationInterface $v): string {
        FilterFormat::load('test_format')
      ))
    ));

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $this->drupalGet('/ckeditor5_test/dialog');
    $page->clickLink('Add Node');
    $assert_session->waitForElementVisible('css', '[role="dialog"]');
    $assert_session->assertWaitOnAjaxRequest();

    $content_area = $assert_session->waitForElementVisible('css', '.ck-editor__editable');
    // Focus the editable area first.
    $content_area->click();
    // Then press the button to add a link.
    $this->pressEditorButton('Link');

    $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.
    $input->setValue($link_url);
    // Save the new link.
    $page->find('xpath', "//button[span[text()='Insert']]")->click();
    // Make sure something was added to the text.
    $this->assertNotEmpty($content_area->getText());
  }

  /**