MutationObserver.cpp 8.1 KB

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