Skip to content

Truly independent web browser

LibWeb: Add the :picture-in-picture pseudo-class

Luke Wilde2b4589db5290
diff --git a/Libraries/LibWeb/CSS/PseudoClasses.json b/Libraries/LibWeb/CSS/PseudoClasses.json
index 6a0bef6dcc6..7a525cd4d9a 100644
--- a/Libraries/LibWeb/CSS/PseudoClasses.json
+++ b/Libraries/LibWeb/CSS/PseudoClasses.json
@@ -171,6 +171,10 @@
"argument": "",
"style-engine-state": true
},
+ "picture-in-picture": {
+ "argument": "",
+ "style-engine-state": true
+ },
"placeholder-shown": {
"argument": "",
"style-engine-state": true
diff --git a/Libraries/LibWeb/CSS/SelectorMatching.cpp b/Libraries/LibWeb/CSS/SelectorMatching.cpp
index 63f2b412a62..20a9a8ffdaa 100644
--- a/Libraries/LibWeb/CSS/SelectorMatching.cpp
+++ b/Libraries/LibWeb/CSS/SelectorMatching.cpp
@@ -148,6 +148,7 @@ static bool element_popover_is_open(DOM::Element const& element)
X(Indeterminate, matches_indeterminate_pseudo_class(element)) \
X(Modal, element_is_modal(element)) \
X(Open, matches_open_state_pseudo_class(element)) \
+ X(PictureInPicture, element.is_picture_in_picture_element()) \
X(PlaceholderShown, element.matches_placeholder_shown_pseudo_class()) \
X(PopoverOpen, element_popover_is_open(element)) \
X(Target, element.is_target()) \
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index faf3af248f1..335e02de08a 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -10554,7 +10554,14 @@ GC::Ref<GC::HeapVector<GC::Ref<Document>>> Document::collect_documents_to_unfull
void Document::set_picture_in_picture_element(GC::Ptr<Element> element)
{
- m_picture_in_picture_element = element;
+ if (m_picture_in_picture_element == element)
+ return;
+
+ auto old_element = exchange(m_picture_in_picture_element, element);
+ if (old_element)
+ CSS::record_element_state_changed(*old_element, CSS::PseudoClass::PictureInPicture, false);
+ if (element)
+ CSS::record_element_state_changed(*element, CSS::PseudoClass::PictureInPicture, true);
}
// https://w3c.github.io/picture-in-picture/#dom-documentorshadowroot-pictureinpictureelement
diff --git a/Libraries/LibWeb/Rust/src/css/style/compiler.rs b/Libraries/LibWeb/Rust/src/css/style/compiler.rs
index 61ec37e2c1f..9295da75395 100644
--- a/Libraries/LibWeb/Rust/src/css/style/compiler.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/compiler.rs
@@ -1223,6 +1223,7 @@ impl<'a, A: AtomSpace> SelectorCompiler<'a, A> {
| Pc::OptimalValue
| Pc::Optional
| Pc::Paused
+ | Pc::PictureInPicture
| Pc::PlaceholderShown
| Pc::Playing
| Pc::PopoverOpen
diff --git a/Libraries/LibWeb/Rust/src/css/style/transaction.rs b/Libraries/LibWeb/Rust/src/css/style/transaction.rs
index b9bfa184fe9..87264982275 100644
--- a/Libraries/LibWeb/Rust/src/css/style/transaction.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/transaction.rs
@@ -73,6 +73,7 @@ impl StateFact {
| Self::Fullscreen
| Self::Hover
| Self::Modal
+ | Self::PictureInPicture
| Self::PopoverOpen
| Self::Target
)
diff --git a/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/css-selector.txt b/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/css-selector.txt
new file mode 100644
index 00000000000..2b90fed16df
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/css-selector.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Entering and leaving Picture-in-Picture toggles CSS selector
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/shadow-dom.txt b/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/shadow-dom.txt
index 4e3082fbb74..2678a5e1d41 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/shadow-dom.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/picture-in-picture/shadow-dom.txt
@@ -2,9 +2,8 @@ Harness status: OK
Found 5 tests
-4 Pass
-1 Fail
-Fail Test for Picture-In-Picture and Shadow DOM
+5 Pass
+Pass Test for Picture-In-Picture and Shadow DOM
Pass Picture-in-Picture on a video inside an open shadow root attached to a disconnected host
Pass Picture-in-Picture on a video inside a closed shadow root attached to a disconnected host
Pass Connecting a previously-disconnected shadow host after Picture-in-Picture starts exposes the video through shadowRoot.pictureInPictureElement
diff --git a/Tests/LibWeb/Text/input/wpt-import/picture-in-picture/css-selector.html b/Tests/LibWeb/Text/input/wpt-import/picture-in-picture/css-selector.html
new file mode 100644
index 00000000000..84e14610ea8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/picture-in-picture/css-selector.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<title>Test CSS selector :picture-in-picture</title>
+<script src="../common/media.js"></script>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="../resources/testdriver.js"></script>
+<script src="../resources/testdriver-vendor.js"></script>
+<script src="resources/picture-in-picture-helpers.js"></script>
+<style>
+ video {
+ color: rgb(0, 0, 255);
+ }
+ :picture-in-picture {
+ color: rgb(0, 255, 0);
+ }
+ /* illegal selector list */
+ video, :picture-in-picture(*) {
+ color: rgb(255, 0, 0);
+ }
+</style>
+<body></body>
+<script>
+promise_test(async t => {
+ const video = await loadVideo();
+ document.body.appendChild(video);
+ assert_equals(getComputedStyle(video).color, 'rgb(0, 0, 255)');
+
+ await requestPictureInPictureWithTrustedClick(video);
+ assert_equals(getComputedStyle(video).color, 'rgb(0, 255, 0)');
+
+ await document.exitPictureInPicture();
+ assert_equals(getComputedStyle(video).color, 'rgb(0, 0, 255)');
+}, 'Entering and leaving Picture-in-Picture toggles CSS selector');
+</script>