HTMLCollection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2021, 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/DOM/Element.h>
  8. #include <LibWeb/DOM/HTMLCollection.h>
  9. #include <LibWeb/DOM/ParentNode.h>
  10. #include <LibWeb/Namespace.h>
  11. namespace Web::DOM {
  12. HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
  13. : m_root(root)
  14. , m_filter(move(filter))
  15. {
  16. }
  17. HTMLCollection::~HTMLCollection() = default;
  18. Vector<NonnullRefPtr<Element>> HTMLCollection::collect_matching_elements() const
  19. {
  20. Vector<NonnullRefPtr<Element>> elements;
  21. m_root->for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  22. if (m_filter(element))
  23. elements.append(element);
  24. return IterationDecision::Continue;
  25. });
  26. return elements;
  27. }
  28. // https://dom.spec.whatwg.org/#dom-htmlcollection-length
  29. size_t HTMLCollection::length()
  30. {
  31. // The length getter steps are to return the number of nodes represented by the collection.
  32. return collect_matching_elements().size();
  33. }
  34. // https://dom.spec.whatwg.org/#dom-htmlcollection-item
  35. Element* HTMLCollection::item(size_t index) const
  36. {
  37. // 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.
  38. auto elements = collect_matching_elements();
  39. if (index >= elements.size())
  40. return nullptr;
  41. return elements[index];
  42. }
  43. // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
  44. Element* HTMLCollection::named_item(FlyString const& name) const
  45. {
  46. // 1. If key is the empty string, return null.
  47. if (name.is_empty())
  48. return nullptr;
  49. auto elements = collect_matching_elements();
  50. // 2. Return the first element in the collection for which at least one of the following is true:
  51. // - it has an ID which is key;
  52. if (auto it = elements.find_if([&](auto& entry) { return entry->attribute(HTML::AttributeNames::id) == name; }); it != elements.end())
  53. return *it;
  54. // - it is in the HTML namespace and has a name attribute whose value is key;
  55. if (auto it = elements.find_if([&](auto& entry) { return entry->namespace_() == Namespace::HTML && entry->name() == name; }); it != elements.end())
  56. return *it;
  57. // or null if there is no such element.
  58. return nullptr;
  59. }
  60. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
  61. Vector<String> HTMLCollection::supported_property_names() const
  62. {
  63. // 1. Let result be an empty list.
  64. Vector<String> result;
  65. // 2. For each element represented by the collection, in tree order:
  66. auto elements = collect_matching_elements();
  67. for (auto& element : elements) {
  68. // 1. If element has an ID which is not in result, append element’s ID to result.
  69. if (element->has_attribute(HTML::AttributeNames::id)) {
  70. auto id = element->attribute(HTML::AttributeNames::id);
  71. if (!result.contains_slow(id))
  72. result.append(id);
  73. }
  74. // 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.
  75. if (element->namespace_() == Namespace::HTML && element->has_attribute(HTML::AttributeNames::name)) {
  76. auto name = element->attribute(HTML::AttributeNames::name);
  77. if (!name.is_empty() && !result.contains_slow(name))
  78. result.append(name);
  79. }
  80. }
  81. // 3. Return result.
  82. return result;
  83. }
  84. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A1
  85. bool HTMLCollection::is_supported_property_index(u32 index) const
  86. {
  87. // 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.
  88. // If there are no such elements, then there are no supported property indices.
  89. auto elements = collect_matching_elements();
  90. if (elements.is_empty())
  91. return false;
  92. return index < elements.size();
  93. }
  94. }