Editor selection is lost when moving focus to editor from input element
When the user moves the keyboard focus from an input element or a textarea element to an editor, the editor selection is reset. (The selection is collapsed and the caret is placed before the first character.)
Steps to reproduce:
- Add an input element to the development sample of a build (sample/index.html). The input element may be added directly before the editor's div element.
- Load the page.
- Make a selection in the editor.
- Press Shift-Tab, focusing the input element.
- Press Tab, focusing the editor element.
Observed result: the original selection is lost. Expected result: the original selection should be retained.
Tested software: v1.0.0-alpha.1
Thanks for the detailed bug report. @Mgsy, could you confirm it?
Steps to reproduce this bug using only Chrome DevTools and https://ckeditor5.github.io:
- Load https://ckeditor5.github.io into Chrome (tested using Chrome 61).
- Open DevTools.
- Locate the
element in the Elements tab. - Edit its HTML markup.
- Add
<input>directly before<div id="editor">...</div>. - Click the input element.
- Press Tab, focusing the editor.
- Press the right arrow key, moving the caret one step to the right.
- Press Shift-Tab, focusing the input element.
- Press Tab, focusing the editor.
- Observe that the position of the caret has been reset.
@davidpolberger, thank you for the detailed steps to reproduce. I've checked it and indeed, the selection had been lost after focusing the input element. I'm providing the screencast.

I believe that selection restoring worked fine for a long time. Pity that we broke this.
I wonder... why does it work fine with our UI (e.g. the link balloon)? I guess we're handling moving the focus back to the editable and in some way this prevents this issue.
We call editor.editing.view.focus() directly in this situation. https://github.com/ckeditor/ckeditor5-link/blob/3b4dba8c1c58e276b0f04f6e33e450cbfffdab40/src/link.js#L296
cc @szymonkups – could you check it?
@davidpolberger – how serious is this issue for you? Are you building an external UI and is it a must that after Tab the selection is maintained? Or did you encounter this when tabbing through a form (in which case I guess the priority would be lower)?
@Mgsy, could you check whether the fix that @scofalik described in https://github.com/ckeditor/ckeditor5-engine/issues/1155 helps for this case too?
@Mgsy, could you check whether the fix that @scofalik described in ckeditor/ckeditor5-engine#1155 helps for this case too?
Unfortunately, this one doesn't solve this issue.
@Reinmar, we're using a custom headless build for the purposes of building a custom user interface. (The headless build and editor will be pushed to GitHub before we make our product incorporating CKEditor 5 public.) We encountered this problem when implementing our user interface for adding and editing links. We want that feature to be keyboard-accessible, so pressing Ctrl+K when the selection is not collapsed turns the selection into a link (with an empty href attribute) and the keyboard focus is then moved to an input element enabling the href attribute to be edited. The link command is executed every time the user changes the href value. When the user is done, he or she is expected to press Enter to be moved back to the editor. That's when we discovered that the selection is reset. For an editor containing multiple pages of text, this significantly degrades the user experience.
To focus the editor, we simply used the DOM focus() method. Switching to editor.editing.view.focus(), as @oleq suggested, makes this work nicely.
Our user interface allows the user to create an arbitrary number of editors on the same page. The user can move up and down by pressing Ctrl+Up and Ctrl+Down, in which case we programmatically move the keyboard focus. Fixing the selection problem in that context would be easy, but it would be more involved to fix when the user is using Tab and Shift+Tab (we'd have to trap these keyboard events and move the focus programmatically).
We're actually quite satisfied now that we've managed to fix this for our link editing experience. Fixing the selection problem when moving between editors through keyboard shortcuts would also be nice, but is a low-priority issue for us.
There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.
We've closed your issue due to inactivity over the last year. We understand that the issue may still be relevant. If so, feel free to open a new one (and link this issue to it).