Truly independent web browser
LibWeb: Exit fullscreen on Escape before the page sees the keydown
sideshowbarker7b3afd934b0d
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex 0fb2bcd54b3..fc8f25d696c 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -1967,6 +1967,34 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u}}+ // https://html.spec.whatwg.org/multipage/interaction.html#close-requests+ // NB: Stopping autoscroll and dismissing a writing suggestion take Escape ahead of fullscreen, below.+ if (key == UIEvents::KeyCode::Key_Escape && !m_middle_button_scroll_handler) {+ GC::Ref<DOM::Document> document = *m_navigable->active_document();+ auto* controller = document->writing_suggestion_controller_if_exists();+ bool suggestion_is_showing = controller && controller->showing().has_value();++ // AD-HOC: The spec checks only the document that the close request targets. But while an ancestor document's+ // element is fullscreen (one containing this document's iframe, e.g.), this document has no fullscreen+ // element of its own. So check the topmost ancestor document in this process instead — otherwise a+ // page that cancels Escape in a focused iframe keeps the user in fullscreen.+ GC::Ref<DOM::Document> topmost_document = document;+ while (auto container_document = topmost_document->container_document())+ topmost_document = *container_document;++ // 1. If document's fullscreen element is not null, then:+ if (!suggestion_is_showing && topmost_document->fullscreen()) {+ // 1. Fully exit fullscreen given document's node navigable's top-level traversable's active document.+ // NB: That document runs the steps in the process holding the top-level traversable, which the chrome+ // hands the request to.+ m_navigable->page().client().page_did_request_fully_exit_fullscreen();+ // 2. Return.+ // NOTE: This does not fire any relevant event, such as keydown; it only causes fullscreenchange to be+ // eventually fired.+ return EventResult::Handled;+ }+ }+GC::Root<DOM::Document> native_command_document;if (native_editing_binding_resolved)native_command_document = m_navigable->active_document();@@ -2048,16 +2076,6 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, ureturn EventResult::Handled;}- // 1. If document's fullscreen element is not null, then:- if (document->fullscreen()) {- // 1. Fully exit fullscreen given document's node navigable's top-level traversable's active document.- // NB: That document runs the steps in the process holding the top-level traversable, which the chrome- // hands the request to.- m_navigable->page().client().page_did_request_fully_exit_fullscreen();- // 2. Return.- return EventResult::Handled;- }-// 7. Let closedSomething be the result of processing close watchers on document's relevant global object.auto closed_something = document->window()->close_watcher_manager()->process_close_watchers();diff --git a/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.txt b/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.txtnew file mode 100644index 00000000000..5349c0be34d--- /dev/null+++ b/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.txt@@ -0,0 +1,4 @@+fullscreen element before Escape: player+iframe's own fullscreen element: null+fullscreen element after Escape: null+keydowns the iframe saw for that Escape: nonediff --git a/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown.txt b/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown.txtnew file mode 100644index 00000000000..8eb986179f9--- /dev/null+++ b/Tests/LibWeb/Text/expected/escape-exits-fullscreen-despite-canceled-keydown.txt@@ -0,0 +1,4 @@+fullscreen element before Escape: target+fullscreen element after Escape: null+keydowns the page saw for that Escape: none+keydowns after a second Escape: keydown Escapediff --git a/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.html b/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.htmlnew file mode 100644index 00000000000..16a868e8705--- /dev/null+++ b/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown-in-iframe.html@@ -0,0 +1,44 @@+<!DOCTYPE html>+<script src="include.js"></script>+<div id="player">+ <iframe id="frame" srcdoc="<div id=inner tabindex=0>iframe content</div>"></iframe>+</div>+<script>+ asyncTest(async done => {+ const frame = document.getElementById("frame");+ if (!frame.contentDocument?.getElementById("inner"))+ await new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));++ const keys = [];+ // The iframe keeps Escape for itself, while its container in this document is fullscreen.+ frame.contentDocument.addEventListener("keydown", event => {+ keys.push(`keydown ${event.key}`);+ event.preventDefault();+ });++ const player = document.getElementById("player");+ internals.grantTransientActivation();+ await player.requestFullscreen();+ const results = [+ `fullscreen element before Escape: ${document.fullscreenElement?.id}`,+ `iframe's own fullscreen element: ${frame.contentDocument.fullscreenElement}`,+ ];++ const exitedFullscreen = new Promise(resolve => {+ document.addEventListener("fullscreenchange", function listener() {+ if (document.fullscreenElement)+ return;+ document.removeEventListener("fullscreenchange", listener);+ resolve();+ });+ });+ internals.sendKey(frame.contentDocument.getElementById("inner"), "Escape");+ await exitedFullscreen;+ results.push(`fullscreen element after Escape: ${document.fullscreenElement}`);+ results.push(`keydowns the iframe saw for that Escape: ${keys.length ? keys.join(", ") : "none"}`);++ for (const result of results)+ println(result);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown.html b/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown.htmlnew file mode 100644index 00000000000..34b15fc5c0c--- /dev/null+++ b/Tests/LibWeb/Text/input/escape-exits-fullscreen-despite-canceled-keydown.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<script src="include.js"></script>+<div id="target" tabindex="0">fullscreen content</div>+<script>+ asyncTest(async done => {+ const target = document.getElementById("target");+ const keys = [];+ // A page that keeps Escape for itself, as video players do.+ document.addEventListener("keydown", event => {+ keys.push(`keydown ${event.key}`);+ event.preventDefault();+ });++ internals.grantTransientActivation();+ await target.requestFullscreen();+ const results = [`fullscreen element before Escape: ${document.fullscreenElement?.id}`];++ const exitedFullscreen = new Promise(resolve => {+ document.addEventListener("fullscreenchange", function listener() {+ if (document.fullscreenElement)+ return;+ document.removeEventListener("fullscreenchange", listener);+ resolve();+ });+ });+ internals.sendKey(target, "Escape");+ await exitedFullscreen;+ results.push(`fullscreen element after Escape: ${document.fullscreenElement}`);+ results.push(`keydowns the page saw for that Escape: ${keys.length ? keys.join(", ") : "none"}`);++ // Out of fullscreen, Escape reaches the page as usual.+ internals.sendKey(target, "Escape");+ results.push(`keydowns after a second Escape: ${keys.join(", ")}`);++ for (const result of results)+ println(result);+ done();+ });+</script>