Debian Codegraph @

packages/labs/ssr-dom-shim/src/lib/element-internals.ts

raw
1 /**
2 * @license
3 * Copyright 2023 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 type StringKeys<T extends object> = {
8 [K in keyof T]: T[K] extends string | null ? K : never;
9 }[keyof T];
10
11 // Since TypeScript 5.9, ARIAMixin has properties with the type Element | null
12 // or Element[] | null. However, we can only support string attributes,
13 // which is why we filter for string properties.
14 type ARIAAttributeMap = {
15 [K in StringKeys<ARIAMixin>]: string;
16 };
17
18 /**
19 * Map of ARIAMixin properties to attributes
20 */
21 export const ariaMixinAttributes: ARIAAttributeMap = {
22 ariaAtomic: 'aria-atomic',
23 ariaAutoComplete: 'aria-autocomplete',
24 ariaBrailleLabel: 'aria-braillelabel',
25 ariaBrailleRoleDescription: 'aria-brailleroledescription',
26 ariaBusy: 'aria-busy',
27 ariaChecked: 'aria-checked',
28 ariaColCount: 'aria-colcount',
29 ariaColIndex: 'aria-colindex',
30 ariaColIndexText: 'aria-colindextext',
31 ariaColSpan: 'aria-colspan',
32 ariaCurrent: 'aria-current',
33 ariaDescription: 'aria-description',
34 ariaDisabled: 'aria-disabled',
35 ariaExpanded: 'aria-expanded',
36 ariaHasPopup: 'aria-haspopup',
37 ariaHidden: 'aria-hidden',
38 ariaInvalid: 'aria-invalid',
39 ariaKeyShortcuts: 'aria-keyshortcuts',
40 ariaLabel: 'aria-label',
41 ariaLevel: 'aria-level',
42 ariaLive: 'aria-live',
43 ariaModal: 'aria-modal',
44 ariaMultiLine: 'aria-multiline',
45 ariaMultiSelectable: 'aria-multiselectable',
46 ariaOrientation: 'aria-orientation',
47 ariaPlaceholder: 'aria-placeholder',
48 ariaPosInSet: 'aria-posinset',
49 ariaPressed: 'aria-pressed',
50 ariaReadOnly: 'aria-readonly',
51 ariaRelevant: 'aria-relevant',
52 ariaRequired: 'aria-required',
53 ariaRoleDescription: 'aria-roledescription',
54 ariaRowCount: 'aria-rowcount',
55 ariaRowIndex: 'aria-rowindex',
56 ariaRowIndexText: 'aria-rowindextext',
57 ariaRowSpan: 'aria-rowspan',
58 ariaSelected: 'aria-selected',
59 ariaSetSize: 'aria-setsize',
60 ariaSort: 'aria-sort',
61 ariaValueMax: 'aria-valuemax',
62 ariaValueMin: 'aria-valuemin',
63 ariaValueNow: 'aria-valuenow',
64 ariaValueText: 'aria-valuetext',
65 role: 'role',
66 };
67
68 type ElementInternalsInterface = ElementInternals;
69
70 // Shim the global element internals object
71 // Methods should be fine as noops and properties can generally
72 // be while on the server.
73 export const ElementInternalsShim = class ElementInternals
74 implements ElementInternalsInterface
75 {
76 ariaActiveDescendantElement = null;
77 ariaAtomic = '';
78 ariaAutoComplete = '';
79 ariaBrailleLabel = '';
80 ariaBrailleRoleDescription = '';
81 ariaBusy = '';
82 ariaChecked = '';
83 ariaColCount = '';
84 ariaColIndex = '';
85 ariaColIndexText = '';
86 ariaColSpan = '';
87 ariaControlsElements = null;
88 ariaCurrent = '';
89 ariaDescribedByElements = null;
90 ariaDescription = '';
91 ariaDetailsElements = null;
92 ariaDisabled = '';
93 ariaErrorMessageElements = null;
94 ariaExpanded = '';
95 ariaFlowToElements = null;
96 ariaHasPopup = '';
97 ariaHidden = '';
98 ariaInvalid = '';
99 ariaKeyShortcuts = '';
100 ariaLabel = '';
101 ariaLabelledByElements = null;
102 ariaLevel = '';
103 ariaLive = '';
104 ariaModal = '';
105 ariaMultiLine = '';
106 ariaMultiSelectable = '';
107 ariaOrientation = '';
108 ariaOwnsElements = null;
109 ariaPlaceholder = '';
110 ariaPosInSet = '';
111 ariaPressed = '';
112 ariaReadOnly = '';
113 ariaRelevant = '';
114 ariaRequired = '';
115 ariaRoleDescription = '';
116 ariaRowCount = '';
117 ariaRowIndex = '';
118 ariaRowIndexText = '';
119 ariaRowSpan = '';
120 ariaSelected = '';
121 ariaSetSize = '';
122 ariaSort = '';
123 ariaValueMax = '';
124 ariaValueMin = '';
125 ariaValueNow = '';
126 ariaValueText = '';
127 role = '';
128 __host: HTMLElement;
129 get shadowRoot() {
130 // Grab the shadow root instance from the Element shim
131 // to ensure that the shadow root is always available
132 // to the internals instance even if the mode is 'closed'
133 return (this.__host as HTMLElement & {__shadowRoot: ShadowRoot})
134 .__shadowRoot;
135 }
136 constructor(_host: HTMLElement) {
137 this.__host = _host;
138 }
139 checkValidity() {
140 // TODO(augustjk) Consider actually implementing logic.
141 // See https://github.com/lit/lit/issues/3740
142 console.warn(
143 '`ElementInternals.checkValidity()` was called on the server.' +
144 'This method always returns true.'
145 );
146 return true;
147 }
148 form = null;
149 labels = [] as unknown as NodeListOf<HTMLLabelElement>;
150 reportValidity() {
151 return true;
152 }
153 setFormValue(): void {}
154 setValidity(): void {}
155 states = new Set<string>();
156 validationMessage = '';
157 validity = {} as ValidityState;
158 willValidate = true;
159 };
160
161 const ElementInternalsShimWithRealType =
162 ElementInternalsShim as object as typeof ElementInternals;
163 export {ElementInternalsShimWithRealType as ElementInternals};
164
165 export const HYDRATE_INTERNALS_ATTR_PREFIX = 'hydrate-internals-';