/* * Copyright (c) 2022, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Web::DOM { // https://dom.spec.whatwg.org/#dictdef-mutationobserverinit struct MutationObserverInit { bool child_list { false }; Optional attributes; Optional character_data; bool subtree { false }; Optional attribute_old_value; Optional character_data_old_value; Optional> attribute_filter; }; // https://dom.spec.whatwg.org/#mutationobserver class MutationObserver final : public RefCounted , public Bindings::Wrappable { public: using WrapperType = Bindings::MutationObserverWrapper; static NonnullRefPtr create_with_global_object(Bindings::WindowObject& window_object, Bindings::CallbackType callback) { return adopt_ref(*new MutationObserver(window_object, move(callback))); } virtual ~MutationObserver() override = default; ExceptionOr observe(Node& target, MutationObserverInit options = {}); void disconnect(); NonnullRefPtrVector take_records(); Vector>& node_list() { return m_node_list; } Vector> const& node_list() const { return m_node_list; } Bindings::CallbackType& callback() { return m_callback; } void enqueue_record(Badge, NonnullRefPtr mutation_record) { m_record_queue.append(move(mutation_record)); } private: MutationObserver(Bindings::WindowObject& window_object, Bindings::CallbackType callback); // https://dom.spec.whatwg.org/#concept-mo-callback Bindings::CallbackType m_callback; // https://dom.spec.whatwg.org/#mutationobserver-node-list Vector> m_node_list; // https://dom.spec.whatwg.org/#concept-mo-queue NonnullRefPtrVector m_record_queue; }; // https://dom.spec.whatwg.org/#registered-observer struct RegisteredObserver : public RefCounted { static NonnullRefPtr create(MutationObserver& observer, MutationObserverInit& options) { return adopt_ref(*new RegisteredObserver(observer, options)); } RegisteredObserver(MutationObserver& observer, MutationObserverInit& options) : observer(observer) , options(options) { } virtual ~RegisteredObserver() = default; NonnullRefPtr observer; MutationObserverInit options; }; // https://dom.spec.whatwg.org/#transient-registered-observer struct TransientRegisteredObserver final : public RegisteredObserver { static NonnullRefPtr create(MutationObserver& observer, MutationObserverInit& options, RegisteredObserver& source) { return adopt_ref(*new TransientRegisteredObserver(observer, options, source)); } TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit& options, RegisteredObserver& source) : RegisteredObserver(observer, options) , source(source) { } virtual ~TransientRegisteredObserver() override = default; NonnullRefPtr source; }; }