HTMLCollection.cpp 6.0 KB

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