Bug 278553
| Summary: | iframe's document not focusing when .focus() is called | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Marcos Caceres <marcosc> |
| Component: | WebCore Misc. | Assignee: | Marcos Caceres <marcosc> |
| Status: | RESOLVED WORKSFORME | ||
| Severity: | Normal | CC: | ahmad.saleem792, webkit-bug-importer |
| Priority: | P2 | Keywords: | BrowserCompat, InRadar |
| Version: | Other | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
| URL: | https://jsfiddle.net/3249g6pw/1/ | ||
| Bug Depends on: | |||
| Bug Blocks: | 268516 | ||
Marcos Caceres
There appears to be an issue with iframes not focusing when .focus() is called on them.
Test case:
```
window.onload = async function () {
const iframes = document.querySelectorAll("iframe");
for (let iframe of iframes) {
await new Promise(resolve => {
iframe.onload = resolve;
iframe.src = "about:blank";
});
iframe.focus();
console.assert(iframe.contentDocument.hasFocus(), "The iframe is not focused.");
}
};
```
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Radar WebKit Bug Importer
<rdar://problem/134992720>
Marcos Caceres
Test case https://jsfiddle.net/otg682h0/
Ahmad Saleem
(In reply to Marcos Caceres from comment #2)
> Test case https://jsfiddle.net/otg682h0/
*** Safari 26.4 Beta ***
"iframe1 is not focused after calling focus()"
"iframe2 is not focused after calling focus()"
"iframe1 is not focused after swapping focus back"
"Iframe focus test completed."
*** Chrome Canary 147 (147.0.7699.0 (Official Build) canary (arm64) ) ***
"Iframe focus test completed."
*** Firefox Nightly 149 (20260220092058) ***
"Iframe focus test completed."
Marcos Caceres
Pull request: https://github.com/WebKit/WebKit/pull/69558
Marcos Caceres
After som investigation, what the DC API was doing was actually wrong... so changed the title to fix that instead.
Marcos Caceres
*** This bug has been marked as a duplicate of bug 256299 ***
Marcos Caceres
Reopening and restoring the original title. Bug 256299 covered the Digital Credentials get() user-attention gating; the underlying issue reported here — iframe.focus() not focusing the iframe's document, where Chrome and Firefox do (comment 3) — is a separate focus bug that remains unfixed. Tracking it here.
Marcos Caceres
Testing the reported case shows the failing assertions track whether the top-level document has focus (document.hasFocus()) when focus() is called, and Safari's behavior matches what HTML specifies.
HTML's "has focus steps" (https://html.spec.whatwg.org/multipage/interaction.html#has-focus-steps) step 1: "If target's node navigable's top-level traversable does not have system focus, then return false." So document.hasFocus(), and hence iframe.contentDocument.hasFocus(), returns false whenever the containing window lacks system focus, independent of any focus() call.
Test: create two iframes, load both about:blank, focus iframe1, iframe2, then iframe1 again, checking contentDocument.hasFocus() after each.
- With the window focused, iframe.focus() focuses the iframe's document and every check returns true (Safari, Chrome, and Firefox alike).
- With the window not focused, as at window.onload before the window becomes key (where the original test runs), the checks return false. That is the "has focus steps" step 1 result, not a failure of iframe.focus().
Safari is conformant: iframe.focus() focuses the iframe's document, and hasFocus() correctly returns false when the top-level traversable lacks system focus. Closing as WORKSFORME.