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

LibWeb: Add HTML::EventNames and UIEvents::EventNames

This commit is contained in:
Luke 2020-11-21 19:15:57 +00:00 committed by Andreas Kling
parent e68348298f
commit 9950270808
15 changed files with 271 additions and 19 deletions

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibWeb/HTML/EventNames.h>
namespace Web::HTML::EventNames {
#define __ENUMERATE_HTML_EVENT(name) FlyString name;
ENUMERATE_HTML_EVENTS
#undef __ENUMERATE_HTML_EVENT
// clang-format off
// FIXME: clang-format gets confused here. Why?
[[gnu::constructor]] static void initialize()
// clang-format on
{
static bool s_initialized = false;
if (s_initialized)
return;
#define __ENUMERATE_HTML_EVENT(name) \
name = #name;
ENUMERATE_HTML_EVENTS
#undef __ENUMERATE_HTML_EVENT
s_initialized = true;
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/FlyString.h>
namespace Web::HTML::EventNames {
// FIXME: Add media events https://html.spec.whatwg.org/multipage/media.html#mediaevents
// FIXME: Add app cache events https://html.spec.whatwg.org/multipage/offline.html#appcacheevents
// FIXME: Add drag and drop events https://html.spec.whatwg.org/multipage/dnd.html#dndevents
#define ENUMERATE_HTML_EVENTS \
__ENUMERATE_HTML_EVENT(abort) \
__ENUMERATE_HTML_EVENT(DOMContentLoaded) \
__ENUMERATE_HTML_EVENT(afterprint) \
__ENUMERATE_HTML_EVENT(beforeprint) \
__ENUMERATE_HTML_EVENT(beforeunload) \
__ENUMERATE_HTML_EVENT(blur) \
__ENUMERATE_HTML_EVENT(cancel) \
__ENUMERATE_HTML_EVENT(change) \
__ENUMERATE_HTML_EVENT(click) \
__ENUMERATE_HTML_EVENT(close) \
__ENUMERATE_HTML_EVENT(connect) \
__ENUMERATE_HTML_EVENT(contextmenu) \
__ENUMERATE_HTML_EVENT(copy) \
__ENUMERATE_HTML_EVENT(cut) \
__ENUMERATE_HTML_EVENT(error) \
__ENUMERATE_HTML_EVENT(focus) \
__ENUMERATE_HTML_EVENT(formdata) \
__ENUMERATE_HTML_EVENT(hashchange) \
__ENUMERATE_HTML_EVENT(input) \
__ENUMERATE_HTML_EVENT(invalid) \
__ENUMERATE_HTML_EVENT(languagechange) \
__ENUMERATE_HTML_EVENT(load) \
__ENUMERATE_HTML_EVENT(message) \
__ENUMERATE_HTML_EVENT(messageerror) \
__ENUMERATE_HTML_EVENT(offline) \
__ENUMERATE_HTML_EVENT(online) \
__ENUMERATE_HTML_EVENT(open) \
__ENUMERATE_HTML_EVENT(pagehide) \
__ENUMERATE_HTML_EVENT(pageshow) \
__ENUMERATE_HTML_EVENT(paste) \
__ENUMERATE_HTML_EVENT(popstate) \
__ENUMERATE_HTML_EVENT(readystatechange) \
__ENUMERATE_HTML_EVENT(rejectionhandled) \
__ENUMERATE_HTML_EVENT(reset) \
__ENUMERATE_HTML_EVENT(securitypolicyviolation) \
__ENUMERATE_HTML_EVENT(select) \
__ENUMERATE_HTML_EVENT(slotchange) \
__ENUMERATE_HTML_EVENT(storage) \
__ENUMERATE_HTML_EVENT(submit) \
__ENUMERATE_HTML_EVENT(toggle) \
__ENUMERATE_HTML_EVENT(unhandledrejection) \
__ENUMERATE_HTML_EVENT(unload)
#define __ENUMERATE_HTML_EVENT(name) extern FlyString name;
ENUMERATE_HTML_EVENTS
#undef __ENUMERATE_HTML_EVENT
}

View file

@ -31,6 +31,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/Dump.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
@ -106,7 +107,7 @@ const DOM::Document* HTMLIFrameElement::content_document() const
void HTMLIFrameElement::content_frame_did_load(Badge<FrameLoader>)
{
dispatch_event(DOM::Event::create("load"));
dispatch_event(DOM::Event::create(EventNames::load));
}
}

View file

@ -30,6 +30,7 @@
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/Layout/ImageBox.h>
#include <LibWeb/Loader/ResourceLoader.h>
@ -41,13 +42,13 @@ HTMLImageElement::HTMLImageElement(DOM::Document& document, const QualifiedName&
{
m_image_loader.on_load = [this] {
this->document().update_layout();
dispatch_event(DOM::Event::create("load"));
dispatch_event(DOM::Event::create(EventNames::load));
};
m_image_loader.on_fail = [this] {
dbg() << "HTMLImageElement: Resource did fail: " << this->src();
this->document().update_layout();
dispatch_event(DOM::Event::create("error"));
dispatch_event(DOM::Event::create(EventNames::error));
};
m_image_loader.on_animate = [this] {

View file

@ -28,6 +28,7 @@
#include <LibGUI/TextBox.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/InProcessWebView.h>
@ -49,7 +50,8 @@ HTMLInputElement::~HTMLInputElement()
void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
{
dispatch_event(DOM::Event::create("click"));
// FIXME: This should be a PointerEvent.
dispatch_event(DOM::Event::create(EventNames::click));
if (type().equals_ignoring_case("submit")) {
if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
@ -104,7 +106,7 @@ void HTMLInputElement::set_checked(bool checked)
if (layout_node())
layout_node()->set_needs_display();
dispatch_event(DOM::Event::create("change"));
dispatch_event(DOM::Event::create(EventNames::change));
}
bool HTMLInputElement::enabled() const

View file

@ -29,6 +29,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/Loader/ResourceLoader.h>
@ -58,7 +59,7 @@ void HTMLScriptElement::execute_script()
document().run_javascript(m_script_source);
if (has_attribute(HTML::AttributeNames::src))
dispatch_event(DOM::Event::create("load"));
dispatch_event(DOM::Event::create(EventNames::load));
}
void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)

View file

@ -35,6 +35,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/HTML/HTMLHeadElement.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
@ -180,7 +181,7 @@ void HTMLDocumentParser::run(const URL& url)
script.execute_script();
}
auto content_loaded_event = DOM::Event::create("DOMContentLoaded");
auto content_loaded_event = DOM::Event::create(HTML::EventNames::DOMContentLoaded);
content_loaded_event->set_bubbles(true);
m_document->dispatch_event(content_loaded_event);
@ -192,7 +193,7 @@ void HTMLDocumentParser::run(const URL& url)
// FIXME: Spin the event loop until there is nothing that delays the load event in the Document.
m_document->set_ready_state("complete");
m_document->window().dispatch_event(DOM::Event::create("load"));
m_document->window().dispatch_event(DOM::Event::create(HTML::EventNames::load));
m_document->set_ready_for_post_load_tasks(true);
m_document->completely_finish_loading();