Skip to content

Truly independent web browser

LibWeb: Keep a ResizeObserver observation when observing it again

Andreas Kling374fb39de139
diff --git a/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp b/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp
index ddbfcaa4cf5..c8a94da6bcc 100644
--- a/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp
+++ b/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp
@@ -82,8 +82,15 @@ void ResizeObserver::observe(DOM::Element& target, ResizeObserverOptions options
{
// 1. If target is in [[observationTargets]] slot, call unobserve() with argument target.
auto observation = m_observation_targets.find_if([&](auto& observation) { return observation->target().ptr() == ⌖ });
- if (!observation.is_end())
+ if (!observation.is_end()) {
+ // INTEROP: Other engines keep an observation of the same box as it is. Replacing it would forget the size it
+ // last reported, and notify the observer again about a size that has not changed, so a page that
+ // re-observes its targets from its callback would be notified on every frame.
+ // https://github.com/web-platform-tests/wpt/issues/30708
+ if ((*observation)->observed_box() == options.box)
+ return;
unobserve(target);
+ }
// 2. Let observedBox be the value of the box dictionary member of options.
auto observed_box = options.box;
diff --git a/Tests/LibWeb/Text/expected/ResizeObserver/observe-same-box-again-does-not-notify.txt b/Tests/LibWeb/Text/expected/ResizeObserver/observe-same-box-again-does-not-notify.txt
new file mode 100644
index 00000000000..3e244c60590
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ResizeObserver/observe-same-box-again-does-not-notify.txt
@@ -0,0 +1,3 @@
+notification 1: width=200
+notifications after five more frames: 1
+notification 2: width=300
diff --git a/Tests/LibWeb/Text/input/ResizeObserver/observe-same-box-again-does-not-notify.html b/Tests/LibWeb/Text/input/ResizeObserver/observe-same-box-again-does-not-notify.html
new file mode 100644
index 00000000000..c9e280d5bbf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ResizeObserver/observe-same-box-again-does-not-notify.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<style>
+#box {
+ width: 200px;
+ height: 100px;
+}
+</style>
+<script src="../include.js"></script>
+<div id="box"></div>
+<script>
+ // Observing a target again with the box it is already observed with keeps its observation, so the observer is not
+ // notified again about a size that has not changed. Pages re-observe from their callbacks, and would otherwise
+ // be notified on every frame.
+ promiseTest(async () => {
+ const box = document.getElementById("box");
+ let notifications = 0;
+ let notified;
+ const observer = new ResizeObserver(entries => {
+ ++notifications;
+ println(`notification ${notifications}: width=${entries[0].contentRect.width}`);
+ observer.observe(box);
+ notified?.();
+ });
+
+ await new Promise(resolve => {
+ notified = resolve;
+ observer.observe(box);
+ });
+ for (let i = 0; i < 5; ++i)
+ await animationFrame();
+ println(`notifications after five more frames: ${notifications}`);
+
+ await new Promise(resolve => {
+ notified = resolve;
+ box.style.width = "300px";
+ });
+ observer.disconnect();
+ });
+</script>