CustomElementRegistry.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
  10. namespace Web::HTML {
  11. struct ElementDefinitionOptions {
  12. Optional<String> extends;
  13. };
  14. // https://html.spec.whatwg.org/multipage/custom-elements.html#customelementregistry
  15. class CustomElementRegistry : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(CustomElementRegistry, Bindings::PlatformObject);
  17. GC_DECLARE_ALLOCATOR(CustomElementRegistry);
  18. public:
  19. virtual ~CustomElementRegistry() override;
  20. JS::ThrowCompletionOr<void> define(String const& name, WebIDL::CallbackType* constructor, ElementDefinitionOptions options);
  21. Variant<GC::Root<WebIDL::CallbackType>, JS::Value> get(String const& name) const;
  22. Optional<String> get_name(GC::Root<WebIDL::CallbackType> const& constructor) const;
  23. WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> when_defined(String const& name);
  24. void upgrade(GC::Ref<DOM::Node> root) const;
  25. GC::Ptr<CustomElementDefinition> get_definition_with_name_and_local_name(String const& name, String const& local_name) const;
  26. GC::Ptr<CustomElementDefinition> get_definition_from_new_target(JS::FunctionObject const& new_target) const;
  27. private:
  28. CustomElementRegistry(JS::Realm&);
  29. virtual void initialize(JS::Realm&) override;
  30. virtual void visit_edges(Visitor&) override;
  31. // 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.
  32. Vector<GC::Ref<CustomElementDefinition>> m_custom_element_definitions;
  33. // https://html.spec.whatwg.org/multipage/custom-elements.html#element-definition-is-running
  34. // Every CustomElementRegistry also has an element definition is running flag which is used to prevent reentrant invocations of element definition. It is initially unset.
  35. bool m_element_definition_is_running { false };
  36. // https://html.spec.whatwg.org/multipage/custom-elements.html#when-defined-promise-map
  37. // Every CustomElementRegistry also has a when-defined promise map, mapping valid custom element names to promises. It is used to implement the whenDefined() method.
  38. OrderedHashMap<String, GC::Ref<WebIDL::Promise>> m_when_defined_promise_map;
  39. };
  40. }