1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:27:45 +00:00

LibWeb: Make Document::page() return a Page&

Now that Document always has a Page, and always keeps it alive, we can
make this return a Page&, exposing various unnecessary null checks.
This commit is contained in:
Andreas Kling 2023-12-15 15:41:28 +01:00
parent 70193c0009
commit 7c95ebc302
17 changed files with 43 additions and 78 deletions

View file

@ -803,10 +803,8 @@ WebIDL::ExceptionOr<void> Document::set_title(String const& title)
return {};
}
if (auto* page = this->page()) {
if (browsing_context() == &page->top_level_browsing_context())
page->client().page_did_change_title(title.to_deprecated_string());
}
if (browsing_context() == &page().top_level_browsing_context())
page().client().page_did_change_title(title.to_deprecated_string());
return {};
}
@ -1040,8 +1038,7 @@ void Document::update_layout()
navigable()->set_needs_display();
if (navigable()->is_traversable()) {
if (auto* page = this->page())
page->client().page_did_layout();
page().client().page_did_layout();
}
m_layout_root->recompute_selection_states();
@ -1909,12 +1906,12 @@ void Document::update_readiness(HTML::DocumentReadyState readiness_value)
}
}
Page* Document::page()
Page& Document::page()
{
return m_page;
}
Page const* Document::page() const
Page const& Document::page() const
{
return m_page;
}
@ -1977,9 +1974,7 @@ void Document::completely_finish_loading()
String Document::cookie(Cookie::Source source)
{
if (auto* page = this->page())
return MUST(String::from_deprecated_string(page->client().page_did_request_cookie(m_url, source)));
return String {};
return MUST(String::from_deprecated_string(page().client().page_did_request_cookie(m_url, source)));
}
void Document::set_cookie(StringView cookie_string, Cookie::Source source)
@ -1988,8 +1983,7 @@ void Document::set_cookie(StringView cookie_string, Cookie::Source source)
if (!cookie.has_value())
return;
if (auto* page = this->page())
page->client().page_did_set_cookie(m_url, cookie.value(), source);
page().client().page_did_set_cookie(m_url, cookie.value(), source);
}
String Document::dump_dom_tree_as_json() const
@ -2372,8 +2366,7 @@ void Document::increment_number_of_things_delaying_the_load_event(Badge<Document
{
++m_number_of_things_delaying_the_load_event;
if (auto* page = this->page())
page->client().page_did_update_resource_count(m_number_of_things_delaying_the_load_event);
page().client().page_did_update_resource_count(m_number_of_things_delaying_the_load_event);
}
void Document::decrement_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>)
@ -2381,8 +2374,7 @@ void Document::decrement_number_of_things_delaying_the_load_event(Badge<Document
VERIFY(m_number_of_things_delaying_the_load_event);
--m_number_of_things_delaying_the_load_event;
if (auto* page = this->page())
page->client().page_did_update_resource_count(m_number_of_things_delaying_the_load_event);
page().client().page_did_update_resource_count(m_number_of_things_delaying_the_load_event);
}
bool Document::anything_is_delaying_the_load_event() const
@ -2726,7 +2718,7 @@ void Document::run_unloading_cleanup_steps()
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#destroy-a-document
void Document::destroy()
{
page()->client().page_did_destroy_document(*this);
page().client().page_did_destroy_document(*this);
// NOTE: Abort needs to happen before destory. There is currently bug in the spec: https://github.com/whatwg/html/issues/9148
// 4. Abort document.

View file

@ -181,8 +181,8 @@ public:
void set_browsing_context(HTML::BrowsingContext*);
Page* page();
Page const* page() const;
Page& page();
Page const& page() const;
Color background_color() const;
Vector<CSS::BackgroundLayerData> const* background_layers() const;

View file

@ -1188,22 +1188,16 @@ void Element::set_scroll_left(double x)
// 8. If the element is the root element invoke scroll() on window with x as first argument and scrollY on window as second argument, and terminate these steps.
if (document.document_element() == this) {
// FIXME: Implement this in terms of invoking scroll() on window.
if (auto* page = document.page()) {
if (document.browsing_context() == &page->top_level_browsing_context())
page->client().page_did_request_scroll_to({ static_cast<float>(x), static_cast<float>(window->scroll_y()) });
}
if (document.browsing_context() == &document.page().top_level_browsing_context())
document.page().client().page_did_request_scroll_to({ static_cast<float>(x), static_cast<float>(window->scroll_y()) });
return;
}
// 9. If the element is the body element, document is in quirks mode, and the element is not potentially scrollable, invoke scroll() on window with x as first argument and scrollY on window as second argument, and terminate these steps.
if (document.body() == this && document.in_quirks_mode() && !is_potentially_scrollable()) {
// FIXME: Implement this in terms of invoking scroll() on window.
if (auto* page = document.page()) {
if (document.browsing_context() == &page->top_level_browsing_context())
page->client().page_did_request_scroll_to({ static_cast<float>(x), static_cast<float>(window->scroll_y()) });
}
if (document.browsing_context() == &document.page().top_level_browsing_context())
document.page().client().page_did_request_scroll_to({ static_cast<float>(x), static_cast<float>(window->scroll_y()) });
return;
}
@ -1256,22 +1250,16 @@ void Element::set_scroll_top(double y)
// 8. If the element is the root element invoke scroll() on window with scrollX on window as first argument and y as second argument, and terminate these steps.
if (document.document_element() == this) {
// FIXME: Implement this in terms of invoking scroll() on window.
if (auto* page = document.page()) {
if (document.browsing_context() == &page->top_level_browsing_context())
page->client().page_did_request_scroll_to({ static_cast<float>(window->scroll_x()), static_cast<float>(y) });
}
if (document.browsing_context() == &document.page().top_level_browsing_context())
document.page().client().page_did_request_scroll_to({ static_cast<float>(window->scroll_x()), static_cast<float>(y) });
return;
}
// 9. If the element is the body element, document is in quirks mode, and the element is not potentially scrollable, invoke scroll() on window with scrollX as first argument and y as second argument, and terminate these steps.
if (document.body() == this && document.in_quirks_mode() && !is_potentially_scrollable()) {
// FIXME: Implement this in terms of invoking scroll() on window.
if (auto* page = document.page()) {
if (document.browsing_context() == &page->top_level_browsing_context())
page->client().page_did_request_scroll_to({ static_cast<float>(window->scroll_x()), static_cast<float>(y) });
}
if (document.browsing_context() == &document.page().top_level_browsing_context())
document.page().client().page_did_request_scroll_to({ static_cast<float>(window->scroll_x()), static_cast<float>(y) });
return;
}