2022-07-11 16:37:51 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
2024-10-04 13:19:50 +02:00
|
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2022-07-11 16:37:51 +01:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
#include <LibGC/Root.h>
|
2022-07-11 16:37:51 +01:00
|
|
|
|
#include <LibWeb/DOM/MutationRecord.h>
|
2022-09-24 16:02:41 +01:00
|
|
|
|
#include <LibWeb/WebIDL/CallbackType.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#dictdef-mutationobserverinit
|
|
|
|
|
struct MutationObserverInit {
|
|
|
|
|
bool child_list { false };
|
|
|
|
|
Optional<bool> attributes;
|
|
|
|
|
Optional<bool> character_data;
|
|
|
|
|
bool subtree { false };
|
|
|
|
|
Optional<bool> attribute_old_value;
|
|
|
|
|
Optional<bool> character_data_old_value;
|
2023-09-06 14:28:15 +12:00
|
|
|
|
Optional<Vector<String>> attribute_filter;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#mutationobserver
|
2022-09-01 17:59:48 +02:00
|
|
|
|
class MutationObserver final : public Bindings::PlatformObject {
|
|
|
|
|
WEB_PLATFORM_OBJECT(MutationObserver, Bindings::PlatformObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DECLARE_ALLOCATOR(MutationObserver);
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
static WebIDL::ExceptionOr<GC::Ref<MutationObserver>> construct_impl(JS::Realm&, GC::Ptr<WebIDL::CallbackType>);
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual ~MutationObserver() override;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> observe(Node& target, MutationObserverInit options = {});
|
2022-07-11 16:37:51 +01:00
|
|
|
|
void disconnect();
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Root<MutationRecord>> take_records();
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
|
|
|
|
Vector<WeakPtr<Node>>& node_list() { return m_node_list; }
|
|
|
|
|
Vector<WeakPtr<Node>> const& node_list() const { return m_node_list; }
|
|
|
|
|
|
2022-09-24 16:02:41 +01:00
|
|
|
|
WebIDL::CallbackType& callback() { return *m_callback; }
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void enqueue_record(Badge<Node>, GC::Ref<MutationRecord> mutation_record)
|
2022-07-11 16:37:51 +01:00
|
|
|
|
{
|
2022-09-01 17:13:18 +02:00
|
|
|
|
m_record_queue.append(*mutation_record);
|
2022-07-11 16:37:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
MutationObserver(JS::Realm&, GC::Ptr<WebIDL::CallbackType>);
|
2022-09-01 17:59:48 +02:00
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-mo-callback
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<WebIDL::CallbackType> m_callback;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#mutationobserver-node-list
|
2022-09-01 17:59:48 +02:00
|
|
|
|
// NOTE: These are weak, per https://dom.spec.whatwg.org/#garbage-collection
|
|
|
|
|
// Registered observers in a node’s registered observer list have a weak reference to the node.
|
2022-07-11 16:37:51 +01:00
|
|
|
|
Vector<WeakPtr<Node>> m_node_list;
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-mo-queue
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Ref<MutationRecord>> m_record_queue;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#registered-observer
|
2022-09-01 17:59:48 +02:00
|
|
|
|
class RegisteredObserver : public JS::Cell {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_CELL(RegisteredObserver, JS::Cell);
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
static GC::Ref<RegisteredObserver> create(MutationObserver&, MutationObserverInit const&);
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual ~RegisteredObserver() override;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<MutationObserver> observer() const { return m_observer; }
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
MutationObserverInit const& options() const { return m_options; }
|
|
|
|
|
void set_options(MutationObserverInit options) { m_options = move(options); }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
RegisteredObserver(MutationObserver& observer, MutationObserverInit const& options);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
|
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<MutationObserver> m_observer;
|
2022-09-01 17:59:48 +02:00
|
|
|
|
MutationObserverInit m_options;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#transient-registered-observer
|
2022-09-01 17:59:48 +02:00
|
|
|
|
class TransientRegisteredObserver final : public RegisteredObserver {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_CELL(TransientRegisteredObserver, RegisteredObserver);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(TransientRegisteredObserver);
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
static GC::Ref<TransientRegisteredObserver> create(MutationObserver&, MutationObserverInit const&, RegisteredObserver& source);
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual ~TransientRegisteredObserver() override;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<RegisteredObserver> source() const { return m_source; }
|
2022-07-11 16:37:51 +01:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
private:
|
|
|
|
|
TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit const& options, RegisteredObserver& source);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
|
2022-09-01 17:59:48 +02:00
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<RegisteredObserver> m_source;
|
2022-07-11 16:37:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|