HTMLCollection.cpp 5.6 KB

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