1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibWeb/HTML: Port Window.getSelection() to IDL

This commit is contained in:
Linus Groh 2023-03-06 22:34:48 +00:00
parent 56550b6ec0
commit 606b9ff6f3
5 changed files with 18 additions and 22 deletions

View file

@ -320,6 +320,8 @@ JS::ThrowCompletionOr<void> Document::initialize(JS::Realm& realm)
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::DocumentPrototype>(realm, "Document"));
m_selection = MUST_OR_THROW_OOM(heap().allocate<Selection::Selection>(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<Selection::Selection> Document::get_selection()
JS::GCPtr<Selection::Selection> 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;
}

View file

@ -88,8 +88,7 @@ public:
static WebIDL::ExceptionOr<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();
JS::GCPtr<Selection::Selection> get_selection() const;
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; }

View file

@ -958,8 +958,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "requestIdleCallback", request_idle_callback, 1, attr);
define_native_function(realm, "cancelIdleCallback", cancel_idle_callback, 1, attr);
define_native_function(realm, "getSelection", get_selection, 0, attr);
define_native_function(realm, "structuredClone", structured_clone, 1, attr);
define_native_function(realm, "fetch", Bindings::fetch, 1, attr);
@ -1478,6 +1476,13 @@ double Window::device_pixel_ratio() const
return 1;
}
// https://w3c.github.io/selection-api/#dom-window-getselection
JS::GCPtr<Selection::Selection> 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<JS::NonnullGCPtr<HighResolutionTime::Performance>> 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));

View file

@ -180,6 +180,8 @@ public:
i32 outer_height() const;
double device_pixel_ratio() const;
JS::GCPtr<Selection::Selection> get_selection() const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> 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);

View file

@ -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;