@reckona/mreact-compat 0.0.66 → 0.0.67

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.
@@ -0,0 +1,317 @@
1
+ import {
2
+ Suspense,
3
+ isReactCompatElement,
4
+ type ReactCompatElement,
5
+ type ReactCompatNode,
6
+ } from "./element.js";
7
+ import type { RootRuntime } from "./hooks.js";
8
+ import {
9
+ reportReactSuspenseServerError,
10
+ type RenderOptions,
11
+ } from "./hydration.js";
12
+ import type {
13
+ ReconcileNode,
14
+ ReconcileResult,
15
+ ReconcileSequence,
16
+ } from "./reconcile-types.js";
17
+ import { syncScopedChildNodes } from "./dom-children.js";
18
+ import { isThenable } from "./thenable.js";
19
+
20
+ interface ReactSuspenseBoundary {
21
+ start: Comment;
22
+ end: Comment;
23
+ previousNodes?: Node[];
24
+ consumed: number;
25
+ serverError?: {
26
+ message: string;
27
+ componentStack?: string;
28
+ };
29
+ }
30
+
31
+ let suspensePrimaryRenderDepth = 0;
32
+
33
+ export function isSuspensePrimaryRenderActive(): boolean {
34
+ return suspensePrimaryRenderDepth > 0;
35
+ }
36
+
37
+ export function reconcileSuspense(
38
+ parent: ParentNode,
39
+ previousNodes: readonly Node[],
40
+ element: ReactCompatElement,
41
+ runtime: RootRuntime,
42
+ path: string,
43
+ options: RenderOptions,
44
+ reconcileNode: ReconcileNode,
45
+ ): ReconcileResult {
46
+ const boundary = findReactSuspenseBoundary(previousNodes);
47
+ if (boundary?.serverError !== undefined) {
48
+ reportReactSuspenseServerError(
49
+ options,
50
+ path,
51
+ boundary.serverError.message,
52
+ boundary.serverError.componentStack,
53
+ );
54
+ }
55
+ const boundaryPreviousNodes =
56
+ boundary?.serverError === undefined
57
+ ? boundary?.previousNodes ?? previousNodes
58
+ : [];
59
+
60
+ try {
61
+ suspensePrimaryRenderDepth += 1;
62
+ let result: ReconcileResult;
63
+
64
+ try {
65
+ result = reconcileNode(
66
+ parent,
67
+ boundaryPreviousNodes,
68
+ element.props.children,
69
+ runtime,
70
+ `${path}.s`,
71
+ options,
72
+ );
73
+ } finally {
74
+ suspensePrimaryRenderDepth -= 1;
75
+ }
76
+
77
+ return consumeReactSuspenseBoundary(
78
+ boundary,
79
+ parent,
80
+ result,
81
+ );
82
+ } catch (error) {
83
+ if (!isThenable(error)) {
84
+ throw error;
85
+ }
86
+
87
+ error.then(
88
+ () => runtime.rerender(),
89
+ () => runtime.rerender(),
90
+ );
91
+ return consumeReactSuspenseBoundary(
92
+ boundary,
93
+ parent,
94
+ reconcileNode(
95
+ parent,
96
+ boundaryPreviousNodes,
97
+ element.props.fallback as ReactCompatNode,
98
+ runtime,
99
+ `${path}.fallback`,
100
+ options,
101
+ ),
102
+ );
103
+ }
104
+ }
105
+
106
+ export function reconcileSuspenseList(
107
+ parent: ParentNode,
108
+ previousNodes: readonly Node[],
109
+ element: ReactCompatElement,
110
+ runtime: RootRuntime,
111
+ path: string,
112
+ options: RenderOptions,
113
+ reconcileNode: ReconcileNode,
114
+ reconcileSequence: ReconcileSequence,
115
+ ): ReconcileResult {
116
+ if (
117
+ element.props.revealOrder !== "forwards" &&
118
+ element.props.revealOrder !== "backwards" &&
119
+ element.props.revealOrder !== "together"
120
+ ) {
121
+ return reconcileNode(
122
+ parent,
123
+ previousNodes,
124
+ element.props.children,
125
+ runtime,
126
+ `${path}.sl`,
127
+ options,
128
+ );
129
+ }
130
+
131
+ const children = Array.isArray(element.props.children)
132
+ ? element.props.children
133
+ : [element.props.children];
134
+
135
+ if (element.props.revealOrder === "together") {
136
+ return reconcileSequence(parent, previousNodes, children, runtime, `${path}.sl`, options);
137
+ }
138
+
139
+ const nodes: Node[] = [];
140
+ let previousIndex = 0;
141
+
142
+ if (element.props.revealOrder === "backwards") {
143
+ for (let index = children.length - 1; index >= 0; index -= 1) {
144
+ const child = children[index] as ReactCompatNode;
145
+ const result = reconcileNode(
146
+ parent,
147
+ previousNodes.slice(previousIndex),
148
+ child,
149
+ runtime,
150
+ `${path}.sl.${index}`,
151
+ options,
152
+ );
153
+ nodes.unshift(...result.nodes);
154
+ previousIndex += result.consumed;
155
+
156
+ if (isSuspenseFallback(child, result.nodes)) {
157
+ break;
158
+ }
159
+ }
160
+
161
+ return { nodes, consumed: previousIndex };
162
+ }
163
+
164
+ for (const [index, child] of children.entries()) {
165
+ const result = reconcileNode(
166
+ parent,
167
+ previousNodes.slice(previousIndex),
168
+ child,
169
+ runtime,
170
+ `${path}.sl.${index}`,
171
+ options,
172
+ );
173
+ nodes.push(...result.nodes);
174
+ previousIndex += result.consumed;
175
+
176
+ if (isSuspenseFallback(child, result.nodes)) {
177
+ break;
178
+ }
179
+ }
180
+
181
+ return { nodes, consumed: previousIndex };
182
+ }
183
+
184
+ function findReactSuspenseBoundary(
185
+ previousNodes: readonly Node[],
186
+ ): ReactSuspenseBoundary | undefined {
187
+ const startIndex = previousNodes.findIndex(isReactSuspenseStartComment);
188
+
189
+ if (startIndex < 0) {
190
+ return undefined;
191
+ }
192
+
193
+ let depth = 0;
194
+
195
+ for (let index = startIndex; index < previousNodes.length; index += 1) {
196
+ const node = previousNodes[index];
197
+
198
+ if (isReactSuspenseStartComment(node)) {
199
+ depth += 1;
200
+ continue;
201
+ }
202
+
203
+ if (isReactSuspenseEndComment(node)) {
204
+ depth -= 1;
205
+
206
+ if (depth === 0) {
207
+ const start = previousNodes[startIndex] as Comment;
208
+ const boundaryNodes = previousNodes.slice(startIndex + 1, index);
209
+ const boundary: ReactSuspenseBoundary = {
210
+ start,
211
+ end: node,
212
+ consumed: index - startIndex + 1,
213
+ ...readReactSuspenseServerError(start, boundaryNodes),
214
+ };
215
+
216
+ if (!isReactSuspenseErrorStartComment(start)) {
217
+ boundary.previousNodes = isReactSuspensePendingStartComment(start)
218
+ ? removeReactSuspensePendingTemplate(boundaryNodes)
219
+ : boundaryNodes;
220
+ }
221
+
222
+ return boundary;
223
+ }
224
+ }
225
+ }
226
+
227
+ return undefined;
228
+ }
229
+
230
+ function consumeReactSuspenseBoundary(
231
+ boundary: ReactSuspenseBoundary | undefined,
232
+ parent: ParentNode,
233
+ result: ReconcileResult,
234
+ ): ReconcileResult {
235
+ if (boundary === undefined) {
236
+ return result;
237
+ }
238
+
239
+ syncScopedChildNodes(parent, boundary.start, boundary.end, result.nodes);
240
+ boundary.start.parentNode?.removeChild(boundary.start);
241
+ boundary.end.parentNode?.removeChild(boundary.end);
242
+ return { nodes: result.nodes, consumed: boundary.consumed };
243
+ }
244
+
245
+ function isReactSuspenseStartComment(node: Node | undefined): node is Comment {
246
+ return node instanceof Comment && reactSuspenseStartComments.has(node.data);
247
+ }
248
+
249
+ function isReactSuspensePendingStartComment(node: Comment): boolean {
250
+ return node.data === "$?" || node.data === "$!";
251
+ }
252
+
253
+ function isReactSuspenseErrorStartComment(node: Comment): boolean {
254
+ return node.data === "$!";
255
+ }
256
+
257
+ function isReactSuspenseEndComment(node: Node | undefined): node is Comment {
258
+ return node instanceof Comment && node.data === "/$";
259
+ }
260
+
261
+ function removeReactSuspensePendingTemplate(nodes: readonly Node[]): Node[] {
262
+ const [firstNode, ...remainingNodes] = nodes;
263
+
264
+ return firstNode instanceof HTMLTemplateElement
265
+ ? remainingNodes
266
+ : [...nodes];
267
+ }
268
+
269
+ const reactSuspenseStartComments = new Set(["$", "$?", "$!"]);
270
+
271
+ function readReactSuspenseServerError(
272
+ start: Comment,
273
+ boundaryNodes: readonly Node[],
274
+ ): { serverError: { message: string; componentStack?: string } } | {} {
275
+ if (start.data !== "$!") {
276
+ return {};
277
+ }
278
+
279
+ const template = boundaryNodes[0];
280
+ const message =
281
+ template instanceof HTMLTemplateElement
282
+ ? template.getAttribute("data-msg")
283
+ : null;
284
+ const componentStack =
285
+ template instanceof HTMLTemplateElement
286
+ ? template.getAttribute("data-stck")
287
+ : null;
288
+
289
+ return {
290
+ serverError: {
291
+ message: message ?? "React Suspense server rendering error.",
292
+ ...(componentStack === null ? {} : { componentStack }),
293
+ },
294
+ };
295
+ }
296
+
297
+ function isSuspenseFallback(
298
+ node: ReactCompatNode,
299
+ renderedNodes: readonly Node[],
300
+ ): boolean {
301
+ return (
302
+ isReactCompatElement(node) &&
303
+ node.type === Suspense &&
304
+ renderedNodes.some((renderedNode) =>
305
+ renderedNode instanceof Element &&
306
+ renderedNode.tagName.toLowerCase() === getFallbackTagName(node),
307
+ )
308
+ );
309
+ }
310
+
311
+ function getFallbackTagName(node: ReactCompatElement): string | undefined {
312
+ const fallback = node.props.fallback;
313
+
314
+ return isReactCompatElement(fallback) && typeof fallback.type === "string"
315
+ ? fallback.type
316
+ : undefined;
317
+ }
@@ -0,0 +1,7 @@
1
+ export function isThenable(value: unknown): value is PromiseLike<unknown> {
2
+ return (
3
+ (typeof value === "object" || typeof value === "function") &&
4
+ value !== null &&
5
+ typeof (value as { then?: unknown }).then === "function"
6
+ );
7
+ }
@@ -0,0 +1,7 @@
1
+ export {
2
+ isDangerousHtmlAttribute,
3
+ isDangerousHtmlOptIn,
4
+ isSrcsetAttribute,
5
+ isUnsafeUrlAttribute,
6
+ isUrlAttribute,
7
+ } from "@reckona/mreact-shared/url-safety";