CustomElementDefinition.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/String.h>
  8. #include <LibWeb/HTML/HTMLElement.h>
  9. #include <LibWeb/WebIDL/CallbackType.h>
  10. namespace Web::HTML {
  11. struct AlreadyConstructedCustomElementMarker {
  12. };
  13. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-definition
  14. class CustomElementDefinition : public JS::Cell {
  15. JS_CELL(CustomElementDefinition, JS::Cell);
  16. JS_DECLARE_ALLOCATOR(CustomElementDefinition);
  17. using LifecycleCallbacksStorage = OrderedHashMap<FlyString, JS::GCPtr<WebIDL::CallbackType>>;
  18. using ConstructionStackStorage = Vector<Variant<JS::Handle<DOM::Element>, AlreadyConstructedCustomElementMarker>>;
  19. static JS::NonnullGCPtr<CustomElementDefinition> create(JS::Realm& realm, String const& name, String const& local_name, WebIDL::CallbackType& constructor, Vector<String>&& observed_attributes, LifecycleCallbacksStorage&& lifecycle_callbacks, bool form_associated, bool disable_internals, bool disable_shadow)
  20. {
  21. return realm.heap().allocate<CustomElementDefinition>(realm, name, local_name, constructor, move(observed_attributes), move(lifecycle_callbacks), form_associated, disable_internals, disable_shadow);
  22. }
  23. ~CustomElementDefinition() = default;
  24. String const& name() const { return m_name; }
  25. String const& local_name() const { return m_local_name; }
  26. WebIDL::CallbackType& constructor() { return *m_constructor; }
  27. WebIDL::CallbackType const& constructor() const { return *m_constructor; }
  28. Vector<String> const& observed_attributes() const { return m_observed_attributes; }
  29. LifecycleCallbacksStorage const& lifecycle_callbacks() const { return m_lifecycle_callbacks; }
  30. ConstructionStackStorage& construction_stack() { return m_construction_stack; }
  31. ConstructionStackStorage const& construction_stack() const { return m_construction_stack; }
  32. bool form_associated() const { return m_form_associated; }
  33. bool disable_internals() const { return m_disable_internals; }
  34. bool disable_shadow() const { return m_disable_shadow; }
  35. private:
  36. CustomElementDefinition(String const& name, String const& local_name, WebIDL::CallbackType& constructor, Vector<String>&& observed_attributes, LifecycleCallbacksStorage&& lifecycle_callbacks, bool form_associated, bool disable_internals, bool disable_shadow)
  37. : m_name(name)
  38. , m_local_name(local_name)
  39. , m_constructor(constructor)
  40. , m_observed_attributes(move(observed_attributes))
  41. , m_lifecycle_callbacks(move(lifecycle_callbacks))
  42. , m_form_associated(form_associated)
  43. , m_disable_internals(disable_internals)
  44. , m_disable_shadow(disable_shadow)
  45. {
  46. }
  47. virtual void visit_edges(Visitor& visitor) override;
  48. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-name
  49. // A name
  50. // A valid custom element name
  51. String m_name;
  52. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-local-name
  53. // A local name
  54. // A local name
  55. String m_local_name;
  56. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-constructor
  57. // A Web IDL CustomElementConstructor callback function type value wrapping the custom element constructor
  58. JS::NonnullGCPtr<WebIDL::CallbackType> m_constructor;
  59. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-observed-attributes
  60. // A list of observed attributes
  61. // A sequence<DOMString>
  62. Vector<String> m_observed_attributes;
  63. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-lifecycle-callbacks
  64. // A collection of lifecycle callbacks
  65. // A map, whose keys are the strings "connectedCallback", "disconnectedCallback", "adoptedCallback", "attributeChangedCallback",
  66. // "formAssociatedCallback", "formDisabledCallback", "formResetCallback", and "formStateRestoreCallback".
  67. // The corresponding values are either a Web IDL Function callback function type value, or null.
  68. // By default the value of each entry is null.
  69. LifecycleCallbacksStorage m_lifecycle_callbacks;
  70. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-construction-stack
  71. // A construction stack
  72. // A list, initially empty, that is manipulated by the upgrade an element algorithm and the HTML element constructors.
  73. // Each entry in the list will be either an element or an already constructed marker.
  74. ConstructionStackStorage m_construction_stack;
  75. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-form-associated
  76. // A form-associated boolean
  77. // If this is true, user agent treats elements associated to this custom element definition as form-associated custom elements.
  78. bool m_form_associated { false };
  79. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-disable-internals
  80. // A disable internals boolean
  81. // Controls attachInternals().
  82. bool m_disable_internals { false };
  83. // https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-disable-shadow
  84. // A disable shadow boolean
  85. // Controls attachShadow().
  86. bool m_disable_shadow { false };
  87. };
  88. }