CustomElementDefinition.h 5.3 KB

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