Skip to content

Truly independent web browser

LibWeb: Keep IntersectionObserver targets weakly

Shannon Booth7317afb37ba0
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index fb10ec07078..a5184addb76 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -7438,6 +7438,8 @@ void Document::run_the_update_intersection_observations_steps(HighResolutionTime
};
for (auto& observer : intersection_observers) {
+ observer->remove_dead_observation_targets();
+
auto intersection_root_node = observer->intersection_root_node();
// NB: The root and the targets may each be in another document than this one, and their boxes are reads of
// the render state of their own documents.
@@ -7465,7 +7467,11 @@ void Document::run_the_update_intersection_observations_steps(HighResolutionTime
// 2. For each target in observer’s internal [[ObservationTargets]] slot, processed in the same order that
// observe() was called on each target:
for (auto& observed_target : observer->observation_targets()) {
- auto& target = observed_target.target;
+ // NB: A collection during this loop can clear a target that the pruning above kept.
+ auto maybe_target = observed_target.target.ptr();
+ if (!maybe_target)
+ continue;
+ GC::Ref<Element> target = *maybe_target;
// 1. Let:
// thresholdIndex be 0.
size_t threshold_index = 0;
@@ -8565,9 +8571,12 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
if (root->is_element())
index_shadow_including_element_ancestors(*root, elements_with_intersection_observation_descendants);
for (auto const& observation : observer.observation_targets()) {
- index_shadow_including_element_ancestors(observation.target, elements_with_intersection_observation_descendants);
+ auto target = observation.target.ptr();
+ if (!target)
+ continue;
+ index_shadow_including_element_ancestors(*target, elements_with_intersection_observation_descendants);
if (observer.track_visibility())
- index_shadow_including_element_ancestors(observation.target, elements_with_visibility_observation_descendants);
+ index_shadow_including_element_ancestors(*target, elements_with_visibility_observation_descendants);
}
}
@@ -8803,15 +8812,18 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
return true;
bool subtree_is_clipped_outside_root = transform_subtree_is_clipped_outside(animated_target, observer.root_intersection_rectangle(read));
for (auto const& observation : observer.observation_targets()) {
- if (!animated_target.is_shadow_including_inclusive_ancestor_of(*observation.target) || subtree_is_clipped_outside_root)
+ auto target = observation.target.ptr();
+ if (!target)
+ continue;
+ if (!animated_target.is_shadow_including_inclusive_ancestor_of(*target) || subtree_is_clipped_outside_root)
continue;
if (only_translates_horizontally && ancestor_transform_can_map_horizontal_motion_to_vertical(animated_target, *root)) {
requires_main_thread_sampling = true;
return true;
}
- if (only_translates_horizontally && !observation_has_another_transform_animation(animated_target, *observation.target, effect)) {
- auto const& target_rect = observation_target_rect_cache.ensure(observation.target, [&] {
- return observation.target->bounding_client_rect_assuming_layout_clean();
+ if (only_translates_horizontally && !observation_has_another_transform_animation(animated_target, *target, effect)) {
+ auto const& target_rect = observation_target_rect_cache.ensure(target, [&] {
+ return target->bounding_client_rect_assuming_layout_clean();
});
auto root_rect = observer.root_intersection_rectangle(read);
bool vertical_bands_are_disjoint = target_rect.bottom() < root_rect.top() || target_rect.top() > root_rect.bottom();
diff --git a/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp b/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp
index cabdfca5e15..f177bbcb8d5 100644
--- a/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp
+++ b/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp
@@ -176,8 +176,13 @@ void IntersectionObserver::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_root);
visitor.visit(m_callback);
visitor.visit(m_queued_entries);
- for (auto& observed_target : m_observation_targets)
- visitor.visit(observed_target.target);
+}
+
+void IntersectionObserver::remove_dead_observation_targets()
+{
+ m_observation_targets.remove_all_matching([](auto const& observed_target) {
+ return !observed_target.target;
+ });
}
// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-observe
@@ -187,7 +192,7 @@ void IntersectionObserver::observe(DOM::Element& target)
// https://www.w3.org/TR/intersection-observer/#observe-a-target-element
// 1. If target is in observer’s internal [[ObservationTargets]] slot, return.
auto observed_target = m_observation_targets.find_if([&target](auto const& entry) {
- return entry.target.ptr() == &target;
+ return entry.target == &target;
});
if (!observed_target.is_end())
return;
@@ -228,7 +233,7 @@ void IntersectionObserver::unobserve(DOM::Element& target)
// 2. Remove target from this’s internal [[ObservationTargets]] slot, if present
m_observation_targets.remove_first_matching([&target](ObservationTarget const& entry) {
- return entry.target.ptr() == &target;
+ return entry.target == &target;
});
}
@@ -240,7 +245,8 @@ void IntersectionObserver::disconnect()
// [[RegisteredIntersectionObservers]] slot.
// 2. Remove target from this’s internal [[ObservationTargets]] slot.
for (auto& observed_target : m_observation_targets) {
- observed_target.target->unregister_intersection_observer({}, *this);
+ if (auto target = observed_target.target.ptr())
+ target->unregister_intersection_observer({}, *this);
}
m_observation_targets.clear();
}
diff --git a/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h b/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h
index 70001332525..31b1fdb6ba7 100644
--- a/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h
+++ b/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h
@@ -23,7 +23,7 @@ using IntersectionObserverRoot = NullableIntersectionObserverRoot;
using IntersectionObserverOptions = Bindings::IntersectionObserverInit;
struct ObservationTarget {
- GC::Ref<DOM::Element> target;
+ GC::Weak<DOM::Element> target;
Optional<size_t> previous_threshold_index;
bool previous_is_intersecting { false };
};
@@ -46,6 +46,7 @@ public:
Vector<ObservationTarget>& observation_targets() { return m_observation_targets; }
Vector<ObservationTarget> const& observation_targets() const { return m_observation_targets; }
+ void remove_dead_observation_targets();
WebIDL::CallbackType& callback() { return *m_callback; }
NullableIntersectionObserverRoot root() const;
diff --git a/Tests/LibWeb/Text/expected/IntersectionObserver/removed-target-is-collectable.txt b/Tests/LibWeb/Text/expected/IntersectionObserver/removed-target-is-collectable.txt
new file mode 100644
index 00000000000..980221f92b9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/IntersectionObserver/removed-target-is-collectable.txt
@@ -0,0 +1,3 @@
+initial entries: removed
+removed target collected: true
+entries after collection: live
diff --git a/Tests/LibWeb/Text/input/IntersectionObserver/removed-target-is-collectable.html b/Tests/LibWeb/Text/input/IntersectionObserver/removed-target-is-collectable.html
new file mode 100644
index 00000000000..2ea6521de37
--- /dev/null
+++ b/Tests/LibWeb/Text/input/IntersectionObserver/removed-target-is-collectable.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<style>
+div {
+ width: 100px;
+ height: 100px;
+}
+</style>
+<script src="../include.js"></script>
+<div id="live"></div>
+<script>
+promiseTest(async () => {
+ let resolveEntries;
+ const observer = new IntersectionObserver(entries => {
+ resolveEntries(entries.map(entry => entry.target.id).join(", "));
+ });
+ function observe(target) {
+ const entries = new Promise(resolve => resolveEntries = resolve);
+ observer.observe(target);
+ return entries;
+ }
+
+ async function observeAndRemove() {
+ const removed = document.createElement("div");
+ removed.id = "removed";
+ document.body.append(removed);
+ println(`initial entries: ${await observe(removed)}`);
+ removed.remove();
+ // Preserve the wrapper so collection checks the element, not just a disposable JS wrapper.
+ removed.gcSentinel = true;
+ return new WeakRef(removed);
+ }
+ const weakTarget = await observeAndRemove();
+
+ await collectGarbageUntilCleared([weakTarget]);
+ println(`removed target collected: ${weakTarget.deref() === undefined}`);
+
+ println(`entries after collection: ${await observe(document.getElementById("live"))}`);
+});
+</script>