HTMLCollection.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. JS::MarkedVector<Element*> HTMLCollection::collect_matching_elements() const
  45. {
  46. if (m_cached_dom_tree_version != root()->document().dom_tree_version()) {
  47. m_cached_elements.clear();
  48. if (m_scope == Scope::Descendants) {
  49. m_root->for_each_in_subtree_of_type<Element>([&](auto& element) {
  50. if (m_filter(element))
  51. m_cached_elements.append(element);
  52. return IterationDecision::Continue;
  53. });
  54. } else {
  55. m_root->for_each_child_of_type<Element>([&](auto& element) {
  56. if (m_filter(element))
  57. m_cached_elements.append(element);
  58. return IterationDecision::Continue;
  59. });
  60. }
  61. m_cached_dom_tree_version = root()->document().dom_tree_version();
  62. }
  63. JS::MarkedVector<Element*> elements(heap());
  64. for (auto& element : m_cached_elements)
  65. elements.append(element);
  66. return elements;
  67. }
  68. // https://dom.spec.whatwg.org/#dom-htmlcollection-length
  69. size_t HTMLCollection::length() const
  70. {
  71. // The length getter steps are to return the number of nodes represented by the collection.
  72. return collect_matching_elements().size();
  73. }
  74. // https://dom.spec.whatwg.org/#dom-htmlcollection-item
  75. Element* HTMLCollection::item(size_t index) const
  76. {
  77. // 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.
  78. auto elements = collect_matching_elements();
  79. if (index >= elements.size())
  80. return nullptr;
  81. return elements[index];
  82. }
  83. // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
  84. Element* HTMLCollection::named_item(FlyString const& name) const
  85. {
  86. // 1. If key is the empty string, return null.
  87. if (name.is_empty())
  88. return nullptr;
  89. auto elements = collect_matching_elements();
  90. // 2. Return the first element in the collection for which at least one of the following is true:
  91. // - it has an ID which is key;
  92. if (auto it = elements.find_if([&](auto& entry) { return entry->id().has_value() && entry->id().value() == name; }); it != elements.end())
  93. return *it;
  94. // - it is in the HTML namespace and has a name attribute whose value is key;
  95. if (auto it = elements.find_if([&](auto& entry) { return entry->namespace_uri() == Namespace::HTML && entry->name() == name; }); it != elements.end())
  96. return *it;
  97. // or null if there is no such element.
  98. return nullptr;
  99. }
  100. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
  101. Vector<FlyString> HTMLCollection::supported_property_names() const
  102. {
  103. // 1. Let result be an empty list.
  104. Vector<FlyString> result;
  105. // 2. For each element represented by the collection, in tree order:
  106. auto elements = collect_matching_elements();
  107. for (auto& element : elements) {
  108. // 1. If element has an ID which is not in result, append element’s ID to result.
  109. if (auto const& id = element->id(); id.has_value()) {
  110. if (!result.contains_slow(id.value()))
  111. result.append(id.value());
  112. }
  113. // 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.
  114. if (element->namespace_uri() == Namespace::HTML && element->name().has_value()) {
  115. auto name = element->name().value();
  116. if (!name.is_empty() && !result.contains_slow(name))
  117. result.append(move(name));
  118. }
  119. }
  120. // 3. Return result.
  121. return result;
  122. }
  123. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A1
  124. bool HTMLCollection::is_supported_property_index(u32 index) const
  125. {
  126. // 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.
  127. // If there are no such elements, then there are no supported property indices.
  128. auto elements = collect_matching_elements();
  129. if (elements.is_empty())
  130. return false;
  131. return index < elements.size();
  132. }
  133. WebIDL::ExceptionOr<JS::Value> HTMLCollection::item_value(size_t index) const
  134. {
  135. auto* element = item(index);
  136. if (!element)
  137. return JS::js_undefined();
  138. return const_cast<Element*>(element);
  139. }
  140. WebIDL::ExceptionOr<JS::Value> HTMLCollection::named_item_value(FlyString const& index) const
  141. {
  142. auto* element = named_item(index);
  143. if (!element)
  144. return JS::js_undefined();
  145. return const_cast<Element*>(element);
  146. }
  147. }