From 606b9ff6f33b11c19e0f2c95d5869cb67e4e53fa Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 22:34:48 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.getSelection() to IDL --- Userland/Libraries/LibWeb/DOM/Document.cpp | 13 +++++-------- Userland/Libraries/LibWeb/DOM/Document.h | 3 +-- Userland/Libraries/LibWeb/HTML/Window.cpp | 17 +++++++---------- Userland/Libraries/LibWeb/HTML/Window.h | 4 ++-- Userland/Libraries/LibWeb/HTML/Window.idl | 3 +++ 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 5b69601770..83556273a9 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -320,6 +320,8 @@ JS::ThrowCompletionOr Document::initialize(JS::Realm& realm) MUST_OR_THROW_OOM(Base::initialize(realm)); set_prototype(&Bindings::ensure_web_prototype(realm, "Document")); + m_selection = MUST_OR_THROW_OOM(heap().allocate(realm, realm, *this)); + return {}; } @@ -369,17 +371,12 @@ void Document::visit_edges(Cell::Visitor& visitor) } // https://w3c.github.io/selection-api/#dom-document-getselection -JS::GCPtr Document::get_selection() +JS::GCPtr Document::get_selection() const { // The method must return the selection associated with this if this has an associated browsing context, // and it must return null otherwise. - if (!browsing_context()) { - return nullptr; - } - - if (!m_selection) { - m_selection = Selection::Selection::create(realm(), *this).release_value_but_fixme_should_propagate_errors(); - } + if (!browsing_context()) + return {}; return m_selection; } diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index e2934bf1a3..2d39f1f0fd 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -88,8 +88,7 @@ public: static WebIDL::ExceptionOr> construct_impl(JS::Realm&); virtual ~Document() override; - // https://w3c.github.io/selection-api/#dom-document-getselection - JS::GCPtr get_selection(); + JS::GCPtr get_selection() const; size_t next_layout_node_serial_id(Badge) { return m_next_layout_node_serial_id++; } size_t layout_node_count() const { return m_next_layout_node_serial_id; } diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 083c25371c..a1b4024b9c 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -958,8 +958,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badge Window::get_selection() const +{ + // The method must invoke and return the result of getSelection() on this's Window.document attribute. + return associated_document().get_selection(); +} + // https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance WebIDL::ExceptionOr> Window::performance() { @@ -1652,14 +1657,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) return JS::js_undefined(); } -// https://w3c.github.io/selection-api/#dom-window-getselection -JS_DEFINE_NATIVE_FUNCTION(Window::get_selection) -{ - // The method must invoke and return the result of getSelection() on this's Window.document attribute. - auto* impl = TRY(impl_from(vm)); - return impl->associated_document().get_selection(); -} - JS_DEFINE_NATIVE_FUNCTION(Window::structured_clone) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 335e6c1e18..4c5ec93223 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -180,6 +180,8 @@ public: i32 outer_height() const; double device_pixel_ratio() const; + JS::GCPtr get_selection() const; + WebIDL::ExceptionOr> performance(); WebIDL::ExceptionOr> crypto(); @@ -258,8 +260,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(request_animation_frame); JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); - JS_DECLARE_NATIVE_FUNCTION(get_selection); - JS_DECLARE_NATIVE_FUNCTION(queue_microtask); JS_DECLARE_NATIVE_FUNCTION(request_idle_callback); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index daceb5001b..31a4a2f074 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -77,6 +77,9 @@ interface Window : EventTarget { [Replaceable] readonly attribute long outerHeight; [Replaceable] readonly attribute double devicePixelRatio; + // https://w3c.github.io/selection-api/#extensions-to-window-interface + Selection? getSelection(); + // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope // https://w3c.github.io/hr-time/#the-performance-attribute [Replaceable] readonly attribute Performance performance;