@absolutejs/absolute 0.19.0-beta.704 → 0.19.0-beta.706

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/angular/browser.js +6 -4
  2. package/dist/angular/browser.js.map +3 -3
  3. package/dist/angular/index.js +71 -82
  4. package/dist/angular/index.js.map +10 -10
  5. package/dist/angular/server.js +70 -81
  6. package/dist/angular/server.js.map +10 -10
  7. package/dist/build.js +152 -84
  8. package/dist/build.js.map +15 -15
  9. package/dist/cli/index.js +330 -296
  10. package/dist/client/index.js +7 -4
  11. package/dist/client/index.js.map +3 -3
  12. package/dist/index.js +253 -153
  13. package/dist/index.js.map +24 -24
  14. package/dist/islands/index.js +9 -6
  15. package/dist/islands/index.js.map +5 -5
  16. package/dist/react/browser.js +7 -7
  17. package/dist/react/browser.js.map +2 -2
  18. package/dist/react/components/browser/index.js +101 -101
  19. package/dist/react/components/index.js +104 -104
  20. package/dist/react/components/index.js.map +2 -2
  21. package/dist/react/index.js +77 -88
  22. package/dist/react/index.js.map +10 -10
  23. package/dist/react/jsxDevRuntimeCompat.js +1 -6
  24. package/dist/react/jsxDevRuntimeCompat.js.map +3 -3
  25. package/dist/react/server.js +57 -71
  26. package/dist/react/server.js.map +8 -8
  27. package/dist/src/angular/components/defer-slot-templates.directive.d.ts +0 -7
  28. package/dist/src/angular/components/defer-slot.component.d.ts +2 -5
  29. package/dist/src/angular/components/image.component.d.ts +2 -5
  30. package/dist/src/angular/components/index.d.ts +4 -4
  31. package/dist/src/angular/components/stream-slot.component.d.ts +0 -3
  32. package/dist/src/angular/pageHandler.d.ts +6 -1
  33. package/dist/src/angular/ssrRender.d.ts +1 -1
  34. package/dist/src/build/buildAngularVendor.d.ts +3 -4
  35. package/dist/src/constants.d.ts +21 -0
  36. package/dist/src/core/ssrCache.d.ts +1 -1
  37. package/dist/src/core/wrapPageHandlerWithStreamingSlots.d.ts +1 -1
  38. package/dist/src/react/jsxDevRuntimeCompat.d.ts +3 -6
  39. package/dist/src/react/pageHandler.d.ts +2 -1
  40. package/dist/src/svelte/pageHandler.d.ts +2 -2
  41. package/dist/src/utils/defineConfig.d.ts +2 -2
  42. package/dist/src/utils/imageProcessing.d.ts +1 -1
  43. package/dist/src/utils/loadConfig.d.ts +38 -2
  44. package/dist/src/vue/components/Image.d.ts +3 -3
  45. package/dist/src/vue/components/index.d.ts +1 -1
  46. package/dist/src/vue/index.d.ts +1 -1
  47. package/dist/src/vue/pageHandler.d.ts +2 -1
  48. package/dist/svelte/index.js +52 -58
  49. package/dist/svelte/index.js.map +10 -10
  50. package/dist/svelte/server.js +46 -55
  51. package/dist/svelte/server.js.map +9 -9
  52. package/dist/vue/components/Image.js +18 -18
  53. package/dist/vue/components/Image.js.map +3 -3
  54. package/dist/vue/components/index.js +77 -62
  55. package/dist/vue/components/index.js.map +5 -5
  56. package/dist/vue/index.js +137 -142
  57. package/dist/vue/index.js.map +13 -13
  58. package/dist/vue/server.js +54 -77
  59. package/dist/vue/server.js.map +8 -8
  60. package/package.json +42 -42
  61. package/dist/angular/components/constants.js +0 -56
  62. package/dist/angular/components/core/streamingSlotRegistrar.js +0 -58
  63. package/dist/angular/components/core/streamingSlotRegistry.js +0 -114
  64. package/dist/angular/components/defer-slot-payload.js +0 -6
  65. package/dist/angular/components/defer-slot-templates.directive.js +0 -44
  66. package/dist/angular/components/defer-slot.component.js +0 -149
  67. package/dist/angular/components/image.component.js +0 -202
  68. package/dist/angular/components/index.js +0 -4
  69. package/dist/angular/components/stream-slot.component.js +0 -103
  70. package/dist/dev/client/constants.ts +0 -26
  71. package/dist/dev/client/cssUtils.ts +0 -307
  72. package/dist/dev/client/domDiff.ts +0 -226
  73. package/dist/dev/client/domState.ts +0 -421
  74. package/dist/dev/client/domTracker.ts +0 -61
  75. package/dist/dev/client/errorOverlay.ts +0 -184
  76. package/dist/dev/client/frameworkDetect.ts +0 -63
  77. package/dist/dev/client/handlers/angular.ts +0 -551
  78. package/dist/dev/client/handlers/angularRuntime.ts +0 -206
  79. package/dist/dev/client/handlers/html.ts +0 -363
  80. package/dist/dev/client/handlers/htmx.ts +0 -272
  81. package/dist/dev/client/handlers/react.ts +0 -108
  82. package/dist/dev/client/handlers/rebuild.ts +0 -153
  83. package/dist/dev/client/handlers/svelte.ts +0 -332
  84. package/dist/dev/client/handlers/vue.ts +0 -292
  85. package/dist/dev/client/headPatch.ts +0 -233
  86. package/dist/dev/client/hmrClient.ts +0 -251
  87. package/dist/dev/client/hmrState.ts +0 -14
  88. package/dist/dev/client/moduleVersions.ts +0 -62
  89. package/dist/dev/client/reactRefreshSetup.ts +0 -33
  90. package/dist/src/angular/components/constants.d.ts +0 -53
  91. package/dist/svelte/components/AwaitSlot.svelte +0 -39
  92. package/dist/svelte/components/AwaitSlot.svelte.d.ts +0 -2
  93. package/dist/svelte/components/Head.svelte +0 -144
  94. package/dist/svelte/components/Head.svelte.d.ts +0 -2
  95. package/dist/svelte/components/Image.svelte +0 -164
  96. package/dist/svelte/components/Image.svelte.d.ts +0 -5
  97. package/dist/svelte/components/Island.svelte +0 -71
  98. package/dist/svelte/components/Island.svelte.d.ts +0 -5
  99. package/dist/svelte/components/JsonLd.svelte +0 -21
  100. package/dist/svelte/components/JsonLd.svelte.d.ts +0 -2
  101. package/dist/svelte/components/StreamSlot.svelte +0 -41
  102. package/dist/svelte/components/StreamSlot.svelte.d.ts +0 -2
  103. package/dist/types/globals.d.ts +0 -121
@@ -1,226 +0,0 @@
1
- import type {} from '../../types/globals';
2
- /* DOM diffing/patching for in-place updates (zero flicker) */
3
-
4
- import { UNFOUND_INDEX } from './constants';
5
-
6
- type KeyedEntry = {
7
- index: number;
8
- node: Node;
9
- };
10
-
11
- const getElementKey = (elem: Node, index: number) => {
12
- if (elem.nodeType !== Node.ELEMENT_NODE) return `text_${index}`;
13
- if (!(elem instanceof Element)) return `text_${index}`;
14
- if (elem.id) return `id_${elem.id}`;
15
- if (elem.hasAttribute('data-key'))
16
- return `key_${elem.getAttribute('data-key')}`;
17
-
18
- return `tag_${elem.tagName}_${index}`;
19
- };
20
-
21
- const updateElementAttributes = (oldEl: Element, newEl: Element) => {
22
- const newAttrs = Array.from(newEl.attributes);
23
- const oldAttrs = Array.from(oldEl.attributes);
24
- const runtimeAttrs = ['data-hmr-listeners-attached'];
25
-
26
- oldAttrs.forEach((oldAttr) => {
27
- if (
28
- !newEl.hasAttribute(oldAttr.name) &&
29
- runtimeAttrs.indexOf(oldAttr.name) === UNFOUND_INDEX
30
- ) {
31
- oldEl.removeAttribute(oldAttr.name);
32
- }
33
- });
34
-
35
- newAttrs.forEach((newAttr) => {
36
- if (
37
- runtimeAttrs.indexOf(newAttr.name) !== UNFOUND_INDEX &&
38
- oldEl.hasAttribute(newAttr.name)
39
- ) {
40
- return;
41
- }
42
- const oldValue = oldEl.getAttribute(newAttr.name);
43
- if (oldValue !== newAttr.value) {
44
- oldEl.setAttribute(newAttr.name, newAttr.value);
45
- }
46
- });
47
- };
48
-
49
- const updateTextNode = (oldNode: Node, newNode: Node) => {
50
- if (oldNode.nodeValue !== newNode.nodeValue) {
51
- oldNode.nodeValue = newNode.nodeValue;
52
- }
53
- };
54
-
55
- const matchChildren = (oldChildren: Node[], newChildren: Node[]) => {
56
- const oldMap = new Map<string, KeyedEntry[]>();
57
- const newMap = new Map<string, KeyedEntry[]>();
58
-
59
- oldChildren.forEach((child, idx) => {
60
- const key = getElementKey(child, idx);
61
- if (!oldMap.has(key)) {
62
- oldMap.set(key, []);
63
- }
64
- oldMap.get(key)?.push({ index: idx, node: child });
65
- });
66
-
67
- newChildren.forEach((child, idx) => {
68
- const key = getElementKey(child, idx);
69
- if (!newMap.has(key)) {
70
- newMap.set(key, []);
71
- }
72
- newMap.get(key)?.push({ index: idx, node: child });
73
- });
74
-
75
- return { newMap, oldMap };
76
- };
77
-
78
- const isHMRScript = (elem: Node) =>
79
- elem instanceof Element && elem.hasAttribute('data-hmr-client');
80
-
81
- const isHMRPreserved = (elem: Node) =>
82
- isHMRScript(elem) ||
83
- (elem instanceof Element && elem.hasAttribute('data-hmr-overlay'));
84
-
85
- const isNonHMRScript = (child: Node) =>
86
- child instanceof Element && child.tagName === 'SCRIPT';
87
-
88
- const findBestMatch = (oldMatches: KeyedEntry[], matchedOld: Set<Node>) => {
89
- const unmatched = oldMatches.find((entry) => !matchedOld.has(entry.node));
90
- if (unmatched) return unmatched;
91
- if (oldMatches.length > 0) return oldMatches[0] ?? null;
92
-
93
- return null;
94
- };
95
-
96
- const reconcileChild = (
97
- newChild: Node,
98
- newIndex: number,
99
- oldMap: Map<string, KeyedEntry[]>,
100
- matchedOld: Set<Node>,
101
- parentNode: Node,
102
- oldChildrenFiltered: Node[]
103
- ) => {
104
- const newKey = getElementKey(newChild, newIndex);
105
- const oldMatches = oldMap.get(newKey) || [];
106
-
107
- if (oldMatches.length === 0) {
108
- const clone = newChild.cloneNode(true);
109
- parentNode.insertBefore(clone, oldChildrenFiltered[newIndex] || null);
110
-
111
- return;
112
- }
113
-
114
- const bestMatch = findBestMatch(oldMatches, matchedOld);
115
- if (bestMatch && !matchedOld.has(bestMatch.node)) {
116
- matchedOld.add(bestMatch.node);
117
- patchNode(bestMatch.node, newChild);
118
-
119
- return;
120
- }
121
-
122
- const clone = newChild.cloneNode(true);
123
- parentNode.insertBefore(clone, oldChildrenFiltered[newIndex] || null);
124
- };
125
-
126
- const patchNode = (oldNode: Node, newNode: Node) => {
127
- if (
128
- oldNode.nodeType === Node.TEXT_NODE &&
129
- newNode.nodeType === Node.TEXT_NODE
130
- ) {
131
- updateTextNode(oldNode, newNode);
132
-
133
- return;
134
- }
135
-
136
- if (
137
- oldNode.nodeType !== Node.ELEMENT_NODE ||
138
- newNode.nodeType !== Node.ELEMENT_NODE
139
- ) {
140
- return;
141
- }
142
-
143
- if (!(oldNode instanceof Element) || !(newNode instanceof Element)) return;
144
- const oldEl = oldNode;
145
- const newEl = newNode;
146
-
147
- if (oldEl.tagName !== newEl.tagName) {
148
- const clone = newEl.cloneNode(true);
149
- oldEl.replaceWith(clone);
150
-
151
- return;
152
- }
153
-
154
- updateElementAttributes(oldEl, newEl);
155
-
156
- const oldChildren = Array.from(oldNode.childNodes);
157
- const newChildren = Array.from(newNode.childNodes);
158
-
159
- const oldChildrenFiltered = oldChildren.filter(
160
- (child) => !isHMRScript(child) && !isNonHMRScript(child)
161
- );
162
- const newChildrenFiltered = newChildren.filter(
163
- (child) => !isHMRScript(child) && !isNonHMRScript(child)
164
- );
165
-
166
- const { oldMap } = matchChildren(oldChildrenFiltered, newChildrenFiltered);
167
- const matchedOld = new Set<Node>();
168
-
169
- newChildrenFiltered.forEach((newChild, newIndex) => {
170
- reconcileChild(
171
- newChild,
172
- newIndex,
173
- oldMap,
174
- matchedOld,
175
- oldNode,
176
- oldChildrenFiltered
177
- );
178
- });
179
-
180
- oldChildrenFiltered.forEach((oldChild) => {
181
- if (!matchedOld.has(oldChild) && !isHMRPreserved(oldChild)) {
182
- oldChild.remove();
183
- }
184
- });
185
- };
186
-
187
- export const patchDOMInPlace = (oldContainer: HTMLElement, newHTML: string) => {
188
- const tempDiv = document.createElement('div');
189
- tempDiv.innerHTML = newHTML;
190
- const newContainer = tempDiv;
191
-
192
- const oldChildren = Array.from(oldContainer.childNodes);
193
- const newChildren = Array.from(newContainer.childNodes);
194
-
195
- const oldChildrenFiltered = oldChildren.filter(
196
- (child) =>
197
- !(
198
- child instanceof Element &&
199
- child.tagName === 'SCRIPT' &&
200
- !child.hasAttribute('data-hmr-client')
201
- )
202
- );
203
- const newChildrenFiltered = newChildren.filter(
204
- (child) => !isNonHMRScript(child)
205
- );
206
-
207
- const { oldMap } = matchChildren(oldChildrenFiltered, newChildrenFiltered);
208
- const matchedOld = new Set<Node>();
209
-
210
- newChildrenFiltered.forEach((newChild, newIndex) => {
211
- reconcileChild(
212
- newChild,
213
- newIndex,
214
- oldMap,
215
- matchedOld,
216
- oldContainer,
217
- oldChildrenFiltered
218
- );
219
- });
220
-
221
- oldChildrenFiltered.forEach((oldChild) => {
222
- if (matchedOld.has(oldChild)) return;
223
- if (isHMRPreserved(oldChild)) return;
224
- oldChild.remove();
225
- });
226
- };
@@ -1,421 +0,0 @@
1
- import type {} from '../../types/globals';
2
- /* DOM state snapshot/restore to preserve user-visible state across HMR */
3
-
4
- import type { DOMStateEntry, DOMStateSnapshot } from '../../types/client';
5
- import {
6
- FOCUS_ID_PREFIX_LENGTH,
7
- FOCUS_IDX_PREFIX_LENGTH,
8
- FOCUS_NAME_PREFIX_LENGTH,
9
- UNFOUND_INDEX
10
- } from './constants';
11
-
12
- const trySetSelectionRange = (
13
- element: HTMLInputElement | HTMLTextAreaElement,
14
- start: number,
15
- end: number
16
- ) => {
17
- try {
18
- element.setSelectionRange(start, end);
19
- } catch {
20
- /* ignore */
21
- }
22
- };
23
-
24
- const restoreSelectionRange = (
25
- element: HTMLInputElement | HTMLTextAreaElement,
26
- entry: DOMStateEntry
27
- ) => {
28
- if (
29
- entry.selStart === undefined ||
30
- entry.selEnd === undefined ||
31
- !element.setSelectionRange
32
- )
33
- return;
34
- trySetSelectionRange(element, entry.selStart, entry.selEnd);
35
- };
36
-
37
- const restoreInputEntry = (target: Element, entry: DOMStateEntry) => {
38
- if (!(target instanceof HTMLInputElement)) return;
39
- const input = target;
40
- const type = entry.type || input.getAttribute('type') || 'text';
41
- if (type === 'checkbox' || type === 'radio') {
42
- if (entry.checked !== undefined) input.checked = entry.checked;
43
- } else if (entry.value !== undefined) {
44
- input.value = entry.value;
45
- }
46
- restoreSelectionRange(input, entry);
47
- };
48
-
49
- const restoreTextareaEntry = (target: Element, entry: DOMStateEntry) => {
50
- if (!(target instanceof HTMLTextAreaElement)) return;
51
- const textarea = target;
52
- if (entry.value !== undefined) textarea.value = entry.value;
53
- restoreSelectionRange(textarea, entry);
54
- };
55
-
56
- const restoreSelectEntry = (target: Element, entry: DOMStateEntry) => {
57
- if (!Array.isArray(entry.values)) return;
58
- if (!(target instanceof HTMLSelectElement)) return;
59
- const select = target;
60
- const { values } = entry;
61
- Array.from(select.options).forEach((opt) => {
62
- opt.selected = values.indexOf(opt.value) !== UNFOUND_INDEX;
63
- });
64
- };
65
-
66
- const restoreEntry = (target: Element, entry: DOMStateEntry) => {
67
- if (target.tagName === 'INPUT') {
68
- restoreInputEntry(target, entry);
69
-
70
- return;
71
- }
72
- if (target.tagName === 'TEXTAREA') {
73
- restoreTextareaEntry(target, entry);
74
-
75
- return;
76
- }
77
- if (target.tagName === 'SELECT') {
78
- restoreSelectEntry(target, entry);
79
-
80
- return;
81
- }
82
- if (target.tagName === 'OPTION') {
83
- if (entry.selected !== undefined && target instanceof HTMLOptionElement)
84
- target.selected = entry.selected;
85
-
86
- return;
87
- }
88
- if (target.tagName === 'DETAILS') {
89
- if (entry.open !== undefined && target instanceof HTMLDetailsElement)
90
- target.open = entry.open;
91
-
92
- return;
93
- }
94
- if (target.getAttribute('contenteditable') === 'true') {
95
- if (entry.text !== undefined) target.textContent = entry.text;
96
- }
97
- };
98
-
99
- const findEntryTarget = (
100
- root: HTMLElement,
101
- elements: NodeListOf<Element>,
102
- entry: DOMStateEntry
103
- ) => {
104
- if (entry.id) return root.querySelector(`#${CSS.escape(entry.id)}`);
105
- if (entry.name)
106
- return root.querySelector(`[name="${CSS.escape(entry.name)}"]`);
107
- if (elements[entry.idx]) return elements[entry.idx] ?? null;
108
-
109
- return null;
110
- };
111
-
112
- const resolveFocusElement = (
113
- root: HTMLElement,
114
- elements: NodeListOf<Element>,
115
- activeKey: string
116
- ) => {
117
- if (activeKey.startsWith('id:'))
118
- return root.querySelector(
119
- `#${CSS.escape(activeKey.slice(FOCUS_ID_PREFIX_LENGTH))}`
120
- );
121
- if (activeKey.startsWith('name:'))
122
- return root.querySelector(
123
- `[name="${CSS.escape(activeKey.slice(FOCUS_NAME_PREFIX_LENGTH))}"]`
124
- );
125
- if (!activeKey.startsWith('idx:')) return null;
126
- const idx = parseInt(activeKey.slice(FOCUS_IDX_PREFIX_LENGTH), 10);
127
- if (isNaN(idx) || !elements[idx]) return null;
128
-
129
- return elements[idx];
130
- };
131
-
132
- export const restoreDOMState = (
133
- root: HTMLElement,
134
- snapshot: DOMStateSnapshot
135
- ) => {
136
- if (!snapshot || !snapshot.items) return;
137
- const selector =
138
- 'input, textarea, select, option, [contenteditable="true"], details';
139
- const elements = root.querySelectorAll(selector);
140
-
141
- snapshot.items.forEach((entry) => {
142
- const target = findEntryTarget(root, elements, entry);
143
- if (!target) return;
144
- restoreEntry(target, entry);
145
- });
146
-
147
- if (!snapshot.activeKey) return;
148
- const focusEl = resolveFocusElement(root, elements, snapshot.activeKey);
149
- if (focusEl instanceof HTMLElement) {
150
- focusEl.focus();
151
- }
152
- };
153
-
154
- const resolveFormElement = (
155
- isStandalone: boolean,
156
- form: Element | null,
157
- name: string
158
- ) => {
159
- if (isStandalone) {
160
- const element: HTMLInputElement | null = document.querySelector(
161
- `input[name="${name}"], textarea[name="${name}"], select[name="${name}"]`
162
- );
163
- if (element) return element;
164
-
165
- const byId = document.getElementById(name);
166
- if (byId instanceof HTMLInputElement) return byId;
167
-
168
- return null;
169
- }
170
- if (!form) return null;
171
-
172
- const found = form.querySelector(`[name="${name}"], #${name}`);
173
- if (
174
- found instanceof HTMLInputElement ||
175
- found instanceof HTMLTextAreaElement ||
176
- found instanceof HTMLSelectElement
177
- )
178
- return found;
179
-
180
- return null;
181
- };
182
-
183
- const applyFormValue = (
184
- element: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement,
185
- value: boolean | string
186
- ) => {
187
- if (
188
- element instanceof HTMLInputElement &&
189
- (element.type === 'checkbox' || element.type === 'radio')
190
- ) {
191
- element.checked = value === true;
192
-
193
- return;
194
- }
195
- element.value = String(value);
196
- };
197
-
198
- const resolveForm = (formId: string) => {
199
- const formIndex = parseInt(formId.replace('form-', ''));
200
- const form = document.getElementById(formId);
201
- if (form) return form;
202
- if (isNaN(formIndex)) return null;
203
- try {
204
- return document.querySelector(`form:nth-of-type(${formIndex + 1})`);
205
- } catch {
206
- return null;
207
- }
208
- };
209
-
210
- const restoreRadioGroup = (
211
- isStandalone: boolean,
212
- form: Element | null,
213
- groupName: string,
214
- selectedValue: string
215
- ) => {
216
- const scope = isStandalone ? document : form;
217
- if (!scope) return;
218
-
219
- const escapedName = CSS.escape(groupName);
220
- const escapedValue = CSS.escape(selectedValue);
221
- const radio = scope.querySelector<HTMLInputElement>(
222
- `input[type="radio"][name="${escapedName}"][value="${escapedValue}"]`
223
- );
224
-
225
- if (radio) {
226
- radio.checked = true;
227
- }
228
- };
229
-
230
- const RADIO_PREFIX = '__radio__';
231
-
232
- export const restoreFormState = (
233
- formState: Record<string, Record<string, boolean | string>>
234
- ) => {
235
- Object.keys(formState).forEach((formId) => {
236
- const isStandalone = formId === '__standalone__';
237
- const form = isStandalone ? null : resolveForm(formId);
238
- const formData = formState[formId];
239
- if (!formData) return;
240
- Object.keys(formData).forEach((name) => {
241
- if (name.startsWith(RADIO_PREFIX)) {
242
- const groupName = name.slice(RADIO_PREFIX.length);
243
- const value = formData[name];
244
- if (value === undefined) return;
245
- restoreRadioGroup(isStandalone, form, groupName, String(value));
246
-
247
- return;
248
- }
249
- const element = resolveFormElement(isStandalone, form, name);
250
- if (!element) return;
251
- const value = formData[name];
252
- if (value === undefined) return;
253
- applyFormValue(element, value);
254
- });
255
- });
256
- };
257
-
258
- export const restoreScrollState = (scrollState: {
259
- window: { x: number; y: number };
260
- }) => {
261
- if (scrollState && scrollState.window) {
262
- window.scrollTo(scrollState.window.x, scrollState.window.y);
263
- }
264
- };
265
-
266
- const saveInputEntry = (elem: Element, entry: DOMStateEntry) => {
267
- if (!(elem instanceof HTMLInputElement)) return;
268
- const input = elem;
269
- const type = input.getAttribute('type') || 'text';
270
- entry.type = type;
271
- if (type === 'checkbox' || type === 'radio') {
272
- entry.checked = input.checked;
273
- } else {
274
- entry.value = input.value;
275
- }
276
- if (input.selectionStart !== null && input.selectionEnd !== null) {
277
- entry.selStart = input.selectionStart;
278
- entry.selEnd = input.selectionEnd;
279
- }
280
- };
281
-
282
- const saveTextareaEntry = (elem: Element, entry: DOMStateEntry) => {
283
- if (!(elem instanceof HTMLTextAreaElement)) return;
284
- const textarea = elem;
285
- entry.value = textarea.value;
286
- if (textarea.selectionStart !== null && textarea.selectionEnd !== null) {
287
- entry.selStart = textarea.selectionStart;
288
- entry.selEnd = textarea.selectionEnd;
289
- }
290
- };
291
-
292
- const saveSelectEntry = (elem: Element, entry: DOMStateEntry) => {
293
- if (!(elem instanceof HTMLSelectElement)) return;
294
- const select = elem;
295
- const vals: string[] = [];
296
- Array.from(select.options).forEach((opt) => {
297
- if (opt.selected) vals.push(opt.value);
298
- });
299
- entry.values = vals;
300
- };
301
-
302
- const saveElementEntry = (elem: Element, entry: DOMStateEntry) => {
303
- if (elem.tagName === 'INPUT') {
304
- saveInputEntry(elem, entry);
305
-
306
- return;
307
- }
308
- if (elem.tagName === 'TEXTAREA') {
309
- saveTextareaEntry(elem, entry);
310
-
311
- return;
312
- }
313
- if (elem.tagName === 'SELECT') {
314
- saveSelectEntry(elem, entry);
315
-
316
- return;
317
- }
318
- if (elem.tagName === 'OPTION') {
319
- if (elem instanceof HTMLOptionElement) entry.selected = elem.selected;
320
-
321
- return;
322
- }
323
- if (elem.tagName === 'DETAILS') {
324
- if (elem instanceof HTMLDetailsElement) entry.open = elem.open;
325
-
326
- return;
327
- }
328
- if (elem.getAttribute('contenteditable') === 'true') {
329
- entry.text = elem.textContent || undefined;
330
- }
331
- };
332
-
333
- export const saveDOMState = (root: HTMLElement) => {
334
- const snapshot: DOMStateSnapshot = { activeKey: null, items: [] };
335
- const selector =
336
- 'input, textarea, select, option, [contenteditable="true"], details';
337
- const elements = root.querySelectorAll(selector);
338
-
339
- elements.forEach((el, idx) => {
340
- const entry: DOMStateEntry = {
341
- idx,
342
- tag: el.tagName.toLowerCase()
343
- };
344
- const id = el.getAttribute('id');
345
- const name = el.getAttribute('name');
346
- if (id) entry.id = id;
347
- else if (name) entry.name = name;
348
- saveElementEntry(el, entry);
349
- snapshot.items.push(entry);
350
- });
351
-
352
- const active = document.activeElement;
353
- if (!active || !root.contains(active)) return snapshot;
354
- const id = active.getAttribute('id');
355
- const name = active.getAttribute('name');
356
- if (id) snapshot.activeKey = `id:${id}`;
357
- else if (name) snapshot.activeKey = `name:${name}`;
358
- else
359
- snapshot.activeKey = `idx:${Array.prototype.indexOf.call(elements, active)}`;
360
-
361
- return snapshot;
362
- };
363
-
364
- const collectInputState = (
365
- element: HTMLInputElement,
366
- name: string,
367
- target: Record<string, boolean | string>
368
- ) => {
369
- if (element.type === 'radio') {
370
- if (element.checked) target[`__radio__${name}`] = element.value;
371
-
372
- return;
373
- }
374
- if (element.type === 'checkbox') {
375
- target[name] = element.checked;
376
-
377
- return;
378
- }
379
- target[name] = element.value;
380
- };
381
-
382
- export const saveFormState = () => {
383
- const formState: Record<string, Record<string, boolean | string>> = {};
384
- const forms = document.querySelectorAll('form');
385
- forms.forEach((form, formIndex) => {
386
- const formId = form.id || `form-${formIndex}`;
387
- const formData: Record<string, boolean | string> = {};
388
- formState[formId] = formData;
389
- const inputs = form.querySelectorAll('input, textarea, select');
390
- inputs.forEach((input) => {
391
- if (!(input instanceof HTMLInputElement)) return;
392
- const name =
393
- input.name || input.id || `input-${formIndex}-${inputs.length}`;
394
- collectInputState(input, name, formData);
395
- });
396
- });
397
-
398
- const standaloneInputs = document.querySelectorAll(
399
- 'input:not(form input), textarea:not(form textarea), select:not(form select)'
400
- );
401
- if (standaloneInputs.length <= 0) return formState;
402
- const standaloneData: Record<string, boolean | string> = {};
403
- formState['__standalone__'] = standaloneData;
404
- standaloneInputs.forEach((input) => {
405
- if (!(input instanceof HTMLInputElement)) return;
406
- const name =
407
- input.name || input.id || `standalone-${standaloneInputs.length}`;
408
- collectInputState(input, name, standaloneData);
409
- });
410
-
411
- return formState;
412
- };
413
-
414
- export const saveScrollState = () => {
415
- const scrollX = window.scrollX || window.pageXOffset;
416
- const scrollY = window.scrollY || window.pageYOffset;
417
-
418
- return {
419
- window: { x: scrollX, y: scrollY }
420
- };
421
- };
@@ -1,61 +0,0 @@
1
- import type {} from '../../types/globals';
2
- /* Snapshot/restore for JS-modified DOM state across HMR updates.
3
- * Before patching, captures text and dynamic children of elements with IDs.
4
- * After patching, restores values that were changed by user scripts. */
5
-
6
- type DOMSnapshot = {
7
- children: Map<string, string>;
8
- text: Map<string, string>;
9
- };
10
-
11
- export const restoreDOMChanges = (
12
- root: HTMLElement,
13
- snapshot: DOMSnapshot,
14
- newHTML: string
15
- ) => {
16
- const tempDiv = document.createElement('div');
17
- tempDiv.innerHTML = newHTML;
18
-
19
- /* Restore JS-modified text on leaf elements */
20
- snapshot.text.forEach((liveText, elId) => {
21
- const newEl = tempDiv.querySelector(`#${CSS.escape(elId)}`);
22
- const newText = newEl ? newEl.textContent || '' : '';
23
- if (liveText === newText) return;
24
-
25
- const liveEl = root.querySelector(`#${CSS.escape(elId)}`);
26
- if (liveEl) {
27
- liveEl.textContent = liveText;
28
- }
29
- });
30
-
31
- /* Restore JS-added children (e.g. dynamically appended list items) */
32
- snapshot.children.forEach((liveHTML, elId) => {
33
- const newEl = tempDiv.querySelector(`#${CSS.escape(elId)}`);
34
- const newInner = newEl ? newEl.innerHTML : '';
35
- if (liveHTML === newInner || liveHTML.length <= newInner.length) return;
36
-
37
- const liveEl = root.querySelector(`#${CSS.escape(elId)}`);
38
- if (liveEl) {
39
- liveEl.innerHTML = liveHTML;
40
- }
41
- });
42
- };
43
- export const snapshotDOMChanges = (root: HTMLElement): DOMSnapshot => {
44
- const text = new Map<string, string>();
45
- const children = new Map<string, string>();
46
-
47
- root.querySelectorAll('[id]').forEach((elem) => {
48
- const { childNodes } = elem;
49
- const isTextLeaf = Array.from(childNodes).every(
50
- (child) => child.nodeType === Node.TEXT_NODE
51
- );
52
-
53
- if (isTextLeaf && childNodes.length > 0) {
54
- text.set(elem.id, elem.textContent || '');
55
- } else if (elem.children.length > 0) {
56
- children.set(elem.id, elem.innerHTML);
57
- }
58
- });
59
-
60
- return { children, text };
61
- };