Skip to content

fix(block-events): remove line breaks with Backspace and Delete in Tools with line breaks - #3043

Open
b84330808 wants to merge 2 commits into
codex-team:nextfrom
b84330808:fix-backspace-line-breaks
Open

b84330808 wants to merge 2 commits into
codex-team:nextfrom
b84330808:fix-backspace-line-breaks

Conversation

@b84330808

Copy link
Copy Markdown

Fixes #2854. The same bug is reported in #2730, #2685 and #2609.

In a Tool with enableLineBreaks (Quote, or a custom contenteditable Tool), each Enter makes the browser wrap the new line in a <div>, and Dom.findAllInputs() lists every one of those divs as a separate input. So Backspace at the start of any line after the first is caught by BlockEvents.backspace(), which calls Caret.navigatePrevious(): the caret jumps up a line and the line break stays. Delete does the same at the end of a line (navigateNext()). Repro on the editorjs.io demo: add a Quote, type something, press Enter a few times, then Backspace.

backspace() and delete() now leave the key to the browser when the caret's input is a line inside a contenteditable that has visible content (text, a <br> or another line) before it, for Backspace, or after it, for Delete. At the start of the first line and the end of the last line the Block behaviour is unchanged, and separate inputs like the Quote text and caption are not affected.

New cases in Backspace.cy.ts and Delete.cy.ts use a Tool with enableLineBreaks. The four that remove an empty line or join two lines fail on next and pass with this change; the two that check the first/last line Block behaviour pass on both. The full Cypress suite passes in Chrome.

Comment thread src/components/modules/blockEvents.ts Outdated
* Caret is at the start of a line inside a contenteditable, and there is another line before it,
* for example in a Tool with line breaks enabled. Leave native behaviour to remove the line break
*/
if (hasContentNextToLineInContentEditable(currentBlock.currentInput, 'left')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kicks in for any Tool whose input is a block inside a contenteditable, not only Tools with line breaks. A Tool with several block "fields" under one contenteditable would now get them merged natively instead of moving the caret between them. Could we gate it with currentBlock.tool.isLineBreaksEnabled? That matches #2854 exactly. Also, isVisible should skip contenteditable="false" siblings: Chrome deletes a whole non-editable node on native Backspace.

Comment thread package.json Outdated
{
"name": "@editorjs/editorjs",
"version": "2.32.0-rc.3",
"version": "2.32.0-rc.4",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the version bump — we bump it on release, and next is already at 2.32.0-rc.5, which is what conflicts here. After rebasing, keep the CHANGELOG line at the end of the 2.32.0 section.

.should('have.text', 'The second bloc'); // last char is removed
});

describe('in a Tool with line breaks enabled', function () {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add cases for a bare first text line (abc<div>def</div> — that's what Chrome actually produces), a formatted line (<div><b>de</b>f</div>), whitespace text nodes between line divs, and a Tool without enableLineBreaks that keeps the old navigation? The ToolWithLineBreaks mock is duplicated in both specs — worth moving to a shared fixture.

Comment thread src/components/dom.ts Outdated
* @param direction - 'left' to look for content before the input, 'right' to look for content after it
* @returns true if there is visible content in the same contenteditable in the passed direction
*/
export function hasContentNextToLineInContentEditable(input: HTMLElement | undefined, direction: 'left' | 'right'): boolean {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is a bit confusing: "next" reads as the following line only (the helper also checks the previous one), and "content" doesn't say what exactly is being looked for. Maybe hasNonEmptySibling(input, direction)?

isVisible needs a JSDoc too: it's not obvious why a <br> or an empty block element counts as visible.

Also, did you consider reusing checkContenteditableSliceForEmptiness from utils/caret.ts instead of the two loops? It already checks everything to the left/right inside a contenteditable via a Range, so there's no need to walk siblings level by level. Something like:

static hasNonEmptySibling(input: HTMLElement, direction: 'left' | 'right'): boolean {
  const root = input.parentElement?.closest('[contenteditable="true"]');

  if (!root) {
    return false;
  }

  const offset = direction === 'left' ? 0 : input.childNodes.length;

  return !checkContenteditableSliceForEmptiness(root, input, offset, direction);
}

The catch is that it only looks at text, so an empty line (<div><br></div>) would count as empty. It'd need a small extension, e.g. an option to treat <br> and block elements in the slice as non-empty — then isVisible isn't needed at all. Either way, it'd be nice to keep one way of doing this. And the helper might fit better as a static Dom method, next to findAllInputs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, we don't even need a separate helper — just extend checkContenteditableSliceForEmptiness and call it directly in blockEvents.ts, otherwise we'd have two functions answering the same question.

…ols with line breaks

In a Tool with enableLineBreaks, browsers wrap each line created by
Enter in a <div>. Dom.findAllInputs() lists those divs as separate
inputs, so Backspace at the start of a line was treated as "caret at the
start of a non-first input" and moved the caret to the previous line
instead of removing the line break. Delete had the same problem at the
end of a line.

Leave the native behaviour when there is other content before (or
after) the line in the same contenteditable. The first line and the
last line keep the existing Block behaviour.
…bleLineBreaks

Apply the native line break removal only when the Tool enables line
breaks. Tools without it keep treating each line as a separate input,
so Backspace and Delete still move the Caret between lines there.

Check the content before (or after) the line with
checkContenteditableSliceForEmptiness() instead of a separate DOM
helper. With countLineBreaks it also counts <br> and non-empty block
elements, and ignores contenteditable="false" nodes.

Cover a bare first text line, formatted lines and whitespace text
nodes between lines, and check that a Tool without line breaks still
handles the key itself.
@b84330808
b84330808 force-pushed the fix-backspace-line-breaks branch from 48cf9f4 to 1b665f5 Compare December 6, 2026 01:45
@b84330808

Copy link
Copy Markdown
Author

Thanks for the detailed review! Reworked it:

  • Gated on currentBlock.tool.isLineBreaksEnabled, so Tools without line breaks keep moving the caret between lines.
  • Dropped the separate helper and extended checkContenteditableSliceForEmptiness with a countLineBreaks option instead: it treats a <br> and non-empty block elements in the slice as content and skips contenteditable="false" nodes. blockEvents.ts calls it directly.
  • Dropped the version bump, rebased on next, and moved the CHANGELOG line to the end of the 2.32.0 section.
  • Added the cases you listed (bare first text line, formatted line, whitespace text nodes between lines, Tool without enableLineBreaks) and moved ToolWithLineBreaks to a shared fixture.

One note on the "without enableLineBreaks" Backspace case: it checks that the Editor handles the key itself (the event is prevented and both lines stay), since the caret move can't be observed reliably with synthetic key events in Cypress. The Delete case checks the caret lands in the next line.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in div content editable backspace doesnt delete blank lines

2 participants