Skip to content

Truly independent web browser

2.0 KB
Raw Blame
/*
 * Copyright (c) 2024, Shannon Booth <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/Utf16FlyString.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/Forward.h>

namespace Web::HTML {

class HTMLAllCollection : public Bindings::GCAllocatedWrappable {
    WEB_WRAPPABLE(HTMLAllCollection, Bindings::GCAllocatedWrappable);
    GC_DECLARE_ALLOCATOR(HTMLAllCollection);

public:
    enum class Scope {
        Children,
        Descendants,
    };
    [[nodiscard]] static GC::Ref<HTMLAllCollection> create(DOM::ParentNode& root, Scope, ESCAPING Function<bool(DOM::Element const&)> filter);

    virtual ~HTMLAllCollection() override;

    size_t length() const;
    GC::Ptr<DOM::Element> item(size_t index) const;
    Variant<GC::Ref<DOM::HTMLCollection>, GC::Ref<DOM::Element>, Empty> item(Optional<Utf16String> const& name_or_index) const;
    Variant<GC::Ref<DOM::HTMLCollection>, GC::Ref<DOM::Element>, Empty> named_item(Utf16View name) const;

    GC::RootVector<GC::Ref<DOM::Element>> collect_matching_elements() const;

    virtual Vector<Utf16FlyString> supported_property_names() const override;

protected:
    HTMLAllCollection(DOM::ParentNode& root, Scope, ESCAPING Function<bool(DOM::Element const&)> filter);

private:
    Variant<GC::Ref<DOM::HTMLCollection>, GC::Ref<DOM::Element>, Empty> get_the_all_named_elements(Utf16View name) const;
    GC::Ptr<DOM::Element> get_the_all_indexed_element(u32 index) const;
    Variant<GC::Ref<DOM::HTMLCollection>, GC::Ref<DOM::Element>, Empty> get_the_all_indexed_or_named_elements(Utf16View name_or_index) const;
    Variant<GC::Ref<DOM::HTMLCollection>, GC::Ref<DOM::Element>, Empty> get_the_all_indexed_or_named_elements(JS::PropertyKey const& name_or_index) const;

    virtual void visit_edges(GC::Cell::Visitor&) override;

    GC::Ref<DOM::ParentNode> m_root;
    Function<bool(DOM::Element const&)> m_filter;
    Scope m_scope { Scope::Descendants };
};

}