Truly independent web browser
/*
* Copyright (c) 2023, Luke Wilde <[email protected]>
* Copyright (c) 2025, Sam Atkins <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGC/WeakHashSet.h>
#include <LibWeb/Bindings/CustomElementRegistry.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
namespace Web::HTML {
class CustomElementRegistry;
}
namespace Web::HTML {
using ElementDefinitionOptions = Bindings::ElementDefinitionOptions;
// https://html.spec.whatwg.org/multipage/custom-elements.html#customelementregistry
class WEB_API CustomElementRegistry : public Bindings::GCAllocatedWrappable {
WEB_WRAPPABLE(CustomElementRegistry, Bindings::GCAllocatedWrappable);
GC_DECLARE_ALLOCATOR(CustomElementRegistry);
public:
[[nodiscard]] static GC::Ref<CustomElementRegistry> create_scoped();
[[nodiscard]] static GC::Ref<CustomElementRegistry> create_global(DOM::Document&);
virtual ~CustomElementRegistry() override;
JS::ThrowCompletionOr<void> define(JS::Realm&, Utf16String const& name, GC::Ref<WebIDL::CallbackType> constructor, ElementDefinitionOptions const&);
Variant<GC::Ref<WebIDL::CallbackType>, Empty> get(Utf16FlyString const& name) const;
Optional<Utf16String> get_name(GC::Ref<WebIDL::CallbackType> constructor) const;
GC::Ptr<WebIDL::CallbackType> constructor_for_defined_name(Utf16FlyString const& name) const;
GC::Ptr<WebIDL::Promise> when_defined_promise(Utf16FlyString const& name) const;
void set_when_defined_promise(Utf16FlyString const& name, GC::Ref<WebIDL::Promise>);
void upgrade(GC::Ref<DOM::Node> root) const;
WebIDL::ExceptionOr<void> initialize_for_bindings(GC::Ref<DOM::Node> root);
bool is_scoped() const { return m_is_scoped; }
void append_scoped_document(GC::Ref<DOM::Document>);
GC::Ptr<CustomElementDefinition> get_definition_with_name_and_local_name(Utf16FlyString const& name, Utf16FlyString const& local_name) const;
GC::Ptr<CustomElementDefinition> get_definition_from_new_target(JS::FunctionObject const& new_target) const;
private:
CustomElementRegistry();
virtual void visit_edges(GC::Cell::Visitor&) override;
virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;
GC::Ptr<DOM::Document> m_global_document;
// https://html.spec.whatwg.org/multipage/custom-elements.html#is-scoped
// Every CustomElementRegistry has an is scoped, a boolean, initially false.
bool m_is_scoped { false };
// https://html.spec.whatwg.org/multipage/custom-elements.html#scoped-document-set
// Every CustomElementRegistry has a scoped document set, a set of Document objects, initially « ».
// NB: This is a "weak set", see https://github.com/whatwg/html/issues/12092#issuecomment-3769252677
// NB: Keep association order when defining elements across documents.
GC::OrderedWeakHashSet<DOM::Document> m_scoped_documents;
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-definition-set
// Every CustomElementRegistry has a custom element definition set, a set of custom element definitions,
// initially « ». Lookup of items in this set uses their name, local name, or constructor.
Vector<GC::Ref<CustomElementDefinition>> m_custom_element_definitions;
// https://html.spec.whatwg.org/multipage/custom-elements.html#element-definition-is-running
// Every CustomElementRegistry also has an element definition is running boolean which is used to prevent reentrant
// invocations of element definition. It is initially false.
bool m_element_definition_is_running { false };
// https://html.spec.whatwg.org/multipage/custom-elements.html#when-defined-promise-map
// Every CustomElementRegistry also has a when-defined promise map, mapping valid custom element names to promises.
// It is used to implement the whenDefined() method.
OrderedHashMap<Utf16FlyString, GC::Ref<WebIDL::Promise>> m_when_defined_promise_map;
};
GC::Ptr<CustomElementRegistry> look_up_a_custom_element_registry(DOM::Node const&);
GC::Ptr<CustomElementDefinition> look_up_a_custom_element_definition(GC::Ptr<CustomElementRegistry> registry, Optional<Utf16FlyString> const& namespace_, Utf16FlyString const& local_name, Optional<Utf16FlyString> const& is);
bool is_a_global_custom_element_registry(GC::Ptr<CustomElementRegistry>);
}