@yuneta/gobj-ui 7.26.16 → 7.26.17

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.
@@ -2563,12 +2563,13 @@ function push_overlay_marker(gobj, entry) {
2563
2563
  let priv = gobj.priv;
2564
2564
  entry.below_hash = window.location.hash;
2565
2565
  let below = window.history.state;
2566
- if (below === null || typeof below === "object" && !Array.isArray(below)) try {
2566
+ let plain = below !== null && typeof below === "object" && (Object.getPrototypeOf(below) === Object.prototype || Object.getPrototypeOf(below) === null);
2567
+ if (below === null || plain) try {
2567
2568
  window.history.replaceState(Object.assign({}, below, { __yui_below__: entry.id }), "");
2568
2569
  } catch (e) {
2569
2570
  (0, _yuneta_gobj_js.log_warning)(`C_YUI_SHELL: stamping the entry under an overlay failed: ${e}`);
2570
2571
  }
2571
- else (0, _yuneta_gobj_js.log_warning)("C_YUI_SHELL: the entry under an overlay holds a history state that is not an object, it cannot be stamped: Back will not close the overlay");
2572
+ else (0, _yuneta_gobj_js.log_warning)("C_YUI_SHELL: the entry under an overlay holds a history state that is not a plain object, it cannot be stamped: Back will not close the overlay");
2572
2573
  try {
2573
2574
  window.history.pushState({ __yui_overlay__: entry.id }, "");
2574
2575
  } catch (e) {
@@ -2621,11 +2622,12 @@ function clear_deferred_timer(gobj) {
2621
2622
  function push_pending_markers(gobj) {
2622
2623
  let priv = gobj.priv;
2623
2624
  let pending = priv.pending_markers.splice(0);
2625
+ let pushed = [];
2624
2626
  for (let entry of pending) {
2625
2627
  entry.marker_pending = false;
2626
2628
  if (entry.dismissed || priv.overlay_stack.indexOf(entry) < 0) continue;
2627
2629
  entry.hash = window.location.hash;
2628
- push_overlay_marker(gobj, entry);
2630
+ if (push_overlay_marker(gobj, entry)) pushed.push(entry.id);
2629
2631
  }
2630
2632
  clear_deferred_timer(gobj);
2631
2633
  if (priv.expected_pops === 0) priv.expected_pop_ids = [];
@@ -2634,6 +2636,7 @@ function push_pending_markers(gobj) {
2634
2636
  priv.deferred_route = "";
2635
2637
  yui_shell_navigate(gobj, route);
2636
2638
  }
2639
+ return pushed;
2637
2640
  }
2638
2641
  function overlay_dismissed(gobj, entry) {
2639
2642
  let priv = gobj.priv;
@@ -2786,7 +2789,8 @@ function ac_back_wait_timeout(gobj, event, kw, src) {
2786
2789
  priv.expected_pop_hash = "";
2787
2790
  priv.expected_pop_ids = [];
2788
2791
  priv.restore_hash = "";
2789
- push_pending_markers(gobj);
2792
+ let pushed = push_pending_markers(gobj);
2793
+ given_up.ids.push(...pushed);
2790
2794
  priv.given_up = given_up;
2791
2795
  return 0;
2792
2796
  }
@@ -2558,12 +2558,13 @@ function push_overlay_marker(gobj, entry) {
2558
2558
  let priv = gobj.priv;
2559
2559
  entry.below_hash = window.location.hash;
2560
2560
  let below = window.history.state;
2561
- if (below === null || typeof below === "object" && !Array.isArray(below)) try {
2561
+ let plain = below !== null && typeof below === "object" && (Object.getPrototypeOf(below) === Object.prototype || Object.getPrototypeOf(below) === null);
2562
+ if (below === null || plain) try {
2562
2563
  window.history.replaceState(Object.assign({}, below, { __yui_below__: entry.id }), "");
2563
2564
  } catch (e) {
2564
2565
  log_warning(`C_YUI_SHELL: stamping the entry under an overlay failed: ${e}`);
2565
2566
  }
2566
- else log_warning("C_YUI_SHELL: the entry under an overlay holds a history state that is not an object, it cannot be stamped: Back will not close the overlay");
2567
+ else log_warning("C_YUI_SHELL: the entry under an overlay holds a history state that is not a plain object, it cannot be stamped: Back will not close the overlay");
2567
2568
  try {
2568
2569
  window.history.pushState({ __yui_overlay__: entry.id }, "");
2569
2570
  } catch (e) {
@@ -2616,11 +2617,12 @@ function clear_deferred_timer(gobj) {
2616
2617
  function push_pending_markers(gobj) {
2617
2618
  let priv = gobj.priv;
2618
2619
  let pending = priv.pending_markers.splice(0);
2620
+ let pushed = [];
2619
2621
  for (let entry of pending) {
2620
2622
  entry.marker_pending = false;
2621
2623
  if (entry.dismissed || priv.overlay_stack.indexOf(entry) < 0) continue;
2622
2624
  entry.hash = window.location.hash;
2623
- push_overlay_marker(gobj, entry);
2625
+ if (push_overlay_marker(gobj, entry)) pushed.push(entry.id);
2624
2626
  }
2625
2627
  clear_deferred_timer(gobj);
2626
2628
  if (priv.expected_pops === 0) priv.expected_pop_ids = [];
@@ -2629,6 +2631,7 @@ function push_pending_markers(gobj) {
2629
2631
  priv.deferred_route = "";
2630
2632
  yui_shell_navigate(gobj, route);
2631
2633
  }
2634
+ return pushed;
2632
2635
  }
2633
2636
  function overlay_dismissed(gobj, entry) {
2634
2637
  let priv = gobj.priv;
@@ -2781,7 +2784,8 @@ function ac_back_wait_timeout(gobj, event, kw, src) {
2781
2784
  priv.expected_pop_hash = "";
2782
2785
  priv.expected_pop_ids = [];
2783
2786
  priv.restore_hash = "";
2784
- push_pending_markers(gobj);
2787
+ let pushed = push_pending_markers(gobj);
2788
+ given_up.ids.push(...pushed);
2785
2789
  priv.given_up = given_up;
2786
2790
  return 0;
2787
2791
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.26.16",
3
+ "version": "7.26.17",
4
4
  "type": "module",
5
5
  "main": "dist/gobj-ui.cjs.js",
6
6
  "module": "dist/gobj-ui.es.js",
@@ -34,8 +34,10 @@
34
34
  * string (its type is not known before running); a description
35
35
  * built piece by piece (push() into an array that is later the
36
36
  * content); a computed tag with an attrs slot that is not an object
37
- * literal; markup given to innerHTML, or to a widget (Tabulator,
38
- * G6), which are other sinks. set_strings_are_text(true) logs, once
37
+ * literal; a replace() whose replacement is a function or whose
38
+ * pattern is a variable, and a method called on a function result
39
+ * (String(x).replace(...)); markup given to innerHTML, or to a
40
+ * widget (Tabulator, G6), which are other sinks. set_strings_are_text(true) logs, once
39
41
  * at run time, every string that reaches createElement2() starting
40
42
  * with '<': that is the other half of the check.
41
43
  *
@@ -107,41 +109,82 @@ const PURE_METHODS = new Set([
107
109
  "toLowerCase", "toUpperCase"
108
110
  ]);
109
111
 
110
- /* replace()/replaceAll() on a receiver that is static and with static
111
- * arguments is computed, so a tag stripped away is seen as stripped.
112
- * Anything else is no longer static. Its head is the receiver's -- a
113
- * receiver that carries data stays BUILT markup through them, and so
114
- * does a static one whose replacement is data
115
- * ('<b>NAME</b>'.replace('NAME', name)) -- unless the REPLACEMENT brings
116
- * markup: then that is what the result may start with, and with data
117
- * somewhere it is built markup ('x'.replace('x', `<b>${y}</b>`),
118
- * ('a' + name).replace('a', '<b>')). */
119
- function replaced(method, head, args)
112
+ /* replace()/replaceAll(). A receiver that is static, with a static
113
+ * pattern and a static replacement, is computed, so a tag stripped away
114
+ * is seen as stripped. Otherwise the result is no longer static, and its
115
+ * head is what is known to come BEFORE the first match: the receiver's
116
+ * text up to it, then the replacement's head
117
+ * ('<b>NAME</b>'.replace('NAME', name) is '<b>' + data: built;
118
+ * 'abc'.replace('abc', '<i>' + y) is '<i>' + data: built). Where the
119
+ * first match is decides it, so the replacement is the head only when the
120
+ * pattern can match at 0: ('z' + name).replace('a', '<b>') starts with
121
+ * 'z', and 'abc'.replace('x', '<i>' + y) is 'abc'.
122
+ * Not seen: a replacement that is a function, a pattern in a variable. */
123
+ function static_pattern(arg)
120
124
  {
121
- let replacement = (args.length === 2)? leading(args[1]) : null;
122
- if(head.complete && args.length === 2) {
123
- let pattern = null;
124
- if(args[0].type === "Literal" && args[0].regex) {
125
- try {
126
- pattern = new RegExp(args[0].regex.pattern, args[0].regex.flags);
127
- } catch(e) {
128
- pattern = null;
129
- }
130
- } else if(is_string_literal(args[0])) {
131
- pattern = args[0].value;
125
+ if(arg.type === "Literal" && arg.regex) {
126
+ try {
127
+ return new RegExp(arg.regex.pattern, arg.regex.flags);
128
+ } catch(e) {
129
+ return null;
132
130
  }
133
- if(pattern !== null && replacement && replacement.complete) {
134
- try {
135
- return {text: String.prototype[method].call(head.text, pattern, replacement.text),
136
- complete: true};
137
- } catch(e) {
138
- /* replaceAll() with a regex that is not global throws:
139
- * so does the code, the check need not know what it gives. */
140
- }
131
+ }
132
+ if(is_string_literal(arg)) {
133
+ return arg.value;
134
+ }
135
+ return null;
136
+ }
137
+
138
+ function first_match(text, pattern)
139
+ {
140
+ if(typeof pattern === "string") {
141
+ return text.indexOf(pattern);
142
+ }
143
+ try {
144
+ const re = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ""));
145
+ const m = re.exec(text);
146
+ return m ? m.index : -1;
147
+ } catch(e) {
148
+ return -1;
149
+ }
150
+ }
151
+
152
+ function replaced(method, head, args)
153
+ {
154
+ if(args.length !== 2) {
155
+ return {text: head.text, complete: false};
156
+ }
157
+ const pattern = static_pattern(args[0]);
158
+ const replacement = leading(args[1]);
159
+ if(pattern === null) {
160
+ return {text: head.text, complete: false};
161
+ }
162
+ if(head.complete && replacement && replacement.complete) {
163
+ try {
164
+ return {text: String.prototype[method].call(head.text, pattern, replacement.text),
165
+ complete: true};
166
+ } catch(e) {
167
+ /* replaceAll() with a regex that is not global throws: so
168
+ * does the code, the check need not know what it gives. */
169
+ return {text: head.text, complete: false};
141
170
  }
142
171
  }
143
- if(replacement && starts_with_markup(replacement.text)) {
144
- return {text: replacement.text, complete: false};
172
+ const rep_head = replacement ? replacement.text : "";
173
+ const idx = first_match(head.text, pattern);
174
+ if(idx >= 0) {
175
+ return {text: head.text.slice(0, idx) + rep_head, complete: false};
176
+ }
177
+ if(head.complete) {
178
+ return {text: head.text, complete: true}; // no match: the receiver as it is
179
+ }
180
+ if(typeof pattern === "string") {
181
+ /* The first match is in the data: no earlier than where the
182
+ * pattern could start straddling the known text. */
183
+ const keep = Math.max(0, head.text.length - pattern.length + 1);
184
+ if(keep === 0) {
185
+ return {text: rep_head, complete: false};
186
+ }
187
+ return {text: head.text.slice(0, keep), complete: false};
145
188
  }
146
189
  return {text: head.text, complete: false};
147
190
  }
@@ -2803,7 +2803,12 @@ function push_overlay_marker(gobj, entry)
2803
2803
  * lower overlay's marker, an app's own) keeps its keys; the stamp
2804
2804
  * is added to them, and replaces an older stamp. */
2805
2805
  let below = window.history.state;
2806
- if(below === null || (typeof below === "object" && !Array.isArray(below))) {
2806
+ /* A PLAIN object only: a Map or a Date is an object too (the state
2807
+ * is a structured clone), and Object.assign() copies none of it. */
2808
+ let plain = below !== null && typeof below === "object" &&
2809
+ (Object.getPrototypeOf(below) === Object.prototype ||
2810
+ Object.getPrototypeOf(below) === null);
2811
+ if(below === null || plain) {
2807
2812
  try {
2808
2813
  window.history.replaceState(
2809
2814
  Object.assign({}, below, { __yui_below__: entry.id }), "");
@@ -2813,7 +2818,7 @@ function push_overlay_marker(gobj, entry)
2813
2818
  } else {
2814
2819
  log_warning(
2815
2820
  `C_YUI_SHELL: the entry under an overlay holds a history state that ` +
2816
- `is not an object, it cannot be stamped: Back will not close the overlay`
2821
+ `is not a plain object, it cannot be stamped: Back will not close the overlay`
2817
2822
  );
2818
2823
  }
2819
2824
  try {
@@ -2931,6 +2936,7 @@ function push_pending_markers(gobj)
2931
2936
  {
2932
2937
  let priv = gobj.priv;
2933
2938
  let pending = priv.pending_markers.splice(0);
2939
+ let pushed = [];
2934
2940
  for(let entry of pending) {
2935
2941
  entry.marker_pending = false;
2936
2942
  if(entry.dismissed || priv.overlay_stack.indexOf(entry) < 0) {
@@ -2939,7 +2945,10 @@ function push_pending_markers(gobj)
2939
2945
  /* The traversal was same-hash, but a route may have moved
2940
2946
  * meanwhile: the marker lives on the hash it is pushed on. */
2941
2947
  entry.hash = window.location.hash;
2942
- push_overlay_marker(gobj, entry); // Error already logged
2948
+ if(push_overlay_marker(gobj, entry)) {
2949
+ pushed.push(entry.id);
2950
+ }
2951
+ // else: Error already logged
2943
2952
  }
2944
2953
  clear_deferred_timer(gobj);
2945
2954
  if(priv.expected_pops === 0) {
@@ -2950,6 +2959,7 @@ function push_pending_markers(gobj)
2950
2959
  priv.deferred_route = "";
2951
2960
  yui_shell_navigate(gobj, route);
2952
2961
  }
2962
+ return pushed; // the ids of the markers it pushed, in order
2953
2963
  }
2954
2964
 
2955
2965
  function overlay_dismissed(gobj, entry)
@@ -3264,7 +3274,12 @@ function ac_back_wait_timeout(gobj, event, kw, src)
3264
3274
  priv.expected_pop_hash = "";
3265
3275
  priv.expected_pop_ids = [];
3266
3276
  priv.restore_hash = "";
3267
- push_pending_markers(gobj); // and navigates the deferred route
3277
+ /* The markers of the overlays opened during the wait go on top of
3278
+ * the dead one, before the route: a late back() steps back one entry
3279
+ * from the route and lands on the highest of them, not on the dead
3280
+ * marker -- they are its landing too. */
3281
+ let pushed = push_pending_markers(gobj); // and navigates the deferred route
3282
+ given_up.ids.push(...pushed);
3268
3283
  priv.given_up = given_up;
3269
3284
  return 0;
3270
3285
  }
@@ -357,7 +357,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
357
357
  expect(shell.priv.expected_pops).toBe(0);
358
358
  expect(session.entries.length).toBe(2); // the dead first marker, ahead
359
359
  expect(session.index).toBe(0);
360
- expect(window.history.state.__yui_overlay__).toBeUndefined(); // stamped, no marker
360
+ expect(window.history.state).toEqual({__yui_below__: first.id}); // stamped, no marker
361
361
  expect(errors()).toEqual([]);
362
362
  });
363
363
 
@@ -622,7 +622,7 @@ describe("a routed shell, a realistic history (V5)", () => {
622
622
  const b = yui_shell_register_overlay(shell, () => {});
623
623
  yui_shell_overlay_dismissed(shell, b); // its button: back() to A's marker
624
624
  await next_task();
625
- expect(window.history.state.__yui_overlay__).toBe(a.id); // stamped for B, which is gone
625
+ expect(window.history.state).toEqual({__yui_overlay__: a.id, __yui_below__: b.id});
626
626
 
627
627
  traverse_to(session.index + 1); // Forward, onto B's dead marker
628
628
  expect(closed_a).toBe(0);
@@ -960,3 +960,48 @@ describe("a back() given up on that lands after all (V7.2)", () => {
960
960
  expect(shell_warnings().some(m => m.includes("undoes route '/prev'"))).toBe(true);
961
961
  });
962
962
  });
963
+
964
+ describe("V8: the edges of the stamp and of the late landing", () => {
965
+ test("a late back() lands on the marker of an overlay opened during the wait: said too", async () => {
966
+ const app = gobj_create("v8app1", "C_TEST_MARKER_APP", {}, yuno);
967
+ gobj_start(app);
968
+ window.location.hash = "#/home";
969
+ install_history_model();
970
+ const shell = routed_shell("v8shell1", app);
971
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed, yui_shell_navigate} =
972
+ await import("./c_yui_shell.js");
973
+ const x = yui_shell_register_overlay(shell, () => {});
974
+ let late = null;
975
+ window.history.back = () => { late = () => traverse_to(session.index - 1); };
976
+ links_on_hash_assign();
977
+
978
+ yui_shell_overlay_dismissed(shell, x);
979
+ yui_shell_navigate(shell, "/prev");
980
+ const y = yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
981
+ expect(y.marker_pending).toBe(true);
982
+ await new Promise(resolve => setTimeout(resolve, 1100));
983
+ await next_task();
984
+ expect(window.location.hash).toBe("#/prev");
985
+
986
+ late(); // from #/prev onto Y's marker
987
+ await next_task();
988
+ await next_task();
989
+ expect(window.history.state.__yui_overlay__).toBe(y.id);
990
+ expect(shell_warnings().filter(m => m.includes("arriving late")).length).toBe(1);
991
+ });
992
+
993
+ test("a state that is not a plain object is not stamped over, and says so", async () => {
994
+ const app = gobj_create("v8app2", "C_TEST_MARKER_APP", {}, yuno);
995
+ gobj_start(app);
996
+ window.location.hash = "#/home";
997
+ install_history_model();
998
+ const shell = routed_shell("v8shell2", app);
999
+ const {yui_shell_register_overlay} = await import("./c_yui_shell.js");
1000
+ const app_state = new Map([["k", 1]]);
1001
+ window.history.replaceState(app_state, "");
1002
+ logged.length = 0;
1003
+ yui_shell_register_overlay(shell, () => {});
1004
+ expect(session.entries[0].state).toBe(app_state);
1005
+ expect(shell_warnings().filter(m => m.includes("not a plain object")).length).toBe(1);
1006
+ });
1007
+ });