HTMLCollection.cpp 5.3 KB

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