setSelectionRange() doesn't move cursor (and selection) to the visible input area
Reported by
nikolay....@gmail.com,
Jan 26 2018
|
||||||
Issue descriptionUserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 Steps to reproduce the problem: 1. Open the following sample https://jsfiddle.net/Lipata/c02ux411/3/ 2. Notice that the cursor is not visible. It is moved to 9th position, but the text is not moved and the cursor is hidden. 3. The same problem is valid when setSelectionRange() is called inside a event handler (e.g. keypress). At the first point I though the problem was caused because the preventDefault() is called, but I guess the root problem is the one with the setSelectionRange() method. What is the expected behavior? The text is moved so that the cursor and current selections are visible What went wrong? The selection/cursor is not visible, when the text width is more than the input width. Did this work before? N/A Does this work in other browsers? N/A Chrome version: 63.0.3239.132 Channel: n/a OS Version: 10.0 Flash Version:
,
Jan 30 2018
Able to reproduce the issue on reported chrome version 63.0.3239.132 and on the latest canary 66.0.3334.0 using Windows 10, Ubuntu 17.10 and Mac 10.13.1. As the issue is seen from M60(60.0.3072.0) considering it as non-regression and marking it as Untriaged. Thanks!
,
Jan 30 2018
,
Jan 30 2018
Thank you, guys! Regards
,
Feb 6 2018
Not JS related.
,
Feb 8 2018
,
Feb 14 2018
Default event handler for "keypress" moves selection but the sample script doesn't. Please use "input" event handler to capitalize input value, e.g. https://jsfiddle.net/c02ux411/11/
,
Feb 14 2018
Hi, guys. Thank you for looking at this. I understand that "input" event is alternative for "keypress" handler, but can you guys give more information why is that. As far as I remember in older versions of Chrome this was working inside the "keypress" event handler. Even though the problem of setting the selection inside the "input" event handler is a solution, still there is a problem of using setSelection(), outside the context of any event handler, like here: https://jsfiddle.net/c02ux411/14/ Thank you in advance for the answer! Regards, Nikolay Alipiev
,
Feb 15 2018
# "input" is our friends
TL;DR: Use "input" for detecting change by user, use "keypress" only for game
We should handle "input" event to detect value change by user interaction.
Keyboard is one of way to enter a character, move/extend selection, delete
characters. IME, which uses keyboard, hand writing, voice, camera, etc, is
also change value by user interaction.
On smart phones, IME is main source of user input and all user inputs come
from IME by "compositionstart", "compositionupdate" and "compositionend".
For IME case, "keypress" event doesn't mean what user wants to input, e.g.
alphabet + accessnt(é), CJK (ka=か), etc, thus converting keyboard character
to upper case, we should use String.prototype.toLocaleUpperCase(), isn't
enough or does wrong.
# HTMLInput#setSelectionRange()
Please try to call Element#focus() after HTMLInput#setSelectionRange()[1].
In this way, INPUT element is scrolled to show caret[2],
by FrameSelection::RevealSelection() via FrameSelection::SetSelectio().
When you call setSelectionRange() after focus()[4], Blink doesn't call RevealSelection()[3],
because setSelectionRange() calls SetSelection() with SetSelectionBy::kSystem.
if (set_selection_by == SetSelectionBy::kUser) {
....
RevealSelection(alignment, kRevealExtent);
}
Note: All browsers behave different:
SetSelection before Focus[1]:
- Chrome: reveal
- Edge: not reveal
- Firefox: not reveal
SetSelection after Focus[2]:
- Chrome: not reveal
- Edge: reveal
- Firefox: not reveal
Could you ask which behavior is a good for developer as issue in [5]?
[1] https://jsfiddle.net/c02ux411/15/
[2] https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/dom/Element.cpp?sq=package:chromium&l=2970
[3] https://jsfiddle.net/c02ux411/14/
[4] https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/editing/FrameSelection.cpp?gsn=SetSelection&l=289
[5] https://github.com/w3c/editing/issues
,
Feb 22 2018
Thank you very much for the detailed answer - I really appreciate it, especially the part with the code! I'm aware of the IME specifics so I can agree on the usage of input event. As far as about the setSelection() usage and calling the focus, after that, I still have some doubts. In my case when the setSelection() method is called, the input is already on focus. If I execute it for a second time after selection takes place, then still the input is not scrolled to the caret position. Here (https://jsfiddle.net/Lipata/x966r76n/) is the sample. I've already logged an issue in the W3C repo (https://github.com/w3c/editing/issues/173). Best regards, Nikolay Alipiev
,
May 22 2018
Hi, guys. As you suggested I have logged a bug (first to w3c (https://github.com/w3c/editing/issues/173) and then whatwg (https://github.com/whatwg/html/issues/3694#event-1632501344)). You can see the answer from @domenic in the whatwg/html repo, and tell me what you think. Do you think that this is something you need to implement, or there are reasons that it cannot be implemented. Thank you in advance! Nikolay Alipiev |
||||||
►
Sign in to add a comment |
||||||
Comment 1 by krajshree@chromium.org
, Jan 29 2018