summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/QualifiedName.cpp
blob: 28fb86d74a9540b7170feb5cb552240a8669df32 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/HashTable.h>
#include <LibWeb/DOM/QualifiedName.h>

namespace Web::DOM {

struct ImplTraits : public Traits<QualifiedName::Impl*> {
    static unsigned hash(QualifiedName::Impl* impl)
    {
        return pair_int_hash(impl->local_name.hash(), pair_int_hash(impl->prefix.hash(), impl->namespace_.hash()));
    }

    static bool equals(QualifiedName::Impl* a, QualifiedName::Impl* b)
    {
        return a->local_name == b->local_name
            && a->prefix == b->prefix
            && a->namespace_ == b->namespace_;
    }
};

static HashTable<QualifiedName::Impl*, ImplTraits> impls;

static NonnullRefPtr<QualifiedName::Impl> ensure_impl(DeprecatedFlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_)
{
    auto hash = pair_int_hash(local_name.hash(), pair_int_hash(prefix.hash(), namespace_.hash()));
    auto it = impls.find(hash, [&](QualifiedName::Impl* entry) {
        return entry->local_name == local_name
            && entry->prefix == prefix
            && entry->namespace_ == namespace_;
    });
    if (it != impls.end())
        return *(*it);
    return adopt_ref(*new QualifiedName::Impl(local_name, prefix, namespace_));
}

QualifiedName::QualifiedName(DeprecatedFlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_)
    : m_impl(ensure_impl(local_name, prefix, namespace_))
{
}

QualifiedName::Impl::Impl(DeprecatedFlyString const& a_local_name, DeprecatedFlyString const& a_prefix, DeprecatedFlyString const& a_namespace)
    : local_name(a_local_name)
    , prefix(a_prefix)
    , namespace_(a_namespace)
{
    impls.set(this);
    make_internal_string();
}

QualifiedName::Impl::~Impl()
{
    impls.remove(this);
}

// https://dom.spec.whatwg.org/#concept-attribute-qualified-name
// https://dom.spec.whatwg.org/#concept-element-qualified-name
void QualifiedName::Impl::make_internal_string()
{
    // This is possible to do according to the spec: "User agents could have this as an internal slot as an optimization."
    if (prefix.is_null()) {
        as_string = local_name;
        return;
    }

    as_string = DeprecatedString::formatted("{}:{}", prefix, local_name);
}

void QualifiedName::set_prefix(DeprecatedFlyString const& value)
{
    m_impl->prefix = value;
}

}