Skip to content

Truly independent web browser

3.2 KB
Raw Blame
/*
 * Copyright (c) 2021, Tim Flynn <[email protected]>
 * Copyright (c) 2022, Andreas Kling <[email protected]>
 * Copyright (c) 2022, Alexander Narsudinov <[email protected]>
 * Copyright (c) 2023, Luke Wilde <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Badge.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebIDL/ExceptionOr.h>

namespace Web::DOM {

// https://dom.spec.whatwg.org/#interface-namednodemap
class WEB_API NamedNodeMap : public Bindings::GCAllocatedWrappable {
    WEB_WRAPPABLE(NamedNodeMap, Bindings::GCAllocatedWrappable);
    GC_DECLARE_ALLOCATOR(NamedNodeMap);

public:
    [[nodiscard]] static GC::Ref<NamedNodeMap> create(Element&);
    ~NamedNodeMap() = default;

    virtual Vector<Utf16FlyString> supported_property_names() const override;
    virtual bool is_supported_property_name(Utf16FlyString const&) const override;

    size_t length() const;
    bool is_empty() const { return length() == 0; }

    // Methods defined by the spec for JavaScript:
    GC::Ptr<Attr> item(u32 index) const;
    GC::Ptr<Attr> get_named_item(Utf16FlyString const& qualified_name) const;
    GC::Ptr<Attr> get_named_item_ns(Optional<Utf16FlyString> const& namespace_, Utf16FlyString const& local_name) const;
    WebIDL::ExceptionOr<GC::Ptr<Attr>> set_named_item(Attr& attribute);
    WebIDL::ExceptionOr<GC::Ptr<Attr>> set_named_item_ns(Attr& attribute);
    WebIDL::ExceptionOr<GC::Ref<Attr>> remove_named_item(Utf16FlyString const& qualified_name);
    WebIDL::ExceptionOr<GC::Ref<Attr>> remove_named_item_ns(Optional<Utf16FlyString> const& namespace_, Utf16FlyString const& local_name);

    // Methods defined by the spec for internal use:
    GC::Ptr<Attr> get_attribute(Utf16FlyString const& qualified_name, size_t* item_index = nullptr) const;
    WebIDL::ExceptionOr<GC::Ptr<Attr>> set_attribute(GC::Ref<Attr> attribute);
    void replace_attribute(GC::Ref<Attr> old_attribute, GC::Ref<Attr> new_attribute, size_t old_attribute_index);
    void append_attribute(GC::Ref<Attr> attribute);
    void detach_attribute_node(QualifiedName const&, Utf16String value);
    void move_attribute_nodes_to_document(Badge<Element>, Document&);

    GC::Ptr<Attr> get_attribute_ns(Optional<Utf16FlyString> const& namespace_, Utf16FlyString const& local_name, size_t* item_index = nullptr) const;

    GC::Ptr<Attr> remove_attribute(Utf16FlyString const& qualified_name);
    GC::Ptr<Attr> remove_attribute_ns(Optional<Utf16FlyString> const& namespace_, Utf16FlyString const& local_name);

    WebIDL::ExceptionOr<GC::Ref<Attr>> remove_attribute_node(GC::Ref<Attr>);

private:
    explicit NamedNodeMap(Element&);

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

    Element& associated_element() { return *m_element; }
    Element const& associated_element() const { return *m_element; }

    void remove_attribute_at_index(size_t attribute_index);
    GC::Ptr<Attr> find_attribute_node(QualifiedName const&) const;
    GC::Ref<Attr> ensure_attribute_node(QualifiedName const&) const;

    GC::NonnullMember<DOM::Element> m_element;
    mutable Vector<GC::Ref<Attr>> m_attribute_nodes;
};

}