-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autofocus spec should probably allow browsers to delay running the focusing steps, maybe? #3551
Comments
I agree the spec currently doesn't allow this behavior. I guess it's theoretically an observable interop problem to allow arbitrary UA-determined delays here. Is that worth it? |
It doesn't need to be arbitrary, insofar as the concept of "rendering start" is specced and waits for sheets. |
Fair point. I assume such a point is not specced today :-/. |
@tkent-google @rniwa @cdumez do you know if Chrome or Safari do anything like this? |
It seems WebKit also has such delay. |
Could this be done as part of the "Rendering" section of the event loop? If a browser waits until it has various things before it runs that for the first time, it might be adequate? Would it queue a task at that point to focus? |
If we do it as part of update the rendering, I'd say we should do it as part of the list of things we do for each doc, so e.g. right before rAF callbacks perhaps. Implementers, could you describe more precisely how you'd implement this kind of "holding"? Is update the rendering even reasonable---I kind of think that might happen before pending stylesheets have loaded? An alternate approach would be to do it around the end of parsing, after
I'm unclear whether "stylesheets that are blocking scripts" are the same as the ones the OP is talking about. |
I think change would be:
@bzbarsky Does this work well in Firefox? WebKit's current behavior is something like doing the autofocus steps just after rendering completion without checking pending stylesheets. |
Making it possible to delay the autofocus makes sense to me. I'd have to think through as to what would be the ideal timing though. |
Correction: We should use only "autofocus element queue" of top-level browsing context's active document. |
ping @bzbarsky |
By that, you mean autofocus would only work in the top-level browsing context? Is that actually true in the existing browsers? |
No, I wanted to mean that elements with autofocus attribute in all sub-frames should be queued to the top-level queue in order to avoid race between frames. |
@tkent-google : That seems to suggest that autofocus is really page or chrome level feature so this queue perhaps should only exist in the top-level browsing context? Somewhat relatedly, when you It seems that we'd need a lot of tests involving iframes, new auxiliary browsing contexts, and iframes opening auxiliary browsing contexts all interleaving autofocus element queue in one another, and observe what the browsers are doing today, and what we really want to spec. |
@rniwa Yes. I don't think Iframes are problematic. Suppose that a page has multiple iframes, and some of them contain elements with autofocus. I confirmed that the final focused element depended on iframe's load timing with Chrome, Firefox, and Safari. I don't have a good idea to make this deterministic. My current proposal doesn't affect it, and it's out of scope of this issue. |
While I don't think we necessarily need to make it deterministic, it's wholly within the scope of this issue to discuss how it affects them. While we worked on iPadOS, we found dozens of productivity websites that rely on focusing behavior across iframes. In particular, when |
@rniwa Hmm, probably I don't understand what's your concern precisely. Anyway, inserting |
@tkent-google : Consider the following markup: <!DOCTYPE html>
<html>
<body>
<script>
const frame = document.createElement('iframe');
document.body.appendChild(frame);
frame.contentDocument.body.innerHTML = `<input autofocus>`;
</script>
<input autofocus>
</body>
</html> In Firefox, the second input element is always focused. In Chrome, the first input element is always focused. In Safari, it changes each load LOL. There is no reason this needs to stay non-deterministic. |
@rniwa, thank you for the example. With both of the current specification and my proposed change, the input-in-iframe should be focused because it is connected earlier than the input-in-top-document. However, this example is not interoperable due to another reason. Chrome and Safari loads If we change the script to: const frame = document.createElement('iframe');
frame.onload = () => { frame.contentDocument.body.innerHTML = `<input autofocus>`; };
document.body.appendChild(frame); Firefox focuses on the input-in-top-document in many cases, and I guess it's possible to focus on the input-in-iframe sometimes because iframe's load event can be dispatched before the input-in-top-document is connected. |
Hm... so it looks like Safari usually picks the last element with Here's another interesting example. <!DOCTYPE html>
<html>
<body>
<script>
const frame1 = document.createElement('iframe');
document.body.appendChild(frame1);
const frame2 = document.createElement('iframe');
frame2.onload = () => {
frame1.contentDocument.body.innerHTML = `<input autofocus>`;
frame2.contentDocument.body.innerHTML = `<input autofocus>`;
}
document.body.appendChild(frame2);
</script>
<input autofocus>
</body>
</html> So the autofocus queue somehow needs to close after each document had finished parsing / loading, and then remove itself from the top-level browsing context. There appears to be a lot of edge cases around here that worth careful examinations. |
- Run focusing steps after an animation frame - Don't autofocus if the top-level document has focused area - Don't autofocus if one of ancestor document has :target element. This fixes whatwg#3551 and whatwg#4645
I posted a specification PR to run focusing steps after an animation frame: #4763 |
- Run focusing steps after an animation frame - Don't autofocus if the top-level document has focused area - Don't autofocus if one of ancestor document has :target element. This fixes whatwg#3551 and whatwg#4645
Gecko recently made a change to not actually do autofocusing bits until after pending stylesheets have loaded, to prevent flashes of unstyled content.
It's not clear to me that the current spec language (which required queueing a task as soon as the autofocus element is inserted into the DOM) allows this behavior. Should it?
The text was updated successfully, but these errors were encountered: