MutationObserver.cpp 7.9 KB

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