LibWeb: Implement scoped custom element registries

This commit is contained in:
Sam Atkins
2026-02-27 17:05:47 +00:00
committed by Tim Ledbetter
parent 90c39c214d
commit ed6a5f25a0
Notes: github-actions[bot] 2026-03-27 19:50:58 +00:00
48 changed files with 1243 additions and 749 deletions

View File

@@ -7,6 +7,7 @@
#pragma once
#include <LibGC/WeakHashSet.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
@@ -23,6 +24,8 @@ class WEB_API CustomElementRegistry : public Bindings::PlatformObject {
GC_DECLARE_ALLOCATOR(CustomElementRegistry);
public:
[[nodiscard]] static GC::Ref<CustomElementRegistry> construct_impl(JS::Realm&);
virtual ~CustomElementRegistry() override;
JS::ThrowCompletionOr<void> define(String const& name, WebIDL::CallbackType* constructor, ElementDefinitionOptions options);
@@ -30,6 +33,10 @@ public:
Optional<String> get_name(GC::Root<WebIDL::CallbackType> const& constructor) const;
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> when_defined(String const& name);
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(String const& name, String const& local_name) const;
GC::Ptr<CustomElementDefinition> get_definition_from_new_target(JS::FunctionObject const& new_target) const;
@@ -40,18 +47,34 @@ private:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
// Every CustomElementRegistry has a set of custom element definitions, initially empty. In general, algorithms in this specification look up elements in the registry by any of name, local name, or constructor.
// 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
GC::WeakHashSet<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 flag which is used to prevent reentrant invocations of element definition. It is initially unset.
// 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.
// 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<String, 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<FlyString> const& namespace_, FlyString const& local_name, Optional<String> const& is);
bool is_a_global_custom_element_registry(GC::Ptr<CustomElementRegistry>);
}