Skip to content

Fix hasFocus() for editors nested in contenteditable elements#189

Open
excursus wants to merge 1 commit intoProseMirror:masterfrom
excursus:fix-nested-contenteditable
Open

Fix hasFocus() for editors nested in contenteditable elements#189
excursus wants to merge 1 commit intoProseMirror:masterfrom
excursus:fix-nested-contenteditable

Conversation

@excursus
Copy link

Problem

When a ProseMirror editor is nested inside another element with contenteditable="true", view.hasFocus() incorrectly returns false even when the editor has focus. This causes various functionality to break, including selection handling.

Cause

When the editor is inside a contenteditable ancestor, the browser sets document.activeElement to the outermost contenteditable element rather than the editor's DOM element. The existing hasFocus() implementation compares activeElement directly to this.dom, which fails in this nested scenario.

Solution

Added an effectiveActiveElement() helper method that accounts for nested contenteditable elements. When activeElement is an ancestor that contains this.dom, the helper returns this.dom as the effective active element. The hasFocus() method now uses this helper instead of directly accessing this.root.activeElement.

Testing

  • Added a new test case "reports focus correctly when nested in contenteditable" that verifies hasFocus() returns true when the editor is mounted inside a contenteditable wrapper
  • Wrapped the entire test harness in a contenteditable element to validate behavior; several tests were failing before this fix, and now all pass.

@marijnh
Copy link
Member

marijnh commented Jan 26, 2026

the browser sets document.activeElement to the outermost contenteditable element

Which browser does this?

@excursus excursus closed this Jan 26, 2026
@excursus
Copy link
Author

I tested this in Chrome on OSX.

@excursus excursus reopened this Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants