rangy icon indicating copy to clipboard operation
rangy copied to clipboard

In Chrome, <input autofocus> doesn't work when Rangy is included

Open timdown opened this issue 11 years ago • 14 comments

From [email protected] on January 21, 2013 14:16:45

What steps will reproduce the problem? 1. Go to this URL:

data:text/html;,

  1. Watch the input box What is the expected output? What do you see instead? Input has focus; typing directly should be possible. When Rangy is included, the input does have an orange outline as if it is focused, but typing is not actually possible. What version of the product are you using? On what operating system? Windows Vista, Google Chrome 26, Rangy dev version

Original issue: http://code.google.com/p/rangy/issues/detail?id=146

timdown avatar Mar 22 '14 23:03 timdown

From [email protected] on January 21, 2013 17:01:13

How bizarre. However, I can't replicate it on Chrome 24 on Windows 7.

timdown avatar Mar 22 '14 23:03 timdown

From [email protected] on January 22, 2013 06:15:18

The issue turns out to be a removeAllRanges() call in one of the tests in the WrappedSelection module. Instead of including Rangy, just calling getSelection().removeAllRanges() on page load also causes the autofocus issue. I guess this is a Chrome bug instead.

timdown avatar Mar 22 '14 23:03 timdown

From [email protected] on February 23, 2013 15:34:11

Status: NotABug

timdown avatar Mar 22 '14 23:03 timdown

From martin.thurau on October 22, 2013 08:00:27

Sorry for digging this up :)

Im using rangy in the content script of a Chrome extension and I'm getting complaints from users, that the extension "steals" their focus on google.com (of course this also happens on other pages but so far it has only be noticed on Google). I have tracked this problem down to exactly this issue. I understand this is a Chrome bug but until it is fixed there I want to work around this by reselecting the input field on certain pages.

I have tried calling focus() on the appropriate element but this has no effect. Could it be that rangy somehow "destroys" the ability to focus something from Javascript? And if so, is there a way around this?

timdown avatar Mar 22 '14 23:03 timdown

From [email protected] on October 22, 2013 08:22:03

I believe input.blur() and then input.focus() can fix it, but I'm not sure if it's reliable. Putting that code in the load event of window seems to solve the issue:

window.addEventListener("load", function() { var elem = document.querySelector(":focus"); elem.blur(); elem.focus(); });

At least, it works on this page for me:

data:text/html;,

Use at your own risk.

timdown avatar Mar 22 '14 23:03 timdown

From [email protected] on October 22, 2013 16:32:51

Rangy does do some selection-related feature tests every time it starts up and I can see that it's possible it could affect autofocus. Perhaps a rethink of the feature tests is in order.

Status: Accepted

timdown avatar Mar 22 '14 23:03 timdown

From martin.thurau on October 23, 2013 10:24:36

The workaround in #5 works fine for me. Thanks :-)

timdown avatar Mar 22 '14 23:03 timdown

I'm seeing this issue too - https://tracker.moodle.org/browse/MDL-53625

I've tracked it down to https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js#L98 causing focus to change.

andrewnicols avatar Mar 29 '16 03:03 andrewnicols

It's specifically caused by https://github.com/timdown/rangy/blob/1e55169d2e4d1d9458c2a87119addf47a8265276/src/core/wrappedselection.js#L496

andrewnicols avatar Mar 29 '16 03:03 andrewnicols

https://developer.mozilla.org/en-US/docs/Web/API/Selection#Selection_and_input_focus

Safari and Chrome (unlike Firefox) historically focus the element containing selection when modifying the selection programmatically, but this might change in the future (see W3C bug 14383 and WebKit bug 38696).

https://bugs.webkit.org/show_bug.cgi?id=38696 https://www.w3.org/Bugs/Public/show_bug.cgi?id=14383

andrewnicols avatar Mar 29 '16 03:03 andrewnicols

Hi @timdown,

Is there anything that we can do to help with this issue. It's driving me nuts. Every time an editor is rendered we lose focus.

andrewnicols avatar Apr 20 '16 00:04 andrewnicols

Pinging @timdown again,

Is there any way we can help here? I'm not sure what the 'best' alternative is here but if you can provide guidance we can probably help with coding it, etc.

andrewnicols avatar May 10 '16 02:05 andrewnicols

Pinging @timdown again. Any suggestion on direction would be awesome!

andrewnicols avatar Aug 12 '16 06:08 andrewnicols

Hi @timdown,

Just checking in again, is there anything we can do to help here? Is this project dead?

andrewnicols avatar Jan 10 '18 03:01 andrewnicols