MutationObserver.cpp 7.8 KB

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