Truly independent web browser
LibWeb: Exit fullscreen and Picture-in-Picture in favor of each other
Luke Wilde3bee7752e501
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 90538daa618..34cafd33b7c 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -146,6 +146,7 @@#include <LibWeb/Namespace.h>#include <LibWeb/Page/Page.h>#include <LibWeb/Painting/BoxViews.h>+#include <LibWeb/PictureInPicture/PictureInPictureController.h>#include <LibWeb/Platform/EventLoopPlugin.h>#include <LibWeb/SVG/SVGAElement.h>#include <LibWeb/SVG/SVGElement.h>@@ -6949,6 +6950,19 @@ void Element::set_fullscreen_flag(bool is_fullscreen)return;m_fullscreen_flag = is_fullscreen;CSS::record_element_state_changed(*this, CSS::PseudoClass::Fullscreen, is_fullscreen);++ // https://w3c.github.io/picture-in-picture/#fullscreen+ // It is RECOMMENDED that when a Picture-in-Picture element's fullscreen flag is set, the user agent runs these+ // steps:+ // AD-HOC: These steps are from the overhaul proposed in https://github.com/w3c/picture-in-picture/pull/260.+ if (is_fullscreen && is_picture_in_picture_element()) {+ // 1. Let doc be the Picture-in-Picture element's node document.+ auto& document = this->document();++ // 2. Enqueue the following steps to doc's picture-in-picture parallel queue:+ // 1. Run the exit Picture-in-Picture algorithm given doc and null.+ document.page().picture_in_picture_controller().enqueue_exit(document, nullptr);+ }}bool Element::is_picture_in_picture_element() constdiff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cppindex 24146886251..ce72bd74050 100644--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp@@ -255,7 +255,10 @@ void PictureInPictureController::did_open_window(Gfx::IntSize window_size)// NB: Nothing reads this list, and https://github.com/w3c/picture-in-picture/issues/263 proposes its removal.// 3. If this is fullscreenElement, then exit fullscreen.- // FIXME: Exit fullscreen.+ // NB: The spec links "exit fullscreen" to the fully exit fullscreen algorithm. The fullscreen element is+ // compared before it is retargeted, so that a video in a shadow tree leaves fullscreen as well.+ if (document.fullscreen_element().ptr() == video.ptr())+ document.fully_exit_fullscreen();// 4. Fire an event named enterpictureinpicture using PictureInPictureEvent at this with its bubbles attribute// initialized to true and its pictureInPictureWindow attribute initialized to Picture-in-Picture window.diff --git a/Tests/LibWeb/Text/expected/PictureInPicture/fullscreen-exits-picture-in-picture.txt b/Tests/LibWeb/Text/expected/PictureInPicture/fullscreen-exits-picture-in-picture.txtnew file mode 100644index 00000000000..8fb20768132--- /dev/null+++ b/Tests/LibWeb/Text/expected/PictureInPicture/fullscreen-exits-picture-in-picture.txt@@ -0,0 +1,3 @@+Picture-in-Picture element before: video+Picture-in-Picture element after: null+Fullscreen element after: videodiff --git a/Tests/LibWeb/Text/expected/PictureInPicture/request-exits-fullscreen.txt b/Tests/LibWeb/Text/expected/PictureInPicture/request-exits-fullscreen.txtnew file mode 100644index 00000000000..c79a4f19382--- /dev/null+++ b/Tests/LibWeb/Text/expected/PictureInPicture/request-exits-fullscreen.txt@@ -0,0 +1,3 @@+Fullscreen element before: video+Fullscreen element after: null+Picture-in-Picture element after: videodiff --git a/Tests/LibWeb/Text/input/PictureInPicture/fullscreen-exits-picture-in-picture.html b/Tests/LibWeb/Text/input/PictureInPicture/fullscreen-exits-picture-in-picture.htmlnew file mode 100644index 00000000000..0ec4a631277--- /dev/null+++ b/Tests/LibWeb/Text/input/PictureInPicture/fullscreen-exits-picture-in-picture.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ asyncTest(async done => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ println(`Picture-in-Picture element before: ${document.pictureInPictureElement === video ? "video" : document.pictureInPictureElement}`);++ const left = new Promise(resolve => video.addEventListener("leavepictureinpicture", resolve, { once: true }));+ internals.grantTransientActivation();+ await video.requestFullscreen();+ await left;++ println(`Picture-in-Picture element after: ${document.pictureInPictureElement}`);+ println(`Fullscreen element after: ${document.fullscreenElement === video ? "video" : document.fullscreenElement}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/PictureInPicture/request-exits-fullscreen.html b/Tests/LibWeb/Text/input/PictureInPicture/request-exits-fullscreen.htmlnew file mode 100644index 00000000000..51f7d819327--- /dev/null+++ b/Tests/LibWeb/Text/input/PictureInPicture/request-exits-fullscreen.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ asyncTest(async done => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ internals.grantTransientActivation();+ await video.requestFullscreen();+ println(`Fullscreen element before: ${document.fullscreenElement === video ? "video" : document.fullscreenElement}`);++ // The fullscreenchange for entering fullscreen can still be pending, so wait for the one that leaves it.+ const exitedFullscreen = new Promise(resolve => {+ document.addEventListener("fullscreenchange", function listener() {+ if (document.fullscreenElement)+ return;+ document.removeEventListener("fullscreenchange", listener);+ resolve();+ });+ });+ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ await exitedFullscreen;++ println(`Fullscreen element after: ${document.fullscreenElement}`);+ println(`Picture-in-Picture element after: ${document.pictureInPictureElement === video ? "video" : document.pictureInPictureElement}`);+ done();+ });+</script>