MutationObserver.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/MainThreadVM.h>
  8. #include <LibWeb/DOM/MutationObserver.h>
  9. #include <LibWeb/DOM/Node.h>
  10. namespace Web::DOM {
  11. JS_DEFINE_ALLOCATOR(MutationObserver);
  12. JS_DEFINE_ALLOCATOR(TransientRegisteredObserver);
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<MutationObserver>> MutationObserver::construct_impl(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
  14. {
  15. return realm.heap().allocate<MutationObserver>(realm, realm, callback);
  16. }
  17. // https://dom.spec.whatwg.org/#dom-mutationobserver-mutationobserver
  18. MutationObserver::MutationObserver(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
  19. : PlatformObject(realm)
  20. , m_callback(move(callback))
  21. {
  22. // 1. Set this’s callback to callback.
  23. // 2. Append this to this’s relevant agent’s mutation observers.
  24. auto* agent_custom_data = verify_cast<Bindings::WebEngineCustomData>(realm.vm().custom_data());
  25. agent_custom_data->mutation_observers.append(*this);
  26. }
  27. MutationObserver::~MutationObserver()
  28. {
  29. auto* agent_custom_data = verify_cast<Bindings::WebEngineCustomData>(vm().custom_data());
  30. agent_custom_data->mutation_observers.remove_all_matching([this](auto& observer) {
  31. return observer.ptr() == this;
  32. });
  33. }
  34. void MutationObserver::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. WEB_SET_PROTOTYPE_FOR_INTERFACE(MutationObserver);
  38. }
  39. void MutationObserver::visit_edges(Cell::Visitor& visitor)
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_callback);
  43. visitor.visit(m_record_queue);
  44. }
  45. // https://dom.spec.whatwg.org/#dom-mutationobserver-observe
  46. WebIDL::ExceptionOr<void> MutationObserver::observe(Node& target, MutationObserverInit options)
  47. {
  48. // 1. If either options["attributeOldValue"] or options["attributeFilter"] exists, and options["attributes"] does not exist, then set options["attributes"] to true.
  49. if ((options.attribute_old_value.has_value() || options.attribute_filter.has_value()) && !options.attributes.has_value())
  50. options.attributes = true;
  51. // 2. If options["characterDataOldValue"] exists and options["characterData"] does not exist, then set options["characterData"] to true.
  52. if (options.character_data_old_value.has_value() && !options.character_data.has_value())
  53. options.character_data = true;
  54. // 3. If none of options["childList"], options["attributes"], and options["characterData"] is true, then throw a TypeError.
  55. if (!options.child_list && (!options.attributes.has_value() || !options.attributes.value()) && (!options.character_data.has_value() || !options.character_data.value()))
  56. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Options must have one of childList, attributes or characterData set to true."sv };
  57. // 4. If options["attributeOldValue"] is true and options["attributes"] is false, then throw a TypeError.
  58. // NOTE: If attributeOldValue is present, attributes will be present because of step 1.
  59. if (options.attribute_old_value.has_value() && options.attribute_old_value.value() && !options.attributes.value())
  60. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "attributes must be true if attributeOldValue is true."sv };
  61. // 5. If options["attributeFilter"] is present and options["attributes"] is false, then throw a TypeError.
  62. // NOTE: If attributeFilter is present, attributes will be present because of step 1.
  63. if (options.attribute_filter.has_value() && !options.attributes.value())
  64. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "attributes must be true if attributeFilter is present."sv };
  65. // 6. If options["characterDataOldValue"] is true and options["characterData"] is false, then throw a TypeError.
  66. // NOTE: If characterDataOldValue is present, characterData will be present because of step 2.
  67. if (options.character_data_old_value.has_value() && options.character_data_old_value.value() && !options.character_data.value())
  68. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "characterData must be true if characterDataOldValue is true."sv };
  69. // 7. For each registered of target’s registered observer list, if registered’s observer is this:
  70. bool updated_existing_observer = false;
  71. if (target.registered_observer_list()) {
  72. for (auto& registered_observer : *target.registered_observer_list()) {
  73. if (registered_observer->observer().ptr() != this)
  74. continue;
  75. updated_existing_observer = true;
  76. // 1. For each node of this’s node list, remove all transient registered observers whose source is registered from node’s registered observer list.
  77. for (auto& node : m_node_list) {
  78. // FIXME: Is this correct?
  79. if (node.is_null())
  80. continue;
  81. if (node->registered_observer_list()) {
  82. node->registered_observer_list()->remove_all_matching([&registered_observer](RegisteredObserver& observer) {
  83. return is<TransientRegisteredObserver>(observer) && verify_cast<TransientRegisteredObserver>(observer).source().ptr() == registered_observer;
  84. });
  85. }
  86. }
  87. // 2. Set registered’s options to options.
  88. registered_observer->set_options(options);
  89. break;
  90. }
  91. }
  92. // 8. Otherwise:
  93. if (!updated_existing_observer) {
  94. // 1. Append a new registered observer whose observer is this and options is options to target’s registered observer list.
  95. auto new_registered_observer = RegisteredObserver::create(*this, options);
  96. target.add_registered_observer(new_registered_observer);
  97. // 2. Append target to this’s node list.
  98. m_node_list.append(target.make_weak_ptr<Node>());
  99. }
  100. return {};
  101. }
  102. // https://dom.spec.whatwg.org/#dom-mutationobserver-disconnect
  103. void MutationObserver::disconnect()
  104. {
  105. // 1. For each node of this’s node list, remove any registered observer from node’s registered observer list for which this is the observer.
  106. for (auto& node : m_node_list) {
  107. // FIXME: Is this correct?
  108. if (node.is_null())
  109. continue;
  110. if (node->registered_observer_list()) {
  111. node->registered_observer_list()->remove_all_matching([this](RegisteredObserver& registered_observer) {
  112. return registered_observer.observer().ptr() == this;
  113. });
  114. }
  115. }
  116. // 2. Empty this’s record queue.
  117. m_record_queue.clear();
  118. }
  119. // https://dom.spec.whatwg.org/#dom-mutationobserver-takerecords
  120. Vector<JS::Handle<MutationRecord>> MutationObserver::take_records()
  121. {
  122. // 1. Let records be a clone of this’s record queue.
  123. Vector<JS::Handle<MutationRecord>> records;
  124. for (auto& record : m_record_queue)
  125. records.append(*record);
  126. // 2. Empty this’s record queue.
  127. m_record_queue.clear();
  128. // 3. Return records.
  129. return records;
  130. }
  131. JS::NonnullGCPtr<RegisteredObserver> RegisteredObserver::create(MutationObserver& observer, MutationObserverInit const& options)
  132. {
  133. return observer.heap().allocate_without_realm<RegisteredObserver>(observer, options);
  134. }
  135. RegisteredObserver::RegisteredObserver(MutationObserver& observer, MutationObserverInit const& options)
  136. : m_observer(observer)
  137. , m_options(options)
  138. {
  139. }
  140. RegisteredObserver::~RegisteredObserver() = default;
  141. void RegisteredObserver::visit_edges(Cell::Visitor& visitor)
  142. {
  143. Base::visit_edges(visitor);
  144. visitor.visit(m_observer);
  145. }
  146. JS::NonnullGCPtr<TransientRegisteredObserver> TransientRegisteredObserver::create(MutationObserver& observer, MutationObserverInit const& options, RegisteredObserver& source)
  147. {
  148. return observer.heap().allocate_without_realm<TransientRegisteredObserver>(observer, options, source);
  149. }
  150. TransientRegisteredObserver::TransientRegisteredObserver(MutationObserver& observer, MutationObserverInit const& options, RegisteredObserver& source)
  151. : RegisteredObserver(observer, options)
  152. , m_source(source)
  153. {
  154. }
  155. TransientRegisteredObserver::~TransientRegisteredObserver() = default;
  156. void TransientRegisteredObserver::visit_edges(Cell::Visitor& visitor)
  157. {
  158. Base::visit_edges(visitor);
  159. visitor.visit(m_source);
  160. }
  161. }