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