diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 2fb07b6ed5..e80db840ff 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -67,6 +67,7 @@ #include #include #include +#include #include #include #include @@ -335,6 +336,7 @@ void Document::visit_edges(Cell::Visitor& visitor) visitor.visit(m_forms); visitor.visit(m_scripts); visitor.visit(m_all); + visitor.visit(m_selection); for (auto& script : m_scripts_to_execute_when_parsing_has_finished) visitor.visit(script.ptr()); @@ -350,6 +352,20 @@ void Document::visit_edges(Cell::Visitor& visitor) visitor.visit(target.ptr()); } +// https://w3c.github.io/selection-api/#dom-document-getselection +JS::GCPtr Document::get_selection() +{ + // 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()); + } + return m_selection; +} + // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-write WebIDL::ExceptionOr Document::write(Vector const& strings) { diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 5524a87752..3633815d04 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -87,6 +87,9 @@ public: static JS::NonnullGCPtr construct_impl(JS::Realm&); virtual ~Document() override; + // https://w3c.github.io/selection-api/#dom-document-getselection + JS::GCPtr get_selection(); + 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; } @@ -592,6 +595,8 @@ private: // https://html.spec.whatwg.org/multipage/dom.html#previous-document-unload-timing DocumentUnloadTimingInfo m_previous_document_unload_timing; + + JS::GCPtr m_selection; }; } diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 3a71f21144..dd6af7e591 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -525,13 +525,6 @@ int Window::screen_y() const return 0; } -// https://w3c.github.io/selection-api/#dom-window-getselection -Selection::Selection* Window::get_selection_impl() -{ - // FIXME: Implement. - return nullptr; -} - // https://html.spec.whatwg.org/multipage/webstorage.html#dom-localstorage JS::NonnullGCPtr Window::local_storage() { @@ -1241,10 +1234,12 @@ JS_DEFINE_NATIVE_FUNCTION(Window::get_computed_style) return impl->get_computed_style_impl(*static_cast(object)); } +// 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->get_selection_impl(); + return impl->associated_document().get_selection(); } JS_DEFINE_NATIVE_FUNCTION(Window::match_media) diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 8e33443125..cda069cac4 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -102,8 +102,6 @@ public: int screen_x() const; int screen_y() const; - Selection::Selection* get_selection_impl(); - JS::NonnullGCPtr local_storage(); JS::NonnullGCPtr session_storage();