LibWeb: Add a proper FocusEvent interface for "focus" and "blur" events

This commit is contained in:
Andreas Kling 2022-02-07 00:04:10 +01:00
parent 386912c019
commit 627ad6c37c
Notes: sideshowbarker 2024-07-17 19:41:28 +09:00
9 changed files with 80 additions and 3 deletions

View file

@ -918,6 +918,8 @@ static NonnullOwnPtr<Interface> parse_interface(StringView filename, StringView
static bool is_wrappable_type(Type const& type)
{
if (type.name == "EventTarget")
return true;
if (type.name == "Node")
return true;
if (type.name == "Document")

View file

@ -60,6 +60,8 @@
#include <LibWeb/Bindings/EventPrototype.h>
#include <LibWeb/Bindings/EventTargetConstructor.h>
#include <LibWeb/Bindings/EventTargetPrototype.h>
#include <LibWeb/Bindings/FocusEventConstructor.h>
#include <LibWeb/Bindings/FocusEventPrototype.h>
#include <LibWeb/Bindings/HTMLAnchorElementConstructor.h>
#include <LibWeb/Bindings/HTMLAnchorElementPrototype.h>
#include <LibWeb/Bindings/HTMLAreaElementConstructor.h>

View file

@ -272,6 +272,7 @@ set(SOURCES
Selection/Selection.cpp
StylePropertiesModel.cpp
UIEvents/EventNames.cpp
UIEvents/FocusEvent.cpp
UIEvents/KeyboardEvent.cpp
UIEvents/MouseEvent.cpp
URL/URL.cpp
@ -504,6 +505,7 @@ libweb_js_wrapper(SVG/SVGGraphicsElement)
libweb_js_wrapper(SVG/SVGPathElement)
libweb_js_wrapper(SVG/SVGSVGElement)
libweb_js_wrapper(Selection/Selection)
libweb_js_wrapper(UIEvents/FocusEvent)
libweb_js_wrapper(UIEvents/KeyboardEvent)
libweb_js_wrapper(UIEvents/MouseEvent)
libweb_js_wrapper(UIEvents/UIEvent)

View file

@ -61,6 +61,7 @@
#include <LibWeb/Page/Page.h>
#include <LibWeb/SVG/TagNames.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/FocusEvent.h>
#include <LibWeb/UIEvents/KeyboardEvent.h>
#include <LibWeb/UIEvents/MouseEvent.h>
@ -748,7 +749,7 @@ NonnullRefPtr<Event> Document::create_event(const String& interface)
} else if (interface_lowercase.is_one_of("event", "events")) {
event = Event::create("");
} else if (interface_lowercase == "focusevent") {
event = Event::create(""); // FIXME: Create FocusEvent
event = UIEvents::FocusEvent::create("");
} else if (interface_lowercase == "hashchangeevent") {
event = Event::create(""); // FIXME: Create HashChangeEvent
} else if (interface_lowercase == "htmlevents") {

View file

@ -333,6 +333,7 @@ class ElementWrapper;
class EventListenerWrapper;
class EventTargetWrapper;
class EventWrapper;
class FocusEventWrapper;
class HistoryWrapper;
class HTMLAnchorElementWrapper;
class HTMLAreaElementWrapper;

View file

@ -22,6 +22,7 @@
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/FocusEvent.h>
namespace Web::HTML {
@ -226,7 +227,7 @@ static void run_focus_update_steps(Vector<DOM::Node&> old_chain, Vector<DOM::Nod
// with related blur target as the related target.
if (blur_event_target) {
// FIXME: Implement the "fire a focus event" spec operation.
auto blur_event = DOM::Event::create(HTML::EventNames::blur);
auto blur_event = UIEvents::FocusEvent::create(HTML::EventNames::blur);
blur_event->set_related_target(related_blur_target);
blur_event_target->dispatch_event(move(blur_event));
}
@ -269,7 +270,7 @@ static void run_focus_update_steps(Vector<DOM::Node&> old_chain, Vector<DOM::Nod
// with related focus target as the related target.
if (focus_event_target) {
// FIXME: Implement the "fire a focus event" spec operation.
auto focus_event = DOM::Event::create(HTML::EventNames::focus);
auto focus_event = UIEvents::FocusEvent::create(HTML::EventNames::focus);
focus_event->set_related_target(related_focus_target);
focus_event_target->dispatch_event(move(focus_event));
}

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/UIEvents/FocusEvent.h>
namespace Web::UIEvents {
FocusEvent::FocusEvent(FlyString const& event_name, FocusEventInit const& event_init)
: UIEvent(event_name)
{
set_related_target(const_cast<DOM::EventTarget*>(event_init.related_target.ptr()));
}
FocusEvent::~FocusEvent()
{
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/UIEvents/UIEvent.h>
namespace Web::UIEvents {
struct FocusEventInit : public UIEventInit {
RefPtr<DOM::EventTarget> related_target;
};
class FocusEvent final : public UIEvent {
public:
using WrapperType = Bindings::FocusEventWrapper;
virtual ~FocusEvent() override;
static NonnullRefPtr<FocusEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, FocusEventInit const& event_init)
{
return adopt_ref(*new FocusEvent(event_name, event_init));
}
private:
FocusEvent(FlyString const& event_name, FocusEventInit const&);
};
}

View file

@ -0,0 +1,15 @@
#import <UIEvents/UIEvent.idl>
[Exposed=Window]
interface FocusEvent : UIEvent {
constructor(DOMString type, optional FocusEventInit eventInitDict = {});
readonly attribute EventTarget? relatedTarget;
};
dictionary FocusEventInit : UIEventInit {
EventTarget? relatedTarget = null;
};