Problem/Motivation

Hovering an embedded component shows CKEditor's "Insert paragraph before block" and "Insert paragraph after block" buttons. The empty line they add should go away with Backspace, as it does next to any other widget, but in two cases it stays:

  • The line above the first embed, in every browser: Backspace does nothing.
  • The line between two embeds, in Safari: Backspace does nothing. In Chrome this one works.

Reproduced on SDC Embed 1.x (same code as 1.2.0) with Drupal 11.4.9 and Olivero, in Chrome 138 and in Safari on macOS.

Steps to reproduce

  1. Install Drupal with the Standard profile and enable SDC Embed. At Configuration > Text formats and editors > Full HTML, add the "Embed component" button and enable the "Embed Single Directory Components" filter.
  2. Create a Basic page in Full HTML and embed "Teaser — olivero" twice, one after the other.
  3. Hover the first embed and click the ↵ button at its top left. An empty line appears above it, with the caret in it. Press Backspace.
  4. In Safari: hover the first embed and click the ↵ button at its bottom right. An empty line appears between the two embeds. Press Backspace.

Expected: the empty line is removed and the embed next to it is selected, as CKEditor does next to other widgets.
Actual: the empty line stays, however many times Backspace is pressed.

Root cause

CKEditor's Widget plugin removes an empty block next to an object in Widget::_handleDelete(), which runs on the view's delete event. Two things keep it from running here:

  • Start of the editable. _getObjectElementNextToSelection() only looks for an object in the direction of the key. Before the first block there is nowhere to go, so it returns null and nothing happens. Forward Delete on the same line works. This is CKEditor's own behaviour: the same happens above a core Horizontal line.
  • Safari. CKEditor fires the delete event from the browser's beforeinput. Between two non-editable widgets, Backspace in Safari never gets that far, so the line stays.

Proposed resolution

Handle the empty block on keydown, which every browser fires. When Backspace or Delete (without modifiers) is pressed in an empty block next to a drupalSdc element, remove the block and select the embed: Backspace takes the one before, Delete the one after, and either takes the other one when there is nothing on its own side. The change is one model batch, so undo brings the line back.

It has to be keydown, at high priority, and it has to stop the event. In Blink, CKEditor's DeleteObserver records the key on keydown and, on keyup, fires a second delete if no beforeinput came and the selection is no longer collapsed. With the embed now selected, that second delete would remove the embed too. Stopping the keydown before the observer sees it keeps that from happening.

Any other block, or an empty block next to something that is not an embed, keeps CKEditor's default behaviour.

With the fix, both cases remove the line and select the embed, in Chrome and in Safari.

The MR adds a FunctionalJavascript test, SdcEmbedDeleteKeysTest, that sends real WebDriver key actions (so the browser fires its own keydown, beforeinput and keyup). It covers Backspace above the first embed, Backspace and Delete between two embeds, and Backspace on an empty line after a paragraph, which must stay untouched. The test runs in Chrome, so without the fix only the first case fails; the Safari case was checked by hand.

Remaining tasks

Review.

User interface changes

None.

API changes

None.

Data model changes

None.

AI usage

AI-Generated: Yes. I used Claude Code (Anthropic) to write the fix, the FunctionalJavascript test and a first draft of this summary. I reproduced both cases and verified the fix in Chrome and Safari, reviewed the code, and confirmed the new test fails without the fix and passes with it.

Issue fork sdc_embed-3630624

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

tiago created an issue. See original summary.

tiago’s picture

Status: Active » Needs review
StatusFileSize
new8.48 KB

MR !4 is ready for review and its pipeline passes.
Attached below is the MR diff as a patch for 1.2.0.

c2h2o5’s picture

Thank you for your contribution. It works perfectly.
It will be released with module version 1.3.0

  • c2h2o5 committed b5f00089 on 1.x
    Issue #3630624: Remove the empty block beside an embed on Backspace and...
c2h2o5’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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