Skip to content

Commit

Permalink
LibWeb: Don't fire resize event until document actually resizes once
Browse files Browse the repository at this point in the history
The first time Document learns its viewport size, we now suppress firing
of the resize event.

This fixes an issue on multiple websites that were not expecting resize
events to fire so early in the loading process.
  • Loading branch information
awesomekling committed Jul 10, 2024
1 parent 0cdbcfd commit 4e7558c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/no-window-resize-on-load.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resize count: 0
12 changes: 12 additions & 0 deletions Tests/LibWeb/Text/input/no-window-resize-on-load.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
var resizeCount = 0;
onresize = function() {
++resizeCount;
}
</script>
<script src="include.js"></script>
<script>
test(() => {
println("resize count: " + resizeCount);
})
</script>
5 changes: 4 additions & 1 deletion Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2553,11 +2553,14 @@ void Document::run_the_resize_steps()
// fire an event named resize at the Window object associated with doc.

auto viewport_size = viewport_rect().size().to_type<int>();
bool is_initial_size = !m_last_viewport_size.has_value();

if (m_last_viewport_size == viewport_size)
return;
m_last_viewport_size = viewport_size;

window()->dispatch_event(DOM::Event::create(realm(), UIEvents::EventNames::resize));
if (!is_initial_size)
window()->dispatch_event(DOM::Event::create(realm(), UIEvents::EventNames::resize));

schedule_layout_update();
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/DOM/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ class Document
bool m_page_showing { false };

// Used by run_the_resize_steps().
Gfx::IntSize m_last_viewport_size;
Optional<Gfx::IntSize> m_last_viewport_size;

HashTable<ViewportClient*> m_viewport_clients;

Expand Down

0 comments on commit 4e7558c

Please sign in to comment.