LibWeb/HTML: Implement the StorageEvent interface

This commit is contained in:
Shannon Booth 2024-12-23 22:45:19 +13:00 committed by Andreas Kling
commit f3e92f2ae5
Notes: github-actions[bot] 2024-12-25 10:58:07 +00:00
13 changed files with 333 additions and 0 deletions

View file

@ -482,6 +482,7 @@ set(SOURCES
HTML/SharedResourceRequest.cpp
HTML/SourceSet.cpp
HTML/Storage.cpp
HTML/StorageEvent.cpp
HTML/StructuredSerialize.cpp
HTML/SubmitEvent.cpp
HTML/SyntaxHighlighter/SyntaxHighlighter.cpp

View file

@ -0,0 +1,70 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/StorageEventPrototype.h>
#include <LibWeb/HTML/Storage.h>
#include <LibWeb/HTML/StorageEvent.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(StorageEvent);
GC::Ref<StorageEvent> StorageEvent::create(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
{
auto event = realm.create<StorageEvent>(realm, event_name, event_init);
event->set_is_trusted(true);
return event;
}
GC::Ref<StorageEvent> StorageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
{
return realm.create<StorageEvent>(realm, event_name, event_init);
}
StorageEvent::~StorageEvent() = default;
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storageevent-initstorageevent
void StorageEvent::init_storage_event(String const& type, bool bubbles, bool cancelable,
Optional<String> const& key, Optional<String> const& old_value, Optional<String> const& new_value,
String const& url, GC::Ptr<Storage> storage_area)
{
// The initStorageEvent(type, bubbles, cancelable, key, oldValue, newValue, url, storageArea) method must initialize
// the event in a manner analogous to the similarly-named initEvent() method. [DOM]
if (dispatched())
return;
initialize_event(type, bubbles, cancelable);
m_key = key;
m_old_value = old_value;
m_new_value = new_value;
m_url = url;
m_storage_area = storage_area;
}
StorageEvent::StorageEvent(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
: DOM::Event(realm, event_name, event_init)
, m_key(event_init.key)
, m_old_value(event_init.old_value)
, m_new_value(event_init.new_value)
, m_url(event_init.url)
, m_storage_area(event_init.storage_area)
{
}
void StorageEvent::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(StorageEvent);
}
void StorageEvent::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_storage_area);
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <AK/Optional.h>
#include <LibGC/Ptr.h>
#include <LibWeb/DOM/Event.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webstorage.html#storageeventinit
struct StorageEventInit : public DOM::EventInit {
Optional<String> key;
Optional<String> old_value;
Optional<String> new_value;
String url;
GC::Ptr<Storage> storage_area;
};
// https://html.spec.whatwg.org/multipage/webstorage.html#storageevent
class StorageEvent : public DOM::Event {
WEB_PLATFORM_OBJECT(StorageEvent, DOM::Event);
GC_DECLARE_ALLOCATOR(StorageEvent);
public:
[[nodiscard]] static GC::Ref<StorageEvent> create(JS::Realm&, FlyString const& event_name, StorageEventInit const& event_init = {});
static GC::Ref<StorageEvent> construct_impl(JS::Realm&, FlyString const& event_name, StorageEventInit const& event_init);
virtual ~StorageEvent() override;
Optional<String> const& key() const { return m_key; }
Optional<String> const& old_value() const { return m_old_value; }
Optional<String> const& new_value() const { return m_new_value; }
String const& url() const { return m_url; }
GC::Ptr<Storage const> storage_area() const { return m_storage_area; }
void init_storage_event(String const& type, bool bubbles = false, bool cancelable = false,
Optional<String> const& key = {}, Optional<String> const& old_value = {}, Optional<String> const& new_value = {},
String const& url = {}, GC::Ptr<Storage> storage_area = {});
protected:
virtual void visit_edges(Visitor& visitor) override;
virtual void initialize(JS::Realm&) override;
private:
StorageEvent(JS::Realm&, FlyString const& event_name, StorageEventInit const& event_init);
Optional<String> m_key;
Optional<String> m_old_value;
Optional<String> m_new_value;
String m_url;
GC::Ptr<Storage> m_storage_area;
};
}

View file

@ -0,0 +1,25 @@
#import <DOM/Event.idl>
#import <HTML/Storage.idl>
// https://html.spec.whatwg.org/multipage/webstorage.html#storageevent
[Exposed=Window]
interface StorageEvent : Event {
constructor(DOMString type, optional StorageEventInit eventInitDict = {});
readonly attribute DOMString? key;
readonly attribute DOMString? oldValue;
readonly attribute DOMString? newValue;
readonly attribute USVString url;
readonly attribute Storage? storageArea;
undefined initStorageEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional DOMString? key = null, optional DOMString? oldValue = null, optional DOMString? newValue = null, optional USVString url = "", optional Storage? storageArea = null);
};
// https://html.spec.whatwg.org/multipage/webstorage.html#storageeventinit
dictionary StorageEventInit : EventInit {
DOMString? key = null;
DOMString? oldValue = null;
DOMString? newValue = null;
USVString url = "";
Storage? storageArea = null;
};

View file

@ -224,6 +224,7 @@ libweb_js_bindings(HTML/PromiseRejectionEvent)
libweb_js_bindings(HTML/RadioNodeList)
libweb_js_bindings(HTML/ShadowRealmGlobalScope GLOBAL)
libweb_js_bindings(HTML/Storage)
libweb_js_bindings(HTML/StorageEvent)
libweb_js_bindings(HTML/SubmitEvent)
libweb_js_bindings(HTML/TextMetrics)
libweb_js_bindings(HTML/TextTrack)