HTMLCollection.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/Element.h>
  9. #include <LibWeb/DOM/HTMLCollection.h>
  10. #include <LibWeb/DOM/ParentNode.h>
  11. #include <LibWeb/Namespace.h>
  12. namespace Web::DOM {
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLCollection>> HTMLCollection::create(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  14. {
  15. return MUST_OR_THROW_OOM(root.heap().allocate<HTMLCollection>(root.realm(), root, scope, move(filter)));
  16. }
  17. HTMLCollection::HTMLCollection(ParentNode& root, Scope scope, Function<bool(Element const&)> filter)
  18. : LegacyPlatformObject(root.realm())
  19. , m_root(root)
  20. , m_filter(move(filter))
  21. , m_scope(scope)
  22. {
  23. }
  24. HTMLCollection::~HTMLCollection() = default;
  25. JS::ThrowCompletionOr<void> HTMLCollection::initialize(JS::Realm& realm)
  26. {
  27. MUST_OR_THROW_OOM(Base::initialize(realm));
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLCollectionPrototype>(realm, "HTMLCollection"));
  29. return {};
  30. }
  31. void HTMLCollection::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_root.ptr());
  35. }
  36. JS::MarkedVector<Element*> HTMLCollection::collect_matching_elements() const
  37. {
  38. JS::MarkedVector<Element*> elements(m_root->heap());
  39. if (m_scope == Scope::Descendants) {
  40. m_root->for_each_in_subtree_of_type<Element>([&](auto& element) {
  41. if (m_filter(element))
  42. elements.append(const_cast<Element*>(&element));
  43. return IterationDecision::Continue;
  44. });
  45. } else {
  46. m_root->for_each_child_of_type<Element>([&](auto& element) {
  47. if (m_filter(element))
  48. elements.append(const_cast<Element*>(&element));
  49. return IterationDecision::Continue;
  50. });
  51. }
  52. return elements;
  53. }
  54. // https://dom.spec.whatwg.org/#dom-htmlcollection-length
  55. size_t HTMLCollection::length()
  56. {
  57. // The length getter steps are to return the number of nodes represented by the collection.
  58. return collect_matching_elements().size();
  59. }
  60. // https://dom.spec.whatwg.org/#dom-htmlcollection-item
  61. Element* HTMLCollection::item(size_t index) const
  62. {
  63. // 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.
  64. auto elements = collect_matching_elements();
  65. if (index >= elements.size())
  66. return nullptr;
  67. return elements[index];
  68. }
  69. // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
  70. Element* HTMLCollection::named_item(DeprecatedFlyString const& name) const
  71. {
  72. // 1. If key is the empty string, return null.
  73. if (name.is_empty())
  74. return nullptr;
  75. auto elements = collect_matching_elements();
  76. // 2. Return the first element in the collection for which at least one of the following is true:
  77. // - it has an ID which is key;
  78. if (auto it = elements.find_if([&](auto& entry) { return entry->attribute(HTML::AttributeNames::id) == name; }); it != elements.end())
  79. return *it;
  80. // - it is in the HTML namespace and has a name attribute whose value is key;
  81. if (auto it = elements.find_if([&](auto& entry) { return entry->namespace_() == Namespace::HTML && entry->name() == name; }); it != elements.end())
  82. return *it;
  83. // or null if there is no such element.
  84. return nullptr;
  85. }
  86. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
  87. Vector<DeprecatedString> HTMLCollection::supported_property_names() const
  88. {
  89. // 1. Let result be an empty list.
  90. Vector<DeprecatedString> result;
  91. // 2. For each element represented by the collection, in tree order:
  92. auto elements = collect_matching_elements();
  93. for (auto& element : elements) {
  94. // 1. If element has an ID which is not in result, append element’s ID to result.
  95. if (element->has_attribute(HTML::AttributeNames::id)) {
  96. auto id = element->attribute(HTML::AttributeNames::id);
  97. if (!result.contains_slow(id))
  98. result.append(id);
  99. }
  100. // 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.
  101. if (element->namespace_() == Namespace::HTML && element->has_attribute(HTML::AttributeNames::name)) {
  102. auto name = element->attribute(HTML::AttributeNames::name);
  103. if (!name.is_empty() && !result.contains_slow(name))
  104. result.append(name);
  105. }
  106. }
  107. // 3. Return result.
  108. return result;
  109. }
  110. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A1
  111. bool HTMLCollection::is_supported_property_index(u32 index) const
  112. {
  113. // 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.
  114. // If there are no such elements, then there are no supported property indices.
  115. auto elements = collect_matching_elements();
  116. if (elements.is_empty())
  117. return false;
  118. return index < elements.size();
  119. }
  120. WebIDL::ExceptionOr<JS::Value> HTMLCollection::item_value(size_t index) const
  121. {
  122. auto* element = item(index);
  123. if (!element)
  124. return JS::js_undefined();
  125. return const_cast<Element*>(element);
  126. }
  127. WebIDL::ExceptionOr<JS::Value> HTMLCollection::named_item_value(DeprecatedFlyString const& index) const
  128. {
  129. auto* element = named_item(index);
  130. if (!element)
  131. return JS::js_undefined();
  132. return const_cast<Element*>(element);
  133. }
  134. }