CustomElementRegistry.h 2.4 KB

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