@absolutejs/absolute 0.19.0-beta.706 → 0.19.0-beta.707
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.
- package/dist/angular/components/constants.js +78 -0
- package/dist/angular/components/core/streamingSlotRegistrar.js +58 -0
- package/dist/angular/components/core/streamingSlotRegistry.js +114 -0
- package/dist/angular/components/defer-slot-payload.js +6 -0
- package/dist/angular/components/defer-slot-templates.directive.js +44 -0
- package/dist/angular/components/defer-slot.component.js +149 -0
- package/dist/angular/components/image.component.js +202 -0
- package/dist/angular/components/index.js +4 -0
- package/dist/angular/components/stream-slot.component.js +103 -0
- package/dist/angular/index.js +7 -9
- package/dist/angular/index.js.map +4 -4
- package/dist/angular/server.js +7 -9
- package/dist/angular/server.js.map +4 -4
- package/dist/build.js +19 -26
- package/dist/build.js.map +4 -4
- package/dist/cli/index.js +197 -144
- package/dist/client/index.js +2 -4
- package/dist/client/index.js.map +2 -2
- package/dist/dev/client/constants.ts +26 -0
- package/dist/dev/client/cssUtils.ts +307 -0
- package/dist/dev/client/domDiff.ts +226 -0
- package/dist/dev/client/domState.ts +421 -0
- package/dist/dev/client/domTracker.ts +61 -0
- package/dist/dev/client/errorOverlay.ts +184 -0
- package/dist/dev/client/frameworkDetect.ts +63 -0
- package/dist/dev/client/handlers/angular.ts +572 -0
- package/dist/dev/client/handlers/angularRuntime.ts +226 -0
- package/dist/dev/client/handlers/html.ts +364 -0
- package/dist/dev/client/handlers/htmx.ts +278 -0
- package/dist/dev/client/handlers/react.ts +108 -0
- package/dist/dev/client/handlers/rebuild.ts +153 -0
- package/dist/dev/client/handlers/svelte.ts +334 -0
- package/dist/dev/client/handlers/vue.ts +292 -0
- package/dist/dev/client/headPatch.ts +233 -0
- package/dist/dev/client/hmrClient.ts +273 -0
- package/dist/dev/client/hmrState.ts +14 -0
- package/dist/dev/client/moduleVersions.ts +62 -0
- package/dist/dev/client/reactRefreshSetup.ts +32 -0
- package/dist/index.js +29 -33
- package/dist/index.js.map +5 -5
- package/dist/islands/index.js +6 -8
- package/dist/islands/index.js.map +4 -4
- package/dist/react/browser.js +7 -7
- package/dist/react/browser.js.map +2 -2
- package/dist/react/components/browser/index.js +101 -101
- package/dist/react/components/index.js +104 -104
- package/dist/react/components/index.js.map +2 -2
- package/dist/react/index.js +17 -19
- package/dist/react/index.js.map +4 -4
- package/dist/react/server.js +3 -3
- package/dist/react/server.js.map +3 -3
- package/dist/src/angular/components/constants.d.ts +75 -0
- package/dist/src/angular/components/defer-slot-templates.directive.d.ts +7 -0
- package/dist/src/angular/components/defer-slot.component.d.ts +5 -2
- package/dist/src/angular/components/image.component.d.ts +5 -2
- package/dist/src/angular/components/index.d.ts +4 -4
- package/dist/src/angular/components/stream-slot.component.d.ts +3 -0
- package/dist/src/constants.d.ts +1 -0
- package/dist/svelte/components/AwaitSlot.svelte +39 -0
- package/dist/svelte/components/AwaitSlot.svelte.d.ts +2 -0
- package/dist/svelte/components/Head.svelte +144 -0
- package/dist/svelte/components/Head.svelte.d.ts +2 -0
- package/dist/svelte/components/Image.svelte +164 -0
- package/dist/svelte/components/Image.svelte.d.ts +5 -0
- package/dist/svelte/components/Island.svelte +71 -0
- package/dist/svelte/components/Island.svelte.d.ts +5 -0
- package/dist/svelte/components/JsonLd.svelte +21 -0
- package/dist/svelte/components/JsonLd.svelte.d.ts +2 -0
- package/dist/svelte/components/StreamSlot.svelte +41 -0
- package/dist/svelte/components/StreamSlot.svelte.d.ts +2 -0
- package/dist/svelte/index.js +7 -9
- package/dist/svelte/index.js.map +4 -4
- package/dist/svelte/server.js +4 -4
- package/dist/svelte/server.js.map +3 -3
- package/dist/types/globals.d.ts +122 -0
- package/dist/vue/index.js +7 -9
- package/dist/vue/index.js.map +4 -4
- package/dist/vue/server.js +3 -3
- package/dist/vue/server.js.map +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,421 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
};
|