HTMLCollection.cpp 5.0 KB

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