Skip to content

fix(selection): focus contenteditable when setting cursor - #3057

Merged
neSpecc merged 7 commits into
codex-team:nextfrom
Anurag-S1ngh:fix/1506-focus-contenteditable
Nov 25, 2026
Merged

neSpecc merged 7 commits into
codex-team:nextfrom
Anurag-S1ngh:fix/1506-focus-contenteditable

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Summary

Setting a selection range does not focus the editing host in every context. When the editor is embedded in an iframe, autofocus and programmatic caret moves leave focus outside the editor, so typing never reaches it.

Changes

  • In Selection.setCursor(), focus the closest [contenteditable="true"] element before applying the range. This matches the method's documented behavior of setting focus for contenteditable inputs, which it previously did only for native inputs.

Testing

  • Editor inside an iframe: before the change document.activeElement was BODY and document.hasFocus() was false. After the change the editable element is focused and the iframe becomes the top-level active element.
  • Normal page: autofocus, blur followed by caret.setToBlock(), and toolbar flows behave as before.
  • eslint passes on the changed file.

Closes #1506

Setting a selection range does not focus the editing host in every context, for example when the editor is embedded in an iframe. Autofocus and programmatic caret moves then leave focus outside the editor.

Focus the closest contenteditable element before applying the range.

Fixes codex-team#1506
@neSpecc
neSpecc requested a balanced review from Copilot November 24, 2026 09:10

This comment was marked as resolved.

@neSpecc neSpecc left a comment

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.

Thanks for picking up this old one, and for the clear description! The fix looks right to me, just a few small things before merge:

  • CHANGELOG: please add an entry to the 2.32.0 section of docs/CHANGELOG.md, something like:
    - `Fix` - Focus the contenteditable input when setting the caret, so autofocus works inside iframes
    
  • Test: could you add a Cypress test for this? E.g. check that document.activeElement is the block's contenteditable after autofocus: true, and after editor.caret.setToBlock() when focus was outside the editor. Cypress runs the app in an iframe itself, so it's pretty close to the original bug.
  • nit (outside the diff): the JSDoc/signature of setCursor says element: HTMLElement, but it actually receives text nodes too (from getDeepestNode), which your new code already handles. Might be worth changing the type to Node while you're here.

Comment thread src/components/selection.ts Resolved
@Anurag-S1ngh

Copy link
Copy Markdown
Contributor Author

Added the changelog entry and Cypress coverage for autofocus and caret.setToBlock() when focus is outside the editor. Also changed the focus call to focus({ preventScroll: true }) and updated the setCursor parameter type to Node. The full Cypress suite passes. Ready for another look.

Comment thread test/cypress/tests/api/caret.cy.ts Resolved
@neSpecc
neSpecc enabled auto-merge (squash) November 25, 2026 07:25
@neSpecc
neSpecc merged commit 07fe361 into codex-team:next Nov 25, 2026
4 checks passed
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.

[Bug] Unable to autofocus

3 participants