Serenity Operating System
at master 73 lines 2.3 kB view raw
1/* 2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/HashTable.h> 8#include <LibWeb/DOM/QualifiedName.h> 9 10namespace Web::DOM { 11 12struct ImplTraits : public Traits<QualifiedName::Impl*> { 13 static unsigned hash(QualifiedName::Impl* impl) 14 { 15 return pair_int_hash(impl->local_name.hash(), pair_int_hash(impl->prefix.hash(), impl->namespace_.hash())); 16 } 17 18 static bool equals(QualifiedName::Impl* a, QualifiedName::Impl* b) 19 { 20 return a->local_name == b->local_name 21 && a->prefix == b->prefix 22 && a->namespace_ == b->namespace_; 23 } 24}; 25 26static HashTable<QualifiedName::Impl*, ImplTraits> impls; 27 28static NonnullRefPtr<QualifiedName::Impl> ensure_impl(DeprecatedFlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_) 29{ 30 auto hash = pair_int_hash(local_name.hash(), pair_int_hash(prefix.hash(), namespace_.hash())); 31 auto it = impls.find(hash, [&](QualifiedName::Impl* entry) { 32 return entry->local_name == local_name 33 && entry->prefix == prefix 34 && entry->namespace_ == namespace_; 35 }); 36 if (it != impls.end()) 37 return *(*it); 38 return adopt_ref(*new QualifiedName::Impl(local_name, prefix, namespace_)); 39} 40 41QualifiedName::QualifiedName(DeprecatedFlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_) 42 : m_impl(ensure_impl(local_name, prefix, namespace_)) 43{ 44} 45 46QualifiedName::Impl::Impl(DeprecatedFlyString const& a_local_name, DeprecatedFlyString const& a_prefix, DeprecatedFlyString const& a_namespace) 47 : local_name(a_local_name) 48 , prefix(a_prefix) 49 , namespace_(a_namespace) 50{ 51 impls.set(this); 52 make_internal_string(); 53} 54 55QualifiedName::Impl::~Impl() 56{ 57 impls.remove(this); 58} 59 60// https://dom.spec.whatwg.org/#concept-attribute-qualified-name 61// https://dom.spec.whatwg.org/#concept-element-qualified-name 62void QualifiedName::Impl::make_internal_string() 63{ 64 // This is possible to do according to the spec: "User agents could have this as an internal slot as an optimization." 65 if (prefix.is_null()) { 66 as_string = local_name; 67 return; 68 } 69 70 as_string = DeprecatedString::formatted("{}:{}", prefix, local_name); 71} 72 73}