summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h
blob: e6bf62ebfa224ea93442a794d05882c9a219c85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/WeakPtr.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/Forward.h>

namespace Web::DOM {

// https://dom.spec.whatwg.org/#interface-namednodemap
class NamedNodeMap final
    : public RefCounted<NamedNodeMap>
    , public Bindings::Wrappable {

public:
    using WrapperType = Bindings::NamedNodeMapWrapper;

    static NonnullRefPtr<NamedNodeMap> create(Element const& associated_element);
    ~NamedNodeMap() = default;

    bool is_supported_property_index(u32 index) const;
    Vector<String> supported_property_names() const;

    size_t length() const { return m_attributes.size(); }
    bool is_empty() const { return m_attributes.is_empty(); }

    // Methods defined by the spec for JavaScript:
    Attribute const* item(u32 index) const;
    Attribute const* get_named_item(StringView qualified_name) const;
    ExceptionOr<Attribute const*> set_named_item(Attribute& attribute);
    ExceptionOr<Attribute const*> remove_named_item(StringView qualified_name);

    // Methods defined by the spec for internal use:
    Attribute* get_attribute(StringView qualified_name, size_t* item_index = nullptr);
    Attribute const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const;
    ExceptionOr<Attribute const*> set_attribute(Attribute& attribute);
    void replace_attribute(Attribute& old_attribute, Attribute& new_attribute, size_t old_attribute_index);
    void append_attribute(Attribute& attribute);
    Attribute const* remove_attribute(StringView qualified_name);

private:
    explicit NamedNodeMap(Element const& associated_element);

    WeakPtr<Element> m_associated_element;
    NonnullRefPtrVector<Attribute> m_attributes;
};

}

namespace Web::Bindings {

NamedNodeMapWrapper* wrap(JS::GlobalObject&, DOM::NamedNodeMap&);

}