@voila.dev/replay 0.0.0-stage → 0.1.0

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/play.js ADDED
@@ -0,0 +1,903 @@
1
+ import {
2
+ FNV_START,
3
+ fnv,
4
+ join,
5
+ length
6
+ } from "./shared/chunk-6US53Q3A.js";
7
+
8
+ // src/play/mirror.ts
9
+ var SVG = "http://www.w3.org/2000/svg";
10
+ var MATHML = "http://www.w3.org/1998/Math/MathML";
11
+ var XLINK = "http://www.w3.org/1999/xlink";
12
+ var XML = "http://www.w3.org/XML/1998/namespace";
13
+ var ADDRESS = /^(src|href|poster|action|formaction|data|xlink:href|cite|background)$/i;
14
+ var CSS_URL = /url\(\s*(['"]?)(?!data:|#|blob:)([^'")]+)\1\s*\)/g;
15
+ var SAFE_URL = /^(https?:|data:image\/|blob:|#|mailto:|tel:)/i;
16
+ var INERT = /^(script|base|object|embed|portal)$/i;
17
+ var NODE = Node.prototype;
18
+ var firstChild = Object.getOwnPropertyDescriptor(NODE, "firstChild")?.get;
19
+ var nextSibling = Object.getOwnPropertyDescriptor(NODE, "nextSibling")?.get;
20
+ var localName = Object.getOwnPropertyDescriptor(Element.prototype, "localName")?.get;
21
+ var EL = Element.prototype;
22
+ var setAttr = (el, name, value) => EL.setAttribute.call(el, name, value);
23
+ var setAttrNS = (el, ns, name, value) => EL.setAttributeNS.call(el, ns, name, value);
24
+ var getAttr = (el, name) => EL.getAttribute.call(el, name);
25
+ var hasAttr = (el, name) => EL.hasAttribute.call(el, name);
26
+ var removeAttr = (el, name) => EL.removeAttribute.call(el, name);
27
+ var added = /* @__PURE__ */ new WeakSet();
28
+ function mirror(doc, hooks = {}) {
29
+ let nodes = [];
30
+ let sheets = [];
31
+ let stack = [];
32
+ let seq = 0;
33
+ let base = "";
34
+ let now = 0;
35
+ let size = null;
36
+ let pointer = null;
37
+ let drawn = /* @__PURE__ */ new Set();
38
+ let scrolled = /* @__PURE__ */ new Set();
39
+ let opened = /* @__PURE__ */ new Set();
40
+ let adopting = /* @__PURE__ */ new Set();
41
+ const win = () => doc.defaultView;
42
+ const absolute = (url, from = base) => {
43
+ try {
44
+ return new URL(url, from).href;
45
+ } catch {
46
+ return url;
47
+ }
48
+ };
49
+ const css = (text, from = base) => text.replace(CSS_URL, (_m, _q, url) => `url("${absolute(url, from)}")`);
50
+ const local = (el) => localName.call(el);
51
+ function add(n) {
52
+ const top = stack[stack.length - 1];
53
+ if (!top) return;
54
+ try {
55
+ top.p.insertBefore(n, top.a && top.a.parentNode === top.p ? top.a : null);
56
+ } catch {
57
+ try {
58
+ top.p.appendChild(n);
59
+ } catch {
60
+ }
61
+ }
62
+ }
63
+ function set(el, name, value) {
64
+ if (/^on/i.test(name) || name.toLowerCase() === "http-equiv") return;
65
+ const tag = local(el);
66
+ if (tag === "iframe" && (name === "src" || name === "srcdoc")) return;
67
+ if (ADDRESS.test(name)) {
68
+ value = absolute(value);
69
+ if (!SAFE_URL.test(value)) value = "about:blank";
70
+ } else if (name === "srcset")
71
+ value = value.split(",").map((part) => {
72
+ const [url, ...rest] = part.trim().split(/\s+/);
73
+ return [absolute(url || ""), ...rest].join(" ");
74
+ }).join(", ");
75
+ else if (name === "style") value = css(value);
76
+ else if (tag === "a" && name === "target") return;
77
+ try {
78
+ if (name.startsWith("xlink:")) setAttrNS(el, XLINK, name, value);
79
+ else if (name.startsWith("xml:")) setAttrNS(el, XML, name, value);
80
+ else setAttr(el, name, value);
81
+ } catch {
82
+ try {
83
+ setAttrNS(el, null, name, value);
84
+ } catch {
85
+ }
86
+ }
87
+ }
88
+ function element(name) {
89
+ const space = name.indexOf(" ");
90
+ try {
91
+ if (space < 0) {
92
+ if (INERT.test(name)) return doc.createElement(`replay-${name}`);
93
+ return doc.createElement(name);
94
+ }
95
+ const ns = name.slice(0, space);
96
+ const tag = name.slice(space + 1);
97
+ return doc.createElementNS(ns === "s" ? SVG : ns === "m" ? MATHML : ns, tag);
98
+ } catch {
99
+ return doc.createElement("replay-unknown");
100
+ }
101
+ }
102
+ function open(p) {
103
+ stack.push({ p, a: null });
104
+ }
105
+ function sheetText(node, text) {
106
+ if (!node || node.nodeType !== 1) return;
107
+ const el = node;
108
+ if (local(el) === "link") {
109
+ const from = absolute(getAttr(el, "href") || "");
110
+ const blob = URL.createObjectURL(new Blob([css(text, from)], { type: "text/css" }));
111
+ setAttr(el, "href", blob);
112
+ return;
113
+ }
114
+ let t = null;
115
+ for (let c = el.firstChild; c; c = c.nextSibling) if (c.nodeType === 3) t = c;
116
+ if (t) t.data = css(text);
117
+ else {
118
+ const node2 = doc.createTextNode(css(text));
119
+ added.add(node2);
120
+ el.appendChild(node2);
121
+ }
122
+ }
123
+ function constructed(k, text) {
124
+ const W = win();
125
+ if (!W?.CSSStyleSheet) return;
126
+ let s = sheets[k];
127
+ if (!s) {
128
+ try {
129
+ s = new W.CSSStyleSheet();
130
+ } catch {
131
+ return;
132
+ }
133
+ sheets[k] = s;
134
+ }
135
+ try {
136
+ s.replaceSync(css(text));
137
+ } catch {
138
+ }
139
+ }
140
+ function reset(mode) {
141
+ doc.open();
142
+ doc.write(mode === 1 ? "" : "<!DOCTYPE html>");
143
+ doc.close();
144
+ while (doc.firstChild) doc.removeChild(doc.firstChild);
145
+ nodes = [void 0, doc];
146
+ sheets = [];
147
+ drawn = /* @__PURE__ */ new Set();
148
+ scrolled = /* @__PURE__ */ new Set();
149
+ opened = /* @__PURE__ */ new Set();
150
+ adopting = /* @__PURE__ */ new Set();
151
+ seq = 1;
152
+ stack = [{ p: doc, a: null }];
153
+ }
154
+ function run(tape, from = 0, until = Number.POSITIVE_INFINITY) {
155
+ let i = from;
156
+ while (i < tape.length) {
157
+ const at = i;
158
+ const n = length(tape, i);
159
+ if (n < 0) throw new Error(`replay: bad op at ${i}`);
160
+ const op = tape[i];
161
+ const a = (k) => tape[at + k];
162
+ if (op === 48 /* TICK */ && now + a(1) > until) return at;
163
+ i += n;
164
+ switch (op) {
165
+ case 0 /* X */:
166
+ stack.pop();
167
+ break;
168
+ case 1 /* E */: {
169
+ const el = element(a(1));
170
+ for (let k = 3; k < n; k += 2) set(el, a(k), a(k + 1));
171
+ nodes[++seq] = el;
172
+ add(el);
173
+ open(el);
174
+ break;
175
+ }
176
+ case 2 /* T */: {
177
+ const top = stack[stack.length - 1]?.p;
178
+ const data = a(1);
179
+ const inStyle = top?.nodeType === 1 && local(top) === "style";
180
+ const t = doc.createTextNode(inStyle ? css(data) : data);
181
+ nodes[++seq] = t;
182
+ add(t);
183
+ break;
184
+ }
185
+ case 3 /* D */: {
186
+ let t;
187
+ try {
188
+ t = doc.implementation.createDocumentType(a(1), a(2), a(3));
189
+ } catch {
190
+ t = doc.createComment("doctype");
191
+ }
192
+ nodes[++seq] = t;
193
+ add(t);
194
+ break;
195
+ }
196
+ case 4 /* B */: {
197
+ const el = element(a(1));
198
+ setAttr(
199
+ el,
200
+ "style",
201
+ `display:inline-block;width:${+a(2) || 0}px;height:${+a(3) || 0}px;background:repeating-linear-gradient(45deg,#0001 0 8px,#0002 8px 16px)`
202
+ );
203
+ setAttr(el, "data-replay-blocked", "");
204
+ nodes[++seq] = el;
205
+ add(el);
206
+ break;
207
+ }
208
+ case 5 /* TPL */: {
209
+ const top = stack[stack.length - 1]?.p;
210
+ const content = top && "content" in top ? top.content : doc.createDocumentFragment();
211
+ nodes[++seq] = content;
212
+ open(content);
213
+ break;
214
+ }
215
+ case 6 /* SHADOW */: {
216
+ const host = stack[stack.length - 1]?.p;
217
+ let root;
218
+ try {
219
+ root = host.shadowRoot || host.attachShadow({ mode: "open" });
220
+ while (root.firstChild) root.removeChild(root.firstChild);
221
+ } catch {
222
+ root = doc.createDocumentFragment();
223
+ }
224
+ nodes[++seq] = root;
225
+ open(root);
226
+ break;
227
+ }
228
+ case 7 /* FRAME */: {
229
+ const frame = stack[stack.length - 1]?.p;
230
+ let inner = null;
231
+ try {
232
+ inner = frame?.contentDocument || null;
233
+ if (inner) while (inner.firstChild) inner.removeChild(inner.firstChild);
234
+ } catch {
235
+ }
236
+ const node = inner || doc.createDocumentFragment();
237
+ nodes[++seq] = node;
238
+ open(node);
239
+ break;
240
+ }
241
+ case 8 /* R */: {
242
+ const node = nodes[a(1)];
243
+ if (node) add(node);
244
+ break;
245
+ }
246
+ case 16 /* INS */: {
247
+ const p = nodes[a(1)];
248
+ const anchor = a(2) ? nodes[a(2)] : null;
249
+ stack.push({ p: p || doc.createDocumentFragment(), a: anchor || null });
250
+ break;
251
+ }
252
+ case 17 /* DEL */: {
253
+ const node = nodes[a(1)];
254
+ node?.parentNode?.removeChild(node);
255
+ break;
256
+ }
257
+ case 18 /* ATTR */: {
258
+ const el = nodes[a(1)];
259
+ const name = a(2);
260
+ const value = a(3);
261
+ if (!el || el.nodeType !== 1) break;
262
+ if (value === null) {
263
+ try {
264
+ removeAttr(el, name);
265
+ } catch {
266
+ }
267
+ } else set(el, name, value);
268
+ break;
269
+ }
270
+ case 19 /* TEXT */: {
271
+ const t = nodes[a(1)];
272
+ if (!t || t.nodeType !== 3) break;
273
+ const data = a(2);
274
+ t.data = t.parentElement && local(t.parentElement) === "style" ? css(data) : data;
275
+ break;
276
+ }
277
+ case 20 /* VAL */: {
278
+ const el = nodes[a(1)];
279
+ const v = a(2);
280
+ if (!el || el.nodeType !== 1) break;
281
+ try {
282
+ if (local(el) === "option") {
283
+ const select = el.parentElement?.closest("select");
284
+ if (!v && el.selected && select && !select.multiple) select.selectedIndex = -1;
285
+ else el.selected = !!v;
286
+ } else if (typeof v === "number") el.checked = !!v;
287
+ else el.value = v;
288
+ } catch {
289
+ }
290
+ break;
291
+ }
292
+ case 21 /* OPEN */: {
293
+ const el = nodes[a(1)];
294
+ const state = a(2);
295
+ if (!el || el.nodeType !== 1) break;
296
+ opened.add(el);
297
+ try {
298
+ if (local(el) === "dialog") {
299
+ if (el.open) el.close();
300
+ if (state === 2 && el.isConnected) el.showModal();
301
+ else if (state === 1) el.show();
302
+ } else if ("showPopover" in el) {
303
+ const p = el;
304
+ state ? p.showPopover() : p.hidePopover();
305
+ }
306
+ } catch {
307
+ }
308
+ break;
309
+ }
310
+ case 32 /* SHEET */: {
311
+ const id = a(1);
312
+ if (id) sheetText(nodes[id], a(3));
313
+ else constructed(a(2), a(3));
314
+ break;
315
+ }
316
+ case 33 /* RULE */: {
317
+ const s = sheets[a(1)];
318
+ const path = String(a(2)).split(".").map(Number);
319
+ const text = a(3);
320
+ try {
321
+ let list = s;
322
+ for (const k of path.slice(0, -1)) list = list?.cssRules[k];
323
+ const last = path[path.length - 1];
324
+ if (text === null) list?.deleteRule(last);
325
+ else list?.insertRule(css(text), last);
326
+ } catch {
327
+ }
328
+ break;
329
+ }
330
+ case 34 /* ADOPT */: {
331
+ const root = nodes[a(1)];
332
+ const list = [];
333
+ for (let k = 3; k < n; k++) {
334
+ const s = sheets[a(k)];
335
+ if (s) list.push(s);
336
+ }
337
+ try {
338
+ if (root && "adoptedStyleSheets" in root) {
339
+ root.adoptedStyleSheets = list;
340
+ adopting.add(root);
341
+ }
342
+ } catch {
343
+ }
344
+ break;
345
+ }
346
+ case 48 /* TICK */:
347
+ now += a(1);
348
+ break;
349
+ case 49 /* MOVE */:
350
+ pointer = [a(1), a(2)];
351
+ hooks.pointer?.(a(1), a(2));
352
+ break;
353
+ case 50 /* TAP */:
354
+ hooks.tap?.(a(1), a(3), a(4));
355
+ break;
356
+ case 51 /* SCROLL */: {
357
+ const id = a(1);
358
+ const x = a(2);
359
+ const y = a(3);
360
+ try {
361
+ if (nodes[id]) scrolled.add(nodes[id]);
362
+ if (id === 1) win()?.scrollTo(x, y);
363
+ else {
364
+ const el = nodes[id];
365
+ if (el?.nodeType === 9) el.defaultView?.scrollTo(x, y);
366
+ else if (el?.nodeType === 1) {
367
+ el.scrollLeft = x;
368
+ el.scrollTop = y;
369
+ }
370
+ }
371
+ } catch {
372
+ }
373
+ break;
374
+ }
375
+ case 52 /* SIZE */:
376
+ size = [a(1), a(2)];
377
+ hooks.size?.(a(1), a(2));
378
+ break;
379
+ case 53 /* FOCUS */: {
380
+ const el = a(1) ? nodes[a(1)] : void 0;
381
+ hooks.focus?.(el?.nodeType === 1 ? el : null);
382
+ break;
383
+ }
384
+ case 96 /* META */:
385
+ base = a(2);
386
+ now = a(3);
387
+ reset(a(6));
388
+ size = [a(4), a(5)];
389
+ hooks.size?.(a(4), a(5));
390
+ break;
391
+ case 97 /* PRINT */: {
392
+ const [count, hash] = fingerprint(doc);
393
+ hooks.print?.(count === a(1) && hash === a(2), now, `${a(1)} n\u0153uds enregistr\xE9s, ${count} rejou\xE9s`);
394
+ break;
395
+ }
396
+ case 128 /* SHOT */: {
397
+ const el = nodes[a(2)];
398
+ const shot = a(3);
399
+ if (!el || typeof el.getContext !== "function" || !/^data:image\//.test(shot)) break;
400
+ drawn.add(el);
401
+ const image = new Image();
402
+ image.onload = () => {
403
+ const c = el.getContext("2d");
404
+ c?.clearRect(0, 0, el.width, el.height);
405
+ c?.drawImage(image, 0, 0, el.width, el.height);
406
+ };
407
+ image.src = shot;
408
+ break;
409
+ }
410
+ }
411
+ }
412
+ return i;
413
+ }
414
+ function save() {
415
+ const holder = blank(doc);
416
+ const { map, frames } = copy(nodes, doc, holder, (f) => f.contentDocument);
417
+ const adopted = [];
418
+ const pixels = [];
419
+ const shown = [];
420
+ const scrolls = [];
421
+ for (const n of adopting) {
422
+ const at = map.get(n);
423
+ const list = n.adoptedStyleSheets;
424
+ if (at && list?.length) adopted.push([at, list.map((x) => sheets.indexOf(x))]);
425
+ }
426
+ for (const n of scrolled) {
427
+ const c = map.get(n);
428
+ if (!c) continue;
429
+ if (n.nodeType === 9) {
430
+ const w = n.defaultView;
431
+ if (w) scrolls.push([c, w.scrollX, w.scrollY]);
432
+ } else if (n.nodeType === 1) scrolls.push([c, n.scrollLeft, n.scrollTop]);
433
+ }
434
+ for (const n of opened) {
435
+ const c = map.get(n);
436
+ try {
437
+ if (c && local(n) === "dialog" && n.matches(":modal")) shown.push([c, 2]);
438
+ else if (c && n.matches(":popover-open")) shown.push([c, 1]);
439
+ } catch {
440
+ }
441
+ }
442
+ for (const n of drawn) {
443
+ const c = map.get(n);
444
+ const from = n;
445
+ if (!c || !from.width || !from.height) continue;
446
+ const pixel = doc.createElement("canvas");
447
+ pixel.width = from.width;
448
+ pixel.height = from.height;
449
+ try {
450
+ pixel.getContext("2d")?.drawImage(from, 0, 0);
451
+ pixels.push([c, pixel]);
452
+ } catch {
453
+ }
454
+ }
455
+ return {
456
+ holder,
457
+ nodes: nodes.map((n) => n && map.get(n)),
458
+ frames,
459
+ sheets: sheets.map((s) => s && rulesOf(s)),
460
+ adopted,
461
+ pixels,
462
+ shown,
463
+ scrolls,
464
+ seq,
465
+ base,
466
+ now,
467
+ mode: doc.compatMode === "BackCompat" ? 1 : 0,
468
+ size,
469
+ pointer
470
+ };
471
+ }
472
+ function restore(cp) {
473
+ reset(cp.mode);
474
+ const { map } = copy(cp.nodes, cp.holder, doc, (f) => cp.frames.get(f) || null);
475
+ nodes = cp.nodes.map((n) => n && map.get(n));
476
+ seq = cp.seq;
477
+ base = cp.base;
478
+ now = cp.now;
479
+ cp.sheets.forEach((text, k) => {
480
+ if (text !== void 0) constructed(k, text);
481
+ });
482
+ for (const [root, list] of cp.adopted) {
483
+ const r = map.get(root);
484
+ try {
485
+ if (r && "adoptedStyleSheets" in r) {
486
+ r.adoptedStyleSheets = list.map((k) => sheets[k]).filter(Boolean);
487
+ adopting.add(r);
488
+ }
489
+ } catch {
490
+ }
491
+ }
492
+ for (const [node, pixel] of cp.pixels) {
493
+ const el = map.get(node);
494
+ try {
495
+ if (!el) continue;
496
+ el.getContext("2d")?.drawImage(pixel, 0, 0);
497
+ drawn.add(el);
498
+ } catch {
499
+ }
500
+ }
501
+ for (const [node, state] of cp.shown) {
502
+ const el = map.get(node);
503
+ try {
504
+ if (!el?.isConnected) continue;
505
+ opened.add(el);
506
+ if (state === 2) {
507
+ if (el.open) el.close();
508
+ el.showModal();
509
+ } else el.showPopover();
510
+ } catch {
511
+ }
512
+ }
513
+ for (const [node, x, y] of cp.scrolls) {
514
+ const n = map.get(node);
515
+ if (n) scrolled.add(n);
516
+ try {
517
+ if (n?.nodeType === 9) n.defaultView?.scrollTo(x, y);
518
+ else if (n?.nodeType === 1) {
519
+ n.scrollLeft = x;
520
+ n.scrollTop = y;
521
+ }
522
+ } catch {
523
+ }
524
+ }
525
+ size = cp.size && [cp.size[0], cp.size[1]];
526
+ pointer = cp.pointer && [cp.pointer[0], cp.pointer[1]];
527
+ if (size) hooks.size?.(size[0], size[1]);
528
+ if (pointer) hooks.pointer?.(pointer[0], pointer[1]);
529
+ }
530
+ return {
531
+ run,
532
+ save,
533
+ restore,
534
+ get rest() {
535
+ return stack.length === 1;
536
+ },
537
+ get time() {
538
+ return now;
539
+ },
540
+ get next() {
541
+ return seq + 1;
542
+ },
543
+ get url() {
544
+ return base;
545
+ },
546
+ get nodes() {
547
+ return nodes;
548
+ }
549
+ };
550
+ }
551
+ function blank(from) {
552
+ const d = from.implementation.createHTMLDocument("");
553
+ while (d.firstChild) d.removeChild(d.firstChild);
554
+ return d;
555
+ }
556
+ function rulesOf(sheet) {
557
+ let text = "";
558
+ try {
559
+ for (const rule of Array.from(sheet.cssRules)) text += rule.cssText;
560
+ } catch {
561
+ }
562
+ return text;
563
+ }
564
+ function copy(from, root, into, inner) {
565
+ const map = /* @__PURE__ */ new Map();
566
+ const frames = /* @__PURE__ */ new Map();
567
+ const owner = /* @__PURE__ */ new Map();
568
+ for (const n of from)
569
+ if (n?.nodeType === 1 && localName.call(n) === "template")
570
+ owner.set(n.content, n);
571
+ const children = (s, d) => {
572
+ for (let c = firstChild.call(s); c; c = nextSibling.call(c)) node(c, d);
573
+ };
574
+ function node(s, parent) {
575
+ let c;
576
+ try {
577
+ const t = s;
578
+ c = s.nodeType === 10 ? into.implementation.createDocumentType(t.name, t.publicId, t.systemId) : into.importNode(s, false);
579
+ } catch {
580
+ return;
581
+ }
582
+ map.set(s, c);
583
+ if (added.has(s)) added.add(c);
584
+ if (parent)
585
+ try {
586
+ parent.appendChild(c);
587
+ } catch {
588
+ }
589
+ if (s.nodeType !== 1) return;
590
+ const el = s;
591
+ const to = c;
592
+ const tag = localName.call(el);
593
+ if (tag === "template" && "content" in el) {
594
+ const content = to.content;
595
+ map.set(el.content, content);
596
+ children(el.content, content);
597
+ }
598
+ const shadow = el.shadowRoot;
599
+ if (shadow)
600
+ try {
601
+ const r = to.shadowRoot || to.attachShadow({ mode: "open" });
602
+ map.set(shadow, r);
603
+ children(shadow, r);
604
+ } catch {
605
+ }
606
+ if (tag === "iframe") {
607
+ const i = inner(el);
608
+ if (i) {
609
+ let d = null;
610
+ try {
611
+ d = to.contentDocument;
612
+ } catch {
613
+ }
614
+ if (d) while (d.firstChild) d.removeChild(d.firstChild);
615
+ else d = blank(into);
616
+ map.set(i, d);
617
+ frames.set(to, d);
618
+ children(i, d);
619
+ }
620
+ }
621
+ children(el, to);
622
+ const a = el;
623
+ const b = to;
624
+ try {
625
+ if (tag === "input" || tag === "textarea") {
626
+ if (b.value !== a.value && a.type !== "file") b.value = a.value;
627
+ if (b.checked !== a.checked) b.checked = a.checked;
628
+ } else if (tag === "select") {
629
+ if (a.multiple || a.size > 1) {
630
+ for (let k = 0; k < a.options.length; k++)
631
+ if (b.options[k] && b.options[k].selected !== a.options[k].selected) b.options[k].selected = a.options[k].selected;
632
+ } else if (b.selectedIndex !== a.selectedIndex) b.selectedIndex = a.selectedIndex;
633
+ } else if (tag === "option" && !el.parentElement?.closest("select") && b.selected !== a.selected)
634
+ b.selected = a.selected;
635
+ } catch {
636
+ }
637
+ }
638
+ map.set(root, into);
639
+ children(root, into);
640
+ for (const n of from) {
641
+ if (!n || map.has(n)) continue;
642
+ let top = n;
643
+ for (; ; ) {
644
+ const up = top.parentNode || (top.nodeType === 11 ? top.host : null) || owner.get(top);
645
+ if (!up) break;
646
+ top = up;
647
+ }
648
+ if (map.has(top)) continue;
649
+ if (top.nodeType === 9 || top.nodeType === 11) {
650
+ const d = top.nodeType === 9 ? blank(into) : into.createDocumentFragment();
651
+ map.set(top, d);
652
+ children(top, d);
653
+ } else node(top, null);
654
+ }
655
+ return { map, frames };
656
+ }
657
+ function fingerprint(root) {
658
+ let count = 0;
659
+ let h = FNV_START;
660
+ const walk = (n) => {
661
+ for (let c = firstChild.call(n); c; c = nextSibling.call(c)) {
662
+ const type = c.nodeType;
663
+ if (type !== 1 && type !== 3 && type !== 10 || added.has(c)) continue;
664
+ count++;
665
+ h = fnv(h, type);
666
+ if (type === 1) {
667
+ const el = c;
668
+ let tag = localName.call(el);
669
+ if (tag.startsWith("replay-") && tag !== "replay-unknown") tag = tag.slice(7);
670
+ h = fnv(h, tag.length);
671
+ h = fnv(h, tag.charCodeAt(0));
672
+ if (hasAttr(el, "data-replay-blocked")) continue;
673
+ if (tag === "template" && "content" in el) walk(el.content);
674
+ walk(el);
675
+ }
676
+ }
677
+ };
678
+ walk(root);
679
+ return [count, h];
680
+ }
681
+
682
+ // src/play/index.ts
683
+ var IDLE_MS = 5e3;
684
+ var MAX_CHECKPOINTS = 64;
685
+ function createPlayer(options) {
686
+ const { root } = options;
687
+ let speed = options.speed ?? 1;
688
+ let skipIdle = options.skipIdle ?? true;
689
+ let every = Math.max(1, options.every ?? 1e4);
690
+ const stage = root.ownerDocument.createElement("div");
691
+ stage.style.cssText = "position:relative;overflow:hidden;transform-origin:0 0";
692
+ const iframe = root.ownerDocument.createElement("iframe");
693
+ iframe.setAttribute("sandbox", "allow-same-origin");
694
+ iframe.setAttribute("referrerpolicy", "no-referrer");
695
+ iframe.style.cssText = "border:0;display:block;background:#fff";
696
+ const cursor = root.ownerDocument.createElement("div");
697
+ cursor.style.cssText = "position:absolute;left:0;top:0;width:14px;height:14px;margin:-7px 0 0 -7px;border-radius:50%;background:#e5484d;box-shadow:0 0 0 3px #e5484d55;pointer-events:none;transition:transform 60ms linear;display:none";
698
+ stage.append(iframe, cursor);
699
+ root.append(stage);
700
+ const doc = iframe.contentDocument;
701
+ const listeners = {};
702
+ function emit(event, payload) {
703
+ const set = listeners[event];
704
+ set?.forEach((h) => {
705
+ try {
706
+ h(payload);
707
+ } catch {
708
+ }
709
+ });
710
+ }
711
+ let size = { width: 1280, height: 720 };
712
+ const prints = [];
713
+ let m = make();
714
+ function make() {
715
+ return mirror(doc, {
716
+ pointer(x, y) {
717
+ cursor.style.display = "block";
718
+ cursor.style.transform = `translate(${x}px,${y}px)`;
719
+ },
720
+ tap(kind) {
721
+ if (kind === 1) cursor.style.background = "#30a46c";
722
+ else cursor.style.background = "#e5484d";
723
+ },
724
+ size(width, height) {
725
+ size = { width, height };
726
+ iframe.style.width = stage.style.width = `${width}px`;
727
+ iframe.style.height = stage.style.height = `${height}px`;
728
+ emit("size", size);
729
+ },
730
+ print(ok, at2, detail) {
731
+ const p = { ok, at: at2 - start, detail };
732
+ if (!prints.some((q) => q.at === p.at)) prints.push(p);
733
+ emit("print", p);
734
+ }
735
+ });
736
+ }
737
+ const joined = join(options.chunks);
738
+ const tape = joined.tape;
739
+ let state = joined.state;
740
+ const gapsAt = [...joined.gaps];
741
+ let metas = [];
742
+ let start = 0;
743
+ let end = 0;
744
+ function index() {
745
+ metas = [];
746
+ let now = 0;
747
+ for (let i = 0; i < tape.length; ) {
748
+ const n = length(tape, i);
749
+ if (n < 0) break;
750
+ const op = tape[i];
751
+ if (op === 96 /* META */) {
752
+ now = tape[i + 3];
753
+ if (!metas.length) start = now;
754
+ metas.push({ index: i, time: now });
755
+ } else if (op === 48 /* TICK */) now += tape[i + 1];
756
+ i += n;
757
+ }
758
+ end = now;
759
+ }
760
+ index();
761
+ let at = 0;
762
+ let clock = 0;
763
+ let playing = false;
764
+ let frame = 0;
765
+ let lastFrame = 0;
766
+ const checkpoints = [];
767
+ const after = (t) => start + (Math.floor((t - start) / every) + 1) * every;
768
+ function keep() {
769
+ if (!m.rest || checkpoints.some((c2) => c2.index === at)) return;
770
+ const c = { index: at, time: m.time, cp: m.save() };
771
+ let k = checkpoints.length;
772
+ while (k > 0 && checkpoints[k - 1].index > at) k--;
773
+ checkpoints.splice(k, 0, c);
774
+ if (checkpoints.length > MAX_CHECKPOINTS) {
775
+ every *= 2;
776
+ for (let i = checkpoints.length - 1; i >= 0; i -= 2) checkpoints.splice(i, 1);
777
+ }
778
+ }
779
+ function runTo(ms) {
780
+ const until = start + ms;
781
+ for (let step = after(Math.max(m.time, start)); step < until; ) {
782
+ at = m.run(tape, at, step);
783
+ if (at >= tape.length || tape[at] !== 48 /* TICK */) break;
784
+ keep();
785
+ step = after(m.time + tape[at + 1]);
786
+ }
787
+ at = m.run(tape, at, until);
788
+ }
789
+ function seek(ms) {
790
+ ms = Math.max(0, Math.min(ms, end - start));
791
+ let from;
792
+ for (const x of metas) if (x.time - start <= ms && (!from || x.index > from.index)) from = x;
793
+ for (const x of checkpoints) if (x.time - start <= ms && (!from || x.index > from.index)) from = x;
794
+ if (from && (ms < clock || at === 0 || from.index > at)) {
795
+ m = make();
796
+ if (from.cp) m.restore(from.cp);
797
+ at = from.index;
798
+ } else if (!from && (ms < clock || at === 0)) return;
799
+ clock = ms;
800
+ runTo(ms);
801
+ emit("time", clock);
802
+ }
803
+ function loop(t) {
804
+ if (!playing) return;
805
+ const dt = lastFrame ? Math.min(t - lastFrame, 250) : 0;
806
+ lastFrame = t;
807
+ let next = clock + dt * speed;
808
+ if (skipIdle && at < tape.length && tape[at] === 48 /* TICK */) {
809
+ const gap = tape[at + 1];
810
+ if (gap > IDLE_MS) next = Math.max(next, m.time - start + gap - 1e3);
811
+ }
812
+ clock = Math.min(next, end - start);
813
+ runTo(clock);
814
+ emit("time", clock);
815
+ if (clock >= end - start) {
816
+ playing = false;
817
+ emit("end", void 0);
818
+ return;
819
+ }
820
+ frame = requestAnimationFrame(loop);
821
+ }
822
+ seek(0);
823
+ return {
824
+ iframe,
825
+ document: doc,
826
+ get duration() {
827
+ return end - start;
828
+ },
829
+ get time() {
830
+ return clock;
831
+ },
832
+ get size() {
833
+ return size;
834
+ },
835
+ get playing() {
836
+ return playing;
837
+ },
838
+ get gaps() {
839
+ const out = [];
840
+ let now = 0;
841
+ let g = 0;
842
+ for (let i = 0; i < tape.length && g < gapsAt.length; ) {
843
+ while (g < gapsAt.length && gapsAt[g].index <= i) {
844
+ out.push({ at: Math.max(0, now - start), ms: gapsAt[g].ms });
845
+ g++;
846
+ }
847
+ const n = length(tape, i);
848
+ if (n < 0) break;
849
+ if (tape[i] === 96 /* META */) now = tape[i + 3];
850
+ else if (tape[i] === 48 /* TICK */) now += tape[i + 1];
851
+ i += n;
852
+ }
853
+ for (; g < gapsAt.length; g++)
854
+ out.push({ at: Math.max(0, now - start), ms: gapsAt[g].ms });
855
+ return out;
856
+ },
857
+ prints,
858
+ play() {
859
+ if (playing) return;
860
+ if (clock >= end - start) seek(0);
861
+ playing = true;
862
+ lastFrame = 0;
863
+ emit("play", void 0);
864
+ frame = requestAnimationFrame(loop);
865
+ },
866
+ pause() {
867
+ playing = false;
868
+ cancelAnimationFrame(frame);
869
+ emit("pause", void 0);
870
+ },
871
+ seek,
872
+ setSpeed(s) {
873
+ speed = s;
874
+ },
875
+ setSkipIdle(on) {
876
+ skipIdle = on;
877
+ },
878
+ append(chunks) {
879
+ const more = join(chunks, tape, state);
880
+ state = more.state;
881
+ gapsAt.push(...more.gaps);
882
+ index();
883
+ runTo(clock);
884
+ },
885
+ on(event, handler) {
886
+ let set = listeners[event];
887
+ if (!set) listeners[event] = set = /* @__PURE__ */ new Set();
888
+ set.add(handler);
889
+ return () => set?.delete(handler);
890
+ },
891
+ destroy() {
892
+ playing = false;
893
+ cancelAnimationFrame(frame);
894
+ stage.remove();
895
+ }
896
+ };
897
+ }
898
+ export {
899
+ createPlayer,
900
+ fingerprint,
901
+ join,
902
+ mirror
903
+ };