2021-04-22 16:11:20 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
|
2023-02-27 21:05:39 -03:00
|
|
|
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
2021-04-22 16:11:20 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/Ptr.h>
|
2024-01-09 20:05:03 -03:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2021-04-22 16:11:20 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
// NOTE: HTMLCollection is in the DOM namespace because it's part of the DOM specification.
|
|
|
|
|
|
|
|
|
|
// This class implements a live, filtered view of a DOM subtree.
|
|
|
|
|
// When constructing an HTMLCollection, you provide a root node + a filter.
|
|
|
|
|
// The filter is a simple Function object that answers the question
|
|
|
|
|
// "is this Element part of the collection?"
|
|
|
|
|
|
2024-01-09 20:05:03 -03:00
|
|
|
class HTMLCollection : public Bindings::PlatformObject {
|
|
|
|
|
WEB_PLATFORM_OBJECT(HTMLCollection, Bindings::PlatformObject);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(HTMLCollection);
|
2021-04-22 16:11:20 -03:00
|
|
|
|
|
|
|
|
public:
|
2023-05-23 06:25:07 -03:00
|
|
|
enum class Scope {
|
|
|
|
|
Children,
|
|
|
|
|
Descendants,
|
|
|
|
|
};
|
2025-10-26 01:28:48 -03:00
|
|
|
[[nodiscard]] static GC::Ref<HTMLCollection> create(ParentNode& root, Scope, ESCAPING Function<bool(Element const&)> filter, ESCAPING Function<bool(Element const&, Element const&)> sort = nullptr);
|
2021-04-22 16:11:20 -03:00
|
|
|
|
2022-09-01 15:50:16 -03:00
|
|
|
virtual ~HTMLCollection() override;
|
2021-04-22 16:11:20 -03:00
|
|
|
|
2023-08-18 22:41:57 -03:00
|
|
|
size_t length() const;
|
2021-09-26 11:14:37 -03:00
|
|
|
Element* item(size_t index) const;
|
2024-04-26 07:27:27 -03:00
|
|
|
Element* named_item(FlyString const& key) const;
|
2021-04-22 16:11:20 -03:00
|
|
|
|
2024-12-26 10:32:52 -03:00
|
|
|
GC::RootVector<GC::Ref<Element>> collect_matching_elements() const;
|
2021-09-26 11:14:37 -03:00
|
|
|
|
2024-07-25 03:15:51 -03:00
|
|
|
virtual Optional<JS::Value> item_value(size_t index) const override;
|
2024-07-25 02:36:10 -03:00
|
|
|
virtual JS::Value named_item_value(FlyString const& name) const override;
|
2023-12-24 16:59:00 -03:00
|
|
|
virtual Vector<FlyString> supported_property_names() const override;
|
2024-07-25 02:49:41 -03:00
|
|
|
virtual bool is_supported_property_name(FlyString const&) const override;
|
2021-04-22 16:11:20 -03:00
|
|
|
|
|
|
|
|
protected:
|
2025-10-26 01:28:48 -03:00
|
|
|
HTMLCollection(ParentNode& root, Scope, ESCAPING Function<bool(Element const&)> filter, ESCAPING Function<bool(Element const&, Element const&)> sort = nullptr);
|
2021-04-22 16:11:20 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-01-10 08:56:59 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<ParentNode> root() { return *m_root; }
|
|
|
|
|
GC::Ref<ParentNode const> root() const { return *m_root; }
|
2022-03-21 21:01:47 -03:00
|
|
|
|
2021-04-22 16:11:20 -03:00
|
|
|
private:
|
2022-09-01 15:50:16 -03:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
2024-04-01 10:08:22 -03:00
|
|
|
void update_cache_if_needed() const;
|
2024-07-25 02:49:41 -03:00
|
|
|
void update_name_to_element_mappings_if_needed() const;
|
2024-04-01 10:08:22 -03:00
|
|
|
|
2024-03-19 11:41:02 -03:00
|
|
|
mutable u64 m_cached_dom_tree_version { 0 };
|
2025-12-16 15:31:23 -03:00
|
|
|
mutable Vector<GC::Weak<Element>> m_cached_elements;
|
|
|
|
|
mutable OwnPtr<OrderedHashMap<FlyString, GC::Weak<Element>>> m_cached_name_to_element_mappings;
|
2024-03-19 11:41:02 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<ParentNode> m_root;
|
2021-04-22 16:11:20 -03:00
|
|
|
Function<bool(Element const&)> m_filter;
|
2025-10-26 01:28:48 -03:00
|
|
|
Function<bool(Element const&, Element const&)> m_sort;
|
2023-05-23 06:25:07 -03:00
|
|
|
|
|
|
|
|
Scope m_scope { Scope::Descendants };
|
2021-04-22 16:11:20 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|