MutationObserver.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefCounted.h>
  9. #include <LibJS/Heap/Handle.h>
  10. #include <LibWeb/DOM/MutationRecord.h>
  11. #include <LibWeb/WebIDL/CallbackType.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::DOM {
  14. // https://dom.spec.whatwg.org/#dictdef-mutationobserverinit
  15. struct MutationObserverInit {
  16. bool child_list { false };
  17. Optional<bool> attributes;
  18. Optional<bool> character_data;
  19. bool subtree { false };
  20. Optional<bool> attribute_old_value;
  21. Optional<bool> character_data_old_value;
  22. Optional<Vector<String>> attribute_filter;
  23. };
  24. // https://dom.spec.whatwg.org/#mutationobserver
  25. class MutationObserver final : public Bindings::PlatformObject {
  26. WEB_PLATFORM_OBJECT(MutationObserver, Bindings::PlatformObject);
  27. JS_DECLARE_ALLOCATOR(MutationObserver);
  28. public:
  29. static WebIDL::ExceptionOr<JS::NonnullGCPtr<MutationObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  30. virtual ~MutationObserver() override;
  31. WebIDL::ExceptionOr<void> observe(Node& target, MutationObserverInit options = {});
  32. void disconnect();
  33. Vector<JS::Handle<MutationRecord>> take_records();
  34. Vector<WeakPtr<Node>>& node_list() { return m_node_list; }
  35. Vector<WeakPtr<Node>> const& node_list() const { return m_node_list; }
  36. WebIDL::CallbackType& callback() { return *m_callback; }
  37. void enqueue_record(Badge<Node>, JS::NonnullGCPtr<MutationRecord> mutation_record)
  38. {
  39. m_record_queue.append(*mutation_record);
  40. }
  41. private:
  42. MutationObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  43. virtual void initialize(JS::Realm&) override;
  44. virtual void visit_edges(Cell::Visitor&) override;
  45. // https://dom.spec.whatwg.org/#concept-mo-callback
  46. JS::GCPtr<WebIDL::CallbackType> m_callback;
  47. // https://dom.spec.whatwg.org/#mutationobserver-node-list
  48. // NOTE: These are weak, per https://dom.spec.whatwg.org/#garbage-collection
  49. // Registered observers in a node’s registered observer list have a weak reference to the node.
  50. Vector<WeakPtr<Node>> m_node_list;
  51. // https://dom.spec.whatwg.org/#concept-mo-queue
  52. Vector<JS::NonnullGCPtr<MutationRecord>> m_record_queue;
  53. };
  54. // https://dom.spec.whatwg.org/#registered-observer
  55. class RegisteredObserver : public JS::Cell {
  56. JS_CELL(RegisteredObserver, JS::Cell);
  57. public:
  58. static JS::NonnullGCPtr<RegisteredObserver> create(MutationObserver&, MutationObserverInit const&);
  59. virtual ~RegisteredObserver() override;
  60. JS::NonnullGCPtr<MutationObserver> observer() const { return m_observer; }
  61. MutationObserverInit const& options() const { return m_options; }
  62. void set_options(MutationObserverInit options) { m_options = move(options); }
  63. protected:
  64. RegisteredObserver(MutationObserver& observer, MutationObserverInit const& options);
  65. virtual void visit_edges(Cell::Visitor&) override;
  66. private:
  67. JS::NonnullGCPtr<MutationObserver> m_observer;
  68. MutationObserverInit m_options;
  69. };
  70. // https://dom.spec.whatwg.org/#transient-registered-observer
  71. class TransientRegisteredObserver final : public RegisteredObserver {
  72. JS_CELL(TransientRegisteredObserver, RegisteredObserver);
  73. public:
  74. static JS::NonnullGCPtr<TransientRegisteredObserver> create(MutationObserver&, MutationObserverInit const&, RegisteredObserver& source);
  75. virtual ~TransientRegisteredObserver() override;
  76. JS::NonnullGCPtr<RegisteredObserver> source() const { return m_source; }
  77. private:
  78. TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit const& options, RegisteredObserver& source);
  79. virtual void visit_edges(Cell::Visitor&) override;
  80. JS::NonnullGCPtr<RegisteredObserver> m_source;
  81. };
  82. }