MutationObserver.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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<DeprecatedString>> 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. public:
  28. static WebIDL::ExceptionOr<JS::NonnullGCPtr<MutationObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  29. virtual ~MutationObserver() override;
  30. WebIDL::ExceptionOr<void> observe(Node& target, MutationObserverInit options = {});
  31. void disconnect();
  32. Vector<JS::Handle<MutationRecord>> take_records();
  33. Vector<WeakPtr<Node>>& node_list() { return m_node_list; }
  34. Vector<WeakPtr<Node>> const& node_list() const { return m_node_list; }
  35. WebIDL::CallbackType& callback() { return *m_callback; }
  36. void enqueue_record(Badge<Node>, JS::NonnullGCPtr<MutationRecord> mutation_record)
  37. {
  38. m_record_queue.append(*mutation_record);
  39. }
  40. private:
  41. MutationObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  42. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  43. virtual void visit_edges(Cell::Visitor&) override;
  44. // https://dom.spec.whatwg.org/#concept-mo-callback
  45. JS::GCPtr<WebIDL::CallbackType> m_callback;
  46. // https://dom.spec.whatwg.org/#mutationobserver-node-list
  47. // NOTE: These are weak, per https://dom.spec.whatwg.org/#garbage-collection
  48. // Registered observers in a node’s registered observer list have a weak reference to the node.
  49. Vector<WeakPtr<Node>> m_node_list;
  50. // https://dom.spec.whatwg.org/#concept-mo-queue
  51. Vector<JS::NonnullGCPtr<MutationRecord>> m_record_queue;
  52. };
  53. // https://dom.spec.whatwg.org/#registered-observer
  54. class RegisteredObserver : public JS::Cell {
  55. JS_CELL(RegisteredObserver, JS::Cell);
  56. public:
  57. static JS::NonnullGCPtr<RegisteredObserver> create(MutationObserver&, MutationObserverInit const&);
  58. virtual ~RegisteredObserver() override;
  59. JS::NonnullGCPtr<MutationObserver> observer() const { return m_observer; }
  60. MutationObserverInit const& options() const { return m_options; }
  61. void set_options(MutationObserverInit options) { m_options = move(options); }
  62. protected:
  63. RegisteredObserver(MutationObserver& observer, MutationObserverInit const& options);
  64. virtual void visit_edges(Cell::Visitor&) override;
  65. private:
  66. JS::NonnullGCPtr<MutationObserver> m_observer;
  67. MutationObserverInit m_options;
  68. };
  69. // https://dom.spec.whatwg.org/#transient-registered-observer
  70. class TransientRegisteredObserver final : public RegisteredObserver {
  71. JS_CELL(TransientRegisteredObserver, RegisteredObserver);
  72. public:
  73. static JS::NonnullGCPtr<TransientRegisteredObserver> create(MutationObserver&, MutationObserverInit const&, RegisteredObserver& source);
  74. virtual ~TransientRegisteredObserver() override;
  75. JS::NonnullGCPtr<RegisteredObserver> source() const { return m_source; }
  76. private:
  77. TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit const& options, RegisteredObserver& source);
  78. virtual void visit_edges(Cell::Visitor&) override;
  79. JS::NonnullGCPtr<RegisteredObserver> m_source;
  80. };
  81. }