HTMLCollection.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/HTMLCollectionPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Element.h>
  11. #include <LibWeb/DOM/HTMLCollection.h>
  12. #include <LibWeb/DOM/ParentNode.h>
  13. #include <LibWeb/Namespace.h>
  14. namespace Web::DOM {
  15. GC_DEFINE_ALLOCATOR(HTMLCollection);
  16. GC::Ref<HTMLCollection> HTMLCollection::create(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  17. {
  18. return root.realm().create<HTMLCollection>(root, scope, move(filter));
  19. }
  20. HTMLCollection::HTMLCollection(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  21. : PlatformObject(root.realm())
  22. , m_root(root)
  23. , m_filter(move(filter))
  24. , m_scope(scope)
  25. {
  26. m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
  27. .supports_indexed_properties = true,
  28. .supports_named_properties = true,
  29. .has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
  30. };
  31. }
  32. HTMLCollection::~HTMLCollection() = default;
  33. void HTMLCollection::initialize(JS::Realm& realm)
  34. {
  35. Base::initialize(realm);
  36. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLCollection);
  37. }
  38. void HTMLCollection::visit_edges(Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_root);
  42. visitor.visit(m_cached_elements);
  43. if (m_cached_name_to_element_mappings)
  44. visitor.visit(*m_cached_name_to_element_mappings);
  45. }
  46. void HTMLCollection::update_name_to_element_mappings_if_needed() const
  47. {
  48. update_cache_if_needed();
  49. if (m_cached_name_to_element_mappings)
  50. return;
  51. m_cached_name_to_element_mappings = make<OrderedHashMap<FlyString, GC::Ref<Element>>>();
  52. for (auto const& element : m_cached_elements) {
  53. // 1. If element has an ID which is not in result, append element’s ID to result.
  54. if (auto const& id = element->id(); id.has_value()) {
  55. if (!id.value().is_empty() && !m_cached_name_to_element_mappings->contains(id.value()))
  56. m_cached_name_to_element_mappings->set(id.value(), element);
  57. }
  58. // 2. If element is in the HTML namespace and has a name attribute whose value is neither the empty string nor is in result, append element’s name attribute value to result.
  59. if (element->namespace_uri() == Namespace::HTML && element->name().has_value()) {
  60. auto element_name = element->name().value();
  61. if (!element_name.is_empty() && !m_cached_name_to_element_mappings->contains(element_name))
  62. m_cached_name_to_element_mappings->set(move(element_name), element);
  63. }
  64. }
  65. }
  66. void HTMLCollection::update_cache_if_needed() const
  67. {
  68. // Nothing to do, the DOM hasn't updated since we last built the cache.
  69. if (m_cached_dom_tree_version == root()->document().dom_tree_version())
  70. return;
  71. m_cached_elements.clear();
  72. m_cached_name_to_element_mappings = nullptr;
  73. if (m_scope == Scope::Descendants) {
  74. m_root->for_each_in_subtree_of_type<Element>([&](auto& element) {
  75. if (m_filter(element))
  76. m_cached_elements.append(element);
  77. return TraversalDecision::Continue;
  78. });
  79. } else {
  80. m_root->for_each_child_of_type<Element>([&](auto& element) {
  81. if (m_filter(element))
  82. m_cached_elements.append(element);
  83. return IterationDecision::Continue;
  84. });
  85. }
  86. m_cached_dom_tree_version = root()->document().dom_tree_version();
  87. }
  88. GC::RootVector<GC::Ref<Element>> HTMLCollection::collect_matching_elements() const
  89. {
  90. update_cache_if_needed();
  91. GC::RootVector<GC::Ref<Element>> elements(heap());
  92. for (auto& element : m_cached_elements)
  93. elements.append(element);
  94. return elements;
  95. }
  96. // https://dom.spec.whatwg.org/#dom-htmlcollection-length
  97. size_t HTMLCollection::length() const
  98. {
  99. // The length getter steps are to return the number of nodes represented by the collection.
  100. update_cache_if_needed();
  101. return m_cached_elements.size();
  102. }
  103. // https://dom.spec.whatwg.org/#dom-htmlcollection-item
  104. Element* HTMLCollection::item(size_t index) const
  105. {
  106. // The item(index) method steps are to return the indexth element in the collection. If there is no indexth element in the collection, then the method must return null.
  107. update_cache_if_needed();
  108. if (index >= m_cached_elements.size())
  109. return nullptr;
  110. return m_cached_elements[index];
  111. }
  112. // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
  113. Element* HTMLCollection::named_item(FlyString const& key) const
  114. {
  115. // 1. If key is the empty string, return null.
  116. if (key.is_empty())
  117. return nullptr;
  118. update_name_to_element_mappings_if_needed();
  119. if (auto it = m_cached_name_to_element_mappings->get(key); it.has_value())
  120. return it.value();
  121. return nullptr;
  122. }
  123. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
  124. bool HTMLCollection::is_supported_property_name(FlyString const& name) const
  125. {
  126. update_name_to_element_mappings_if_needed();
  127. return m_cached_name_to_element_mappings->contains(name);
  128. }
  129. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
  130. Vector<FlyString> HTMLCollection::supported_property_names() const
  131. {
  132. // 1. Let result be an empty list.
  133. Vector<FlyString> result;
  134. // 2. For each element represented by the collection, in tree order:
  135. update_name_to_element_mappings_if_needed();
  136. for (auto const& it : *m_cached_name_to_element_mappings) {
  137. result.append(it.key);
  138. }
  139. // 3. Return result.
  140. return result;
  141. }
  142. Optional<JS::Value> HTMLCollection::item_value(size_t index) const
  143. {
  144. auto* element = item(index);
  145. if (!element)
  146. return {};
  147. return element;
  148. }
  149. JS::Value HTMLCollection::named_item_value(FlyString const& name) const
  150. {
  151. auto* element = named_item(name);
  152. if (!element)
  153. return JS::js_undefined();
  154. return element;
  155. }
  156. }