@yuneta/gobj-ui 7.26.49 → 7.26.51

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.
@@ -14,8 +14,14 @@
14
14
  * the Navigation API: a list and an index, each entry with a key that
15
15
  * a replaceState keeps and a push makes new, pushState truncating
16
16
  * what is ahead, and a traversal (back(), go()) run on a later task
17
- * from whatever entry is current THEN. Real browsers: qa-history
18
- * cases (h) to (l).
17
+ * from whatever entry is current THEN, announced by a
18
+ * `currententrychange` (with the entry it came FROM) before its
19
+ * popstate, as Chromium and Firefox do. Each entry belongs to a
20
+ * document: a reload makes every entry the new document's (both
21
+ * browsers say `sameDocument` of all of them after F5), and a
22
+ * cross-document navigation with replace keeps the entry's key
23
+ * for a NEW document, the entries under it staying the old one's.
24
+ * Real browsers: qa-history cases (h) to (n).
19
25
  *
20
26
  * Copyright (c) 2026, ArtGins.
21
27
  * All Rights Reserved.
@@ -40,7 +46,7 @@ let yuno = null;
40
46
  let key_seq = 0;
41
47
  const shells = [];
42
48
 
43
- const session = {entries: [], index: 0, gos: []};
49
+ const session = {entries: [], index: 0, gos: [], doc: 1, nav_listeners: []};
44
50
 
45
51
  function url_hash(url)
46
52
  {
@@ -51,14 +57,16 @@ function url_hash(url)
51
57
  function new_entry(state, hash)
52
58
  {
53
59
  key_seq++;
54
- return {state: state, hash: hash, key: `k${key_seq}`};
60
+ return {state: state, hash: hash, key: `k${key_seq}`, doc: session.doc};
55
61
  }
56
62
 
57
63
  function install_history_model(with_navigation)
58
64
  {
65
+ session.doc++;
59
66
  session.entries = [new_entry(null, window.location.hash)];
60
67
  session.index = 0;
61
68
  session.gos = [];
69
+ session.nav_listeners = [];
62
70
  window.history = {
63
71
  get state() {
64
72
  return session.entries[session.index].state;
@@ -88,6 +96,7 @@ function install_history_model(with_navigation)
88
96
  this.go(-1);
89
97
  },
90
98
  forward() {
99
+ this.go(1);
91
100
  },
92
101
  go(delta) {
93
102
  session.gos.push(delta);
@@ -103,12 +112,21 @@ function install_history_model(with_navigation)
103
112
  window.navigation = {
104
113
  get currentEntry() {
105
114
  const e = session.entries[session.index];
106
- return {key: e.key, index: session.index};
115
+ return {key: e.key, index: session.index, sameDocument: true};
107
116
  },
108
117
  entries() {
118
+ const doc = session.entries[session.index].doc;
109
119
  return session.entries.map((e, i) => ({
110
- key: e.key, index: i, url: `http://localhost/${e.hash}`
120
+ key: e.key, index: i, url: `http://localhost/${e.hash}`,
121
+ sameDocument: e.doc === doc
111
122
  }));
123
+ },
124
+ addEventListener(type, fn) {
125
+ session.nav_listeners.push({type, fn});
126
+ },
127
+ removeEventListener(type, fn) {
128
+ session.nav_listeners = session.nav_listeners.filter(
129
+ (l) => !(l.type === type && l.fn === fn));
112
130
  }
113
131
  };
114
132
  } else {
@@ -119,16 +137,24 @@ function install_history_model(with_navigation)
119
137
  function traverse_to(index)
120
138
  {
121
139
  const left = window.location.hash;
140
+ const from = {key: session.entries[session.index].key, index: session.index};
122
141
  session.index = index;
142
+ for(const l of session.nav_listeners.slice()) {
143
+ if(l.type === "currententrychange") {
144
+ l.fn({type: "currententrychange", navigationType: "traverse", from: from});
145
+ }
146
+ }
123
147
  window.location.hash = session.entries[index].hash;
124
- const ev = new Event("popstate");
125
- ev.state = window.history.state;
126
- window.dispatchEvent(ev);
148
+ /* The hashchange is queued BEFORE the popstate runs: a traversal
149
+ * the popstate handler starts lands after it (both browsers). */
127
150
  if(session.entries[index].hash !== left) {
128
151
  setTimeout(() => {
129
152
  window.dispatchEvent(new Event("hashchange"));
130
153
  }, 0);
131
154
  }
155
+ const ev = new Event("popstate");
156
+ ev.state = window.history.state;
157
+ window.dispatchEvent(ev);
132
158
  }
133
159
 
134
160
  function user_back()
@@ -136,6 +162,24 @@ function user_back()
136
162
  traverse_to(session.index - 1);
137
163
  }
138
164
 
165
+ function user_forward()
166
+ {
167
+ traverse_to(session.index + 1);
168
+ }
169
+
170
+ /* A navigation to another document with replace (location.replace):
171
+ * the entry keeps its key and is a new document's, and the entries
172
+ * under it are no longer the current document's. */
173
+ function cross_document_replace(hash)
174
+ {
175
+ session.doc++;
176
+ const e = session.entries[session.index];
177
+ e.doc = session.doc;
178
+ e.state = null;
179
+ e.hash = hash;
180
+ window.location.hash = hash;
181
+ }
182
+
139
183
  function next_task()
140
184
  {
141
185
  return new Promise(resolve => setTimeout(resolve, 0));
@@ -441,6 +485,145 @@ describe("a shell stopped with overlays up leaves its markers to the next one",
441
485
  expect(shell_warnings()).toEqual([]);
442
486
  });
443
487
 
488
+ /* location.replace() to another page and back to the app keeps
489
+ * the key of the entry it replaced: the new document sits on the
490
+ * key of a dead marker, and the entries under it are the earlier
491
+ * document's. Up to gobj-ui 7.26.50 the new shell took that key
492
+ * for dead and went back into them: the earlier document was
493
+ * loaded again. */
494
+ test("a dead key a later document sits on is not followed into the document before it", async () => {
495
+ window.location.hash = "#/prev";
496
+ install_history_model(true);
497
+ window.history.pushState(null, "", "#/home");
498
+ let shell = new_shell();
499
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
500
+ await settle();
501
+ yui_shell_confirm_yesno(shell, "leave?");
502
+ expect(session.entries.length).toBe(4);
503
+ stop_shell(shell);
504
+ cross_document_replace("#/elsewhere");
505
+ cross_document_replace("#/home");
506
+
507
+ shell = new_shell();
508
+ await settle();
509
+ expect(session.gos).toEqual([]);
510
+ expect(session.index).toBe(3);
511
+
512
+ /* Nor when a Back of the reader's lands on it. */
513
+ window.history.pushState(null, "", "#/prev");
514
+ window.dispatchEvent(new Event("hashchange"));
515
+ await settle();
516
+ user_back();
517
+ await settle();
518
+ expect(session.gos).toEqual([]);
519
+ expect(session.index).toBe(3);
520
+ expect(window.location.hash).toBe("#/home");
521
+ expect(shell_warnings()).toEqual([]);
522
+ });
523
+
524
+ /* A login page between the logout and the login, and the reader
525
+ * goes somewhere in it: an entry of the reader's over the dead
526
+ * markers, and the next shell starts on it. Up to gobj-ui 7.26.50
527
+ * nothing took them off: two dead Backs under it. */
528
+ test("a Back that lands on dead entries under a later one takes them off; Forward skips them", async () => {
529
+ window.location.hash = "#/prev";
530
+ install_history_model(true);
531
+ window.history.pushState(null, "", "#/home");
532
+ let shell = new_shell();
533
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
534
+ await settle();
535
+ yui_shell_confirm_yesno(shell, "log out?");
536
+ stop_shell(shell);
537
+ window.history.pushState(null, "", "#/prev");
538
+
539
+ shell = new_shell();
540
+ await settle();
541
+ expect(session.gos).toEqual([]);
542
+ expect(session.index).toBe(4);
543
+
544
+ user_back();
545
+ await settle();
546
+ expect(session.gos).toEqual([-2]);
547
+ expect(session.index).toBe(1);
548
+ expect(window.location.hash).toBe("#/home");
549
+
550
+ /* Forward goes over them to the reader's entry, not onto them,
551
+ * and not back down. */
552
+ user_forward();
553
+ await settle();
554
+ expect(session.gos).toEqual([-2, 2]);
555
+ expect(session.index).toBe(4);
556
+ expect(window.location.hash).toBe("#/prev");
557
+
558
+ user_back();
559
+ await settle();
560
+ expect(session.index).toBe(1);
561
+ const r = await count_backs_to_start(() => false);
562
+ expect(r).toEqual({backs: 1, dead: 0});
563
+ expect(window.location.hash).toBe("#/prev");
564
+ expect(shell_warnings()).toEqual([]);
565
+ });
566
+
567
+ /* The dead entries on another hash than the live one under them:
568
+ * the landing on them is not navigated -- it would fire the
569
+ * action again and bring the preferences up. */
570
+ test("a Back over dead entries of an action route does not run the action", async () => {
571
+ window.location.hash = "#/prev";
572
+ install_history_model(true);
573
+ window.history.pushState(null, "", "#/home");
574
+ let shell = new_shell();
575
+ window.history.pushState(null, "", "#/prefs");
576
+ window.dispatchEvent(new Event("hashchange"));
577
+ yui_shell_show_modal(shell, "the prefs", {dialog: true, title: "preferences"});
578
+ expect(session.entries.map(e => e.hash)).toEqual(["#/prev", "#/home", "#/prefs", "#/prefs"]);
579
+ /* An app that does not unpark: the URL stays on the action. */
580
+ stop_shell(shell);
581
+ window.history.pushState(null, "", "#/prev");
582
+ shell = new_shell();
583
+ await settle();
584
+ expect(session.gos).toEqual([]);
585
+ const actions = () => logged.filter((l) => l.msg.includes("EV_OPEN_PREFS")).length;
586
+ const before = actions();
587
+
588
+ user_back();
589
+ await settle();
590
+ expect(session.gos).toEqual([-2]);
591
+ expect(session.index).toBe(1);
592
+ expect(window.location.hash).toBe("#/home");
593
+ expect(actions()).toBe(before);
594
+ expect(shell_warnings()).toEqual([]);
595
+ });
596
+
597
+ test("a Forward onto dead entries with nothing of the reader's above them moves nothing", async () => {
598
+ window.location.hash = "#/prev";
599
+ install_history_model(true);
600
+ window.history.pushState(null, "", "#/home");
601
+ let shell = new_shell();
602
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
603
+ await settle();
604
+ yui_shell_confirm_yesno(shell, "log out?");
605
+ stop_shell(shell);
606
+ shell = new_shell();
607
+ await settle();
608
+ expect(session.gos).toEqual([-2]);
609
+ expect(session.index).toBe(1);
610
+
611
+ user_forward();
612
+ await settle();
613
+ user_forward();
614
+ await settle();
615
+ expect(session.gos).toEqual([-2]);
616
+ expect(session.index).toBe(3);
617
+
618
+ /* Back from there goes down to the reader's entry in ONE. */
619
+ user_back();
620
+ await settle();
621
+ expect(session.gos).toEqual([-2, -1]);
622
+ expect(session.index).toBe(1);
623
+ expect(window.location.hash).toBe("#/home");
624
+ expect(shell_warnings()).toEqual([]);
625
+ });
626
+
444
627
  test("without the Navigation API nothing moves, as before", async () => {
445
628
  window.location.hash = "#/home";
446
629
  install_history_model(false);