Skip to content

Truly independent web browser

LibWeb: Skip updating the rendering of documents nobody can see

Shannon Boothb40d5381a65d
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index abe99455c06..0393b6d6a52 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -914,6 +914,7 @@ public:
void run_the_scroll_steps();
void evaluate_media_queries_and_report_changes();
+ bool needs_media_query_list_evaluation() const { return m_needs_media_query_list_evaluation; }
bool needs_media_rule_evaluation() const { return m_needs_media_rule_evaluation; }
void evaluate_media_rules_for_style_update() { evaluate_media_rules(); }
void set_needs_media_query_evaluation()
@@ -1155,6 +1156,7 @@ public:
void register_resize_observer(Badge<ResizeObserver::ResizeObserver>, ResizeObserver::ResizeObserver&);
bool has_resize_observers() const { return !m_resize_observers.is_empty(); }
+ bool has_intersection_observers() const { return !m_intersection_observers.is_empty(); }
void unregister_resize_observer(Badge<ResizeObserver::ResizeObserver>, ResizeObserver::ResizeObserver&);
void add_shadow_root_needing_slot_assignment(Badge<ShadowRoot>, ShadowRoot&);
@@ -1195,6 +1197,7 @@ public:
void associate_with_animation(GC::Ref<Animations::Animation>);
void disassociate_with_animation(GC::Ref<Animations::Animation>);
size_t associated_animation_count() const;
+ bool has_associated_animations() const { return !m_associated_animations.is_empty(); }
struct PendingAnimationEvent {
GC::Ref<DOM::Event> event;
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index b6a18acda4f..613d20b9888 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -19,6 +19,7 @@
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
+#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/EventLoop/ClockPlan.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
@@ -830,7 +831,22 @@ void EventLoop::update_the_rendering()
return is_renderable(document);
});
- // FIXME: 4. Unnecessary rendering: Remove from docs any Document object doc for which all of the following are true:
+ // 4. Unnecessary rendering: Remove from docs any Document object doc for which all of the following are true:
+ // - the user agent believes that updating the rendering of doc's node navigable would have no visible effect; and
+ // - doc's map of animation frame callbacks is empty.
+ docs.remove_all_matching([](auto const& document) {
+ // NB: Nothing of a document whose navigable container isn't being rendered (e.g. a display: none iframe) can be
+ // seen. Its animations, observers and media query lists still report through the steps below, so keep a
+ // document that has any of them to report.
+ auto container = document->navigable()->container();
+ if (!container || container->has_layout_box())
+ return false;
+ return !document->window()->animation_frame_callback_driver().has_callbacks()
+ && !document->has_associated_animations()
+ && !document->has_intersection_observers()
+ && !document->has_resize_observers()
+ && !document->needs_media_query_list_evaluation();
+ });
// FIXME: 5. Remove from docs all Document objects for which the user agent believes that it's preferable to skip updating the rendering for other reasons.
diff --git a/Tests/LibWeb/Text/expected/css/media-query-list-change-in-hidden-iframe.txt b/Tests/LibWeb/Text/expected/css/media-query-list-change-in-hidden-iframe.txt
new file mode 100644
index 00000000000..dbba111d197
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/media-query-list-change-in-hidden-iframe.txt
@@ -0,0 +1 @@
+change: matches=true
diff --git a/Tests/LibWeb/Text/input/css/media-query-list-change-in-hidden-iframe.html b/Tests/LibWeb/Text/input/css/media-query-list-change-in-hidden-iframe.html
new file mode 100644
index 00000000000..e9267caab63
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/media-query-list-change-in-hidden-iframe.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="frame" style="display: none" srcdoc="<!DOCTYPE html>"></iframe>
+<script>
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ internals.setPreferredColorScheme("light");
+ const query = frame.contentWindow.matchMedia("(prefers-color-scheme: dark)");
+ query.addEventListener("change", event => {
+ println(`change: matches=${event.matches}`);
+ internals.setPreferredColorScheme("auto");
+ done();
+ });
+ internals.setPreferredColorScheme("dark");
+ });
+ });
+</script>