LibWeb: Make Window.getSelection() forward to Document.getSelection()

(And have document create the Selection object on demand.)
This commit is contained in:
Andreas Kling 2022-10-10 21:23:03 +02:00
parent b945d164e2
commit a2348ebcc0
Notes: sideshowbarker 2024-07-17 21:11:12 +09:00
4 changed files with 24 additions and 10 deletions
Userland/Libraries/LibWeb

View file

@ -67,6 +67,7 @@
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/Timer.h>
#include <LibWeb/SVG/TagNames.h>
#include <LibWeb/Selection/Selection.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/FocusEvent.h>
#include <LibWeb/UIEvents/KeyboardEvent.h>
@ -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<Selection::Selection> 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<void> Document::write(Vector<String> const& strings)
{

View file

@ -87,6 +87,9 @@ public:
static JS::NonnullGCPtr<Document> construct_impl(JS::Realm&);
virtual ~Document() override;
// https://w3c.github.io/selection-api/#dom-document-getselection
JS::GCPtr<Selection::Selection> get_selection();
size_t next_layout_node_serial_id(Badge<Layout::Node>) { 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<Selection::Selection> m_selection;
};
}

View file

@ -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<HTML::Storage> Window::local_storage()
{
@ -1241,10 +1234,12 @@ JS_DEFINE_NATIVE_FUNCTION(Window::get_computed_style)
return impl->get_computed_style_impl(*static_cast<DOM::Element*>(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)

View file

@ -102,8 +102,6 @@ public:
int screen_x() const;
int screen_y() const;
Selection::Selection* get_selection_impl();
JS::NonnullGCPtr<HTML::Storage> local_storage();
JS::NonnullGCPtr<HTML::Storage> session_storage();