New issue
Advanced search Search tips

Issue 755110 link

Starred by 5 users

Issue metadata

Status: WontFix
Owner: ----
Closed: Aug 2017
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Windows
Pri: 2
Type: Compat



Sign in to add a comment

setSelectionRange no longer setting focus on a field after call from Javascript

Reported by rjc.pers...@gmail.com, Aug 14 2017

Issue description

UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36

Example URL:
www.wallstreethorizon.com

Steps to reproduce the problem:
We have a page that builds an input field using Javascript. The user clicks buttons and the input field builds. At the end of each interaction we call setSelectionRange to position the cursor at a particular place in the input string. Thus the cursor is positioned for direct user input or ready to click another button to continue building. 

Prior to 8/8/2017, this worked fine in CHROME. As of 8/8/2017, the cursor will no longer positioned in the input (the field looses focus) and there seems to be no programmatic way to re-obtain focus. 

We have tested this in Firefox and Edge and it works fine. It just does not work in CHROME.

What is the expected behavior?
After the JS call, it is anticipated that the cursor will be positioned at the end of the selection and the input field will be in focus. 

What went wrong?
The cursor disappears and there is no focus on the page.

Does it occur on multiple sites: No

Is it a problem with a plugin? No 

Did this work before? Yes prior to 8/8/2017

Does this work in other browsers? Yes

Chrome version: 60.0.3112.90  Channel: stable
OS Version: 10.0
Flash Version: 

This URL is behind a login, If needed we can expose a page that exhibits this behavior on our public site.
 

Comment 1 by change...@gmail.com, Aug 15 2017

We got a similar problem here at www.webmail.hinet.net.
We have an on-browser keyboard for user to type in their account and password.
After Chrome version 60, the cursor will stay at the begin of the input box.(the value of selectionStart and selectionEnd are wrong)

We've tested that it worked fine in the version 59 and before. And it's also good for firefox and IE now.

We are using a modified module(https://github.com/localhost/jquery-fieldselection), and we can solve the problem by adding a focus to the input box inside the module. So we don't know whether it's a bug or not.

We have tried adding a focus() after re positioning the cursor, but that did not work. I think this is a bug.
Components: Blink>Editing>Selection
Sorry, not clear on what your comment (above) means?

Comment 5 by yosin@chromium.org, Aug 23 2017

Status: WontFix (was: Unconfirmed)
HTMLInputElement#setSelectionRange() and HTMLTextAreaElement.setSelectionRange()
don't set focus regarding to the spec[1].

Here is an implementation:

bool TextControlElement::SetSelectionRange(
    unsigned start,
    unsigned end,
    TextFieldSelectionDirection direction) {
...
  frame->Selection().SetSelection(
      SelectionInDOMTree::Builder()
          .Collapse(direction == kSelectionHasBackwardDirection
                        ? end_position
                        : start_position)
          .Extend(direction == kSelectionHasBackwardDirection ? start_position
                                                              : end_position)
          .SetIsDirectional(direction != kSelectionHasNoDirection)
          .Build(),
      SetSelectionOptions::Builder()
          .SetShouldCloseTyping(true)
          .SetShouldClearTypingStyle(true)
// We don't set focus for |setSelectionRange()
          .SetDoNotSetFocus(true)
          .Build());
  return did_change;
}

[1] https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea%2Finput-setselectionrange
OK, here's a sample of what we call - it's very similar:

	if ((varname == "@+ORDER BY ") && (orderPos == -1))
		{
		//alert ("insert");
		sql_string.value = sql_string_value + "\n    ORDER BY ";
		sql_string_value = sql_string.value
		varname = "";
		cursorPos =  sql_string_value.length +1;
		sql_string.setSelectionRange( cursorPos ,cursorPos);
		return;
		}			
	if ((varname == "@+ORDER BY ") && (orderPos != -1))	 return;

	
	if ((varname == "@+DESC ") && (descPos == -1))
		{
		//alert ("insert");
		sql_string.value = sql_string_value + " DESC ";
		sql_string_value = sql_string.value
		varname = "";
		cursorPos =  sql_string_value.length +1;
		sql_string.setSelectionRange( cursorPos ,cursorPos);
		return;
		}	
All we want to do it position the cursor at cursorPos.
The problem is that the input field has now lost focus. There is no blinking cursor anywhere on the screen. You need to click on the field again to re-establish focus.

Just to be clear - we never set focus in our code, it jsut seems that the focus is not being lost after the call to the code.

OK, here's a simple version of the problem:

http://test.wallstreethorizon.com/test.asp

Click on THIS, then IS, then A, then TEST
Everything builds nicely.

Now, position your cursor after IS and click TEST

You have
 THIS  IS TEST   A  TEST 	
All good, 
Now, click TEST again

This is what you should get (and what you get in Firefox or IE):
 THIS  IS TEST  TEST   A  TEST 

Here's what Chrome produced:

 TEST  THIS  IS TEST   A  TEST 	

Note the TEST was inserted at the start instead of the current position.

Here's what's happening:

var cursorPos = sql_string.selectionStart

is returning 0 instead of the current selected position from the last call.




Note that in this test, I can "fix" the failure of selectionStart by remembering the last cursor position with a global variable, but how do I position the cursor where I want it?
OK, looks like if I use JQUERY, I can get the focus back
	$("#sql_string").focus();

The problem isn't focus, but that as of 60 setSelectionStart() doesn't actually set the selection at all if the field doesn't have focus. That can't be right. It's contrary to every other browser including Chrome 59 and below.
I got it to work by forcing the focus (see below), but I agree with jdavidre, this is a change in the behavior and  is, indeed, a bug, selectionStart no longer returns the position of the cursor either.


<script src="./js/jquery-3.0.0.min.js"></script>
<script>
var global_cursorPos = 0;	// Establish global contxext
function build_query(varname)
	{
	
	var sql_string = document.getElementById("sql_string");
	var cursorPos = sql_string.selectionStart;
	if (cursorPos == 0) cursorPos = global_cursorPos; // selectionStart no longer works as of chromium 60, still good in FF, IE, EDGE
	var sql_string_value = sql_string.value;	
	// concatenate the word into the string
	sql_string.value = sql_string_value.substring(0,cursorPos) +
		varname +
		sql_string_value.substring(cursorPos, sql_string_value.length);
	sql_string_value = sql_string.value
	cursorPos = cursorPos + varname.length;
	global_cursorPos = cursorPos; // Remember the position for next time because Chromium 60 forgets
	sql_string.value = sql_string_value;
	sql_string.setSelectionRange( cursorPos,cursorPos);
	
        $("#sql_string").focus(); // Use JQuery to re-establish focus on the field (make the cursor appear)
	
	
	//sql_string.focus;
	}
</script>
TEST2 - Global Positon Memory<br>
The string we are bulding is below:<hr>
<textarea name="sql_string" id="sql_string" rows="5" cols="100">
	
</textarea>
<hr>
<a href="#" style="color:black;font-size:16px;" onclick="build_query(' THIS ');return false;"><b>THIS</b></a><br>
<a href="#" style="color:black;font-size:16px;" onclick="build_query(' IS ');return false;"><b>IS</b></a><br>
<a href="#" style="color:black;font-size:16px;" onclick="build_query(' A ');return false;"><b>A</b></a><br>
<a href="#" style="color:black;font-size:16px;" onclick="build_query(' TEST ');return false;"><b>TEST</b></a><br>

Sign in to add a comment