diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 48cf783e79..1d00c3cf20 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1673,7 +1673,7 @@ bool Document::is_active() const } // https://html.spec.whatwg.org/multipage/history.html#dom-document-location -HTML::Location* Document::location() +WebIDL::ExceptionOr> Document::location() { // The Document object's location attribute's getter must return this Document object's relevant global object's Location object, // if this Document object is fully active, and null otherwise. @@ -1681,7 +1681,7 @@ HTML::Location* Document::location() if (!is_fully_active()) return nullptr; - return window().location(); + return TRY(window().location()); } // https://html.spec.whatwg.org/multipage/interaction.html#dom-document-hidden diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 47e21b6938..17dc38ddb8 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -343,7 +343,7 @@ public: JS::NonnullGCPtr history(); JS::NonnullGCPtr history() const; - HTML::Location* location(); + WebIDL::ExceptionOr> location(); size_t number_of_things_delaying_the_load_event() { return m_number_of_things_delaying_the_load_event; } void increment_number_of_things_delaying_the_load_event(Badge); diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index a115d8aecc..caab16f09e 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -858,9 +858,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badge(realm, "Window")); - m_location = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); - m_navigator = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); - MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this)); // FIXME: These should be native accessors, not properties @@ -959,10 +956,14 @@ void Window::set_name(String const& name) } // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location -JS::NonnullGCPtr Window::location() const +WebIDL::ExceptionOr> Window::location() { + auto& realm = this->realm(); + // The Window object's location getter steps are to return this's Location object. - return *m_location; + if (!m_location) + m_location = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); + return JS::NonnullGCPtr { *m_location }; } // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history @@ -1068,10 +1069,14 @@ WebIDL::ExceptionOr> Window::open(Optional } // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator -JS::NonnullGCPtr Window::navigator() const +WebIDL::ExceptionOr> Window::navigator() { + auto& realm = this->realm(); + // The navigator and clientInformation getter steps are to return this's associated Navigator. - return *m_navigator; + if (!m_navigator) + m_navigator = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); + return JS::NonnullGCPtr { *m_navigator }; } // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-alert @@ -1470,8 +1475,10 @@ WebIDL::ExceptionOr> Window::p // https://w3c.github.io/webcrypto/#dom-windoworworkerglobalscope-crypto WebIDL::ExceptionOr> Window::crypto() { + auto& realm = this->realm(); + if (!m_crypto) - m_crypto = MUST_OR_THROW_OOM(heap().allocate(realm(), realm())); + m_crypto = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); return JS::NonnullGCPtr { *m_crypto }; } @@ -1567,7 +1574,8 @@ size_t Window::document_tree_child_browsing_context_count() const JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) { auto* impl = TRY(impl_from(vm)); - TRY(impl->m_location->set(JS::PropertyKey("href"), vm.argument(0), JS::Object::ShouldThrowExceptions::Yes)); + auto location = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->location(); })); + TRY(location->set(JS::PropertyKey("href"), vm.argument(0), JS::Object::ShouldThrowExceptions::Yes)); return JS::js_undefined(); } diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 067bf5184e..f3f770a68f 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -139,7 +139,7 @@ public: JS::NonnullGCPtr document() const; String name() const; void set_name(String const&); - JS::NonnullGCPtr location() const; + WebIDL::ExceptionOr> location(); JS::NonnullGCPtr history() const; void focus(); @@ -150,7 +150,7 @@ public: JS::GCPtr frame_element() const; WebIDL::ExceptionOr> open(Optional const& url, Optional const& target, Optional const& features); - JS::NonnullGCPtr navigator() const; + WebIDL::ExceptionOr> navigator(); void alert(String const& message = {}); bool confirm(Optional const& message);