CustomElementRegistry.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. public:
  18. virtual ~CustomElementRegistry() override;
  19. JS::ThrowCompletionOr<void> define(String const& name, WebIDL::CallbackType* constructor, ElementDefinitionOptions options);
  20. Variant<JS::Handle<WebIDL::CallbackType>, JS::Value> get(String const& name) const;
  21. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> when_defined(String const& name);
  22. void upgrade(JS::NonnullGCPtr<DOM::Node> root) const;
  23. JS::GCPtr<CustomElementDefinition> get_definition_with_name_and_local_name(String const& name, String const& local_name) const;
  24. JS::GCPtr<CustomElementDefinition> get_definition_from_new_target(JS::FunctionObject const& new_target) const;
  25. private:
  26. CustomElementRegistry(JS::Realm&);
  27. virtual void initialize(JS::Realm&) override;
  28. // 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.
  29. Vector<JS::Handle<CustomElementDefinition>> m_custom_element_definitions;
  30. // https://html.spec.whatwg.org/multipage/custom-elements.html#element-definition-is-running
  31. // Every CustomElementRegistry also has an element definition is running flag which is used to prevent reentrant invocations of element definition. It is initially unset.
  32. bool m_element_definition_is_running { false };
  33. // https://html.spec.whatwg.org/multipage/custom-elements.html#when-defined-promise-map
  34. // Every CustomElementRegistry also has a when-defined promise map, mapping valid custom element names to promises. It is used to implement the whenDefined() method.
  35. OrderedHashMap<String, JS::Handle<JS::Promise>> m_when_defined_promise_map;
  36. };
  37. }