@yuneta/gobj-ui 7.26.11 → 7.26.13

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.
@@ -213,14 +213,17 @@ class YuiToolbar extends Toolbar
213
213
  return `<div class="g6-toolbar-sep" aria-hidden="true"></div>`;
214
214
  }
215
215
 
216
- const extra_class = item.className ? ` ${item.className}` : '';
217
- const title = item.title ?? '';
216
+ /* Everything that goes into this markup is escaped, the
217
+ * read-out branch too: its text, title and value are values
218
+ * (a zoom figure, a translated title), never markup. */
219
+ const extra_class = item.className ? ` ${escapeHtml(String(item.className))}` : '';
220
+ const title = escapeHtml(String(item.title ?? ''));
218
221
 
219
222
  if(item.readout) {
220
223
  return (
221
224
  `<div class="g6-toolbar-readout${extra_class}"` +
222
225
  ` title="${title}" aria-label="${title}">` +
223
- `${item.text ?? ''}` +
226
+ `${escapeHtml(String(item.text ?? ''))}` +
224
227
  `</div>`
225
228
  );
226
229
  }
@@ -231,12 +234,12 @@ class YuiToolbar extends Toolbar
231
234
  const body = is_text
232
235
  ? `<span class="g6-toolbar-text">${escapeHtml(String(item.text))}</span>`
233
236
  : `<svg aria-hidden="true" focusable="false">` +
234
- `<use xlink:href="#${item.id}"></use>` +
237
+ `<use xlink:href="#${escapeHtml(String(item.id))}"></use>` +
235
238
  `</svg>`;
236
239
 
237
240
  return (
238
241
  `<div class="g6-toolbar-item${kind_class}${extra_class}"` +
239
- ` value="${item.value}" title="${title}"` +
242
+ ` value="${escapeHtml(String(item.value))}" title="${title}"` +
240
243
  ` aria-label="${title}"${disabled}>` +
241
244
  `${body}` +
242
245
  `</div>`
@@ -2302,6 +2305,8 @@ function topic_defaults_of(gobj, topic_name)
2302
2305
  * refused value logs, and the node wears the topic's colour.
2303
2306
  ************************************************************/
2304
2307
  const refused_colors_logged = new Set(); // once per (gobj, value): every repaint asks again
2308
+ const REFUSED_COLORS_LOG_MAX = 200;
2309
+ let refused_colors_capped = false;
2305
2310
 
2306
2311
  function data_color(gobj, value)
2307
2312
  {
@@ -2311,9 +2316,21 @@ function data_color(gobj, value)
2311
2316
  let color = css_color_or_null(value);
2312
2317
  if(!color) {
2313
2318
  let key = `${gobj_short_name(gobj)}\u0000${String(value)}`;
2314
- if(!refused_colors_logged.has(key) && refused_colors_logged.size < 200) {
2319
+ if(refused_colors_logged.has(key)) {
2320
+ return color;
2321
+ }
2322
+ if(refused_colors_logged.size < REFUSED_COLORS_LOG_MAX) {
2315
2323
  refused_colors_logged.add(key);
2316
2324
  log_warning(`${gobj_short_name(gobj)}: colour refused, not a css colour: ${JSON.stringify(value)}`);
2325
+ } else if(!refused_colors_capped) {
2326
+ /* Bounded, but not silent: say once that the rest go unlogged.
2327
+ * An LRU would log again and again on every repaint of a
2328
+ * graph with more refused colours than it holds. */
2329
+ refused_colors_capped = true;
2330
+ log_warning(
2331
+ `${gobj_short_name(gobj)}: ${REFUSED_COLORS_LOG_MAX} refused colours ` +
2332
+ `logged; further refused colours are not logged`
2333
+ );
2317
2334
  }
2318
2335
  }
2319
2336
  return color;
@@ -556,7 +556,7 @@ function build_head(gobj, model)
556
556
  {
557
557
  let items = [
558
558
  ['span', {class: 'GCLASS_NAME'},
559
- model.id || gobj_read_str_attr(gobj, "gclass_name") || "?"]
559
+ text_node(model.id || gobj_read_str_attr(gobj, "gclass_name") || "?")]
560
560
  ];
561
561
 
562
562
  if(model.instances !== null) {
@@ -577,7 +577,7 @@ function build_head(gobj, model)
577
577
  items.push(
578
578
  ['span', {class: 'GCLASS_CHIP GCLASS_CURRENT',
579
579
  title: t("current state"),
580
- 'data-i18n-title': 'current state'}, current]);
580
+ 'data-i18n-title': 'current state'}, text_node(current)]);
581
581
  }
582
582
 
583
583
  return ['div', {class: 'GCLASS_HEAD'}, items];
@@ -164,6 +164,18 @@ let PRIVATE_DATA = {
164
164
  /* Overlays registered while one of those history.back() calls is
165
165
  * still in flight: their marker is pushed once it has landed. */
166
166
  pending_markers: [],
167
+ /* The hash the shell last saw, to tell a traversal that moved it
168
+ * (a hashchange is queued behind its popstate) from one that did
169
+ * not. */
170
+ last_hash: "",
171
+ /* The waiting markers are pushed by the queued hashchange, after
172
+ * its navigation, not by the popstate that dropped our back(). */
173
+ markers_after_hashchange: false,
174
+ /* A position (hash) to put back when our back() lands under a
175
+ * marker a replace navigation had moved, and the hashchange of
176
+ * that landing, which must not navigate. */
177
+ restore_hash: "",
178
+ skip_hash: "",
167
179
  popstate_handler: null,
168
180
  /* Avatar item support — every toolbar item with type:"avatar"
169
181
  * registers its <span> here so refresh_avatars() can repaint
@@ -265,16 +277,41 @@ function mt_start(gobj)
265
277
  preinstantiate_eager_views(gobj);
266
278
 
267
279
  if(gobj_read_attr(gobj, "use_hash")) {
280
+ priv.last_hash = window.location.hash;
268
281
  priv.hash_handler = () => {
269
282
  let p = gobj.priv;
270
- if(p && p.expected_pops > 0 &&
283
+ if(!p) {
284
+ return;
285
+ }
286
+ let markers_now = p.markers_after_hashchange;
287
+ p.markers_after_hashchange = false;
288
+ if(p.skip_hash && window.location.hash === p.skip_hash) {
289
+ /* The hashchange of an expected landing whose position
290
+ * was put back (see on_traversal): the route did not
291
+ * change, there is nothing to navigate to. */
292
+ p.skip_hash = "";
293
+ p.last_hash = window.location.hash;
294
+ if(markers_now) {
295
+ push_pending_markers(gobj);
296
+ }
297
+ return;
298
+ }
299
+ p.skip_hash = "";
300
+ if(p.expected_pops > 0 &&
271
301
  window.location.hash !== p.expected_pop_hash) {
272
302
  drop_expected_pops(gobj); // a back() of ours never landed
303
+ markers_now = true;
273
304
  }
274
305
  let route = hash_to_route(window.location.hash);
275
306
  if(!empty_string(route)) {
276
307
  navigate_to(gobj, route);
277
308
  }
309
+ p.last_hash = window.location.hash;
310
+ if(markers_now) {
311
+ /* After the navigation: an overlay it drained gets no
312
+ * marker, a kept one gets it on the hash it now lives on. */
313
+ push_pending_markers(gobj);
314
+ }
278
315
  };
279
316
  window.addEventListener("hashchange", priv.hash_handler);
280
317
 
@@ -288,17 +325,58 @@ function mt_start(gobj)
288
325
  if(!p) {
289
326
  return;
290
327
  }
328
+ /* A traversal that moved the hash is followed by a
329
+ * hashchange, QUEUED: it runs after this handler, and it is
330
+ * the one that navigates (and drains). */
331
+ let hash_moved = window.location.hash !== p.last_hash;
291
332
  if(p.expected_pops > 0 &&
292
333
  window.location.hash !== p.expected_pop_hash) {
293
334
  /* Not the landing of ours: it was dropped. Whatever
294
- * this traversal is, it is handled below as any other. */
335
+ * this traversal is, it is handled as any other, and
336
+ * the overlays that waited get their markers AFTER it:
337
+ * pushed first, the waiting top had a marker the
338
+ * traversal then took as "stepped off" -- it was closed
339
+ * and its marker left orphaned. When the hash moved,
340
+ * "after it" is after the hashchange's navigation: a
341
+ * marker pushed here for an overlay that navigation then
342
+ * drains is left orphaned too. */
295
343
  drop_expected_pops(gobj);
344
+ on_traversal(ev);
345
+ if(hash_moved) {
346
+ p.markers_after_hashchange = true;
347
+ } else {
348
+ push_pending_markers(gobj);
349
+ }
350
+ p.last_hash = window.location.hash;
351
+ return;
352
+ }
353
+ on_traversal(ev);
354
+ if(!p.skip_hash) {
355
+ p.last_hash = window.location.hash;
296
356
  }
357
+ };
358
+ let on_traversal = (ev) => {
359
+ let p = gobj.priv;
297
360
  if(p.expected_pops > 0) {
298
361
  /* A history.back() we issued to retire a dismissed
299
362
  * overlay — the teardown already happened. */
300
363
  p.expected_pops--;
301
364
  if(p.expected_pops === 0) {
365
+ if(p.restore_hash && p.restore_hash !== window.location.hash) {
366
+ /* The marker had moved to another hash (a replace
367
+ * navigation wrote the view's position on it) and
368
+ * the back() landed on the entry under it, on the
369
+ * old one: put the position back, and let its
370
+ * hashchange pass -- re-navigating would lose it. */
371
+ try {
372
+ window.history.replaceState(window.history.state, "", p.restore_hash);
373
+ p.skip_hash = p.restore_hash;
374
+ p.last_hash = p.restore_hash;
375
+ } catch(e) {
376
+ log_warning(`C_YUI_SHELL: restoring '${p.restore_hash}' failed: ${e}`);
377
+ }
378
+ }
379
+ p.restore_hash = "";
302
380
  push_pending_markers(gobj);
303
381
  }
304
382
  return;
@@ -307,6 +385,10 @@ function mt_start(gobj)
307
385
  return;
308
386
  }
309
387
  let top = p.overlay_stack[p.overlay_stack.length - 1];
388
+ if(top.marker_pending || top.no_marker) {
389
+ /* It has no marker (yet): no traversal can step off it. */
390
+ return;
391
+ }
310
392
  /* Did this traversal step OFF the top overlay's marker?
311
393
  * That is the only landing that closes it, and the marker's
312
394
  * OWN hash is what says so: pushState() with no URL keeps
@@ -324,7 +406,13 @@ function mt_start(gobj)
324
406
  * a `stay` modal's marker legitimately lives on a hash that
325
407
  * is NOT the resting one, and matching on the resting route
326
408
  * left Back unable to close it. */
327
- if(window.location.hash !== top.hash) {
409
+ if(window.location.hash !== top.hash &&
410
+ window.location.hash !== top.below_hash) {
411
+ /* Neither the marker's own hash nor the hash of the
412
+ * entry under it: a route traversal. (Under it: a
413
+ * replace navigation moved the marker's hash after it
414
+ * was pushed, and stepping off it lands on the entry
415
+ * below, on the hash it had.) */
328
416
  return;
329
417
  }
330
418
  /* Which entry did it land ON? Markers ({__yui_overlay__:
@@ -341,6 +429,13 @@ function mt_start(gobj)
341
429
  if(st && st.__yui_overlay__ === top.id) {
342
430
  return;
343
431
  }
432
+ if(st && st.__yui_overlay__ > top.id &&
433
+ !p.overlay_stack.some(e => e.id === st.__yui_overlay__)) {
434
+ /* A dead marker NEWER than the top: only Forward reaches
435
+ * it (an overlay closed by its button above this one).
436
+ * It is not a step off the top's marker. */
437
+ return;
438
+ }
344
439
  p.overlay_stack.pop();
345
440
  try {
346
441
  top.close();
@@ -414,10 +509,19 @@ function mt_stop(gobj)
414
509
  window.removeEventListener("popstate", priv.popstate_handler);
415
510
  priv.popstate_handler = null;
416
511
  }
417
- /* Nothing listens for the landings any more. */
512
+ /* Nothing listens for the landings any more. An overlay that
513
+ * waited for one gets no marker: it says so, so neither a later
514
+ * dismissal retires one nor a traversal waits for it. */
515
+ for(let entry of priv.pending_markers) {
516
+ entry.marker_pending = false;
517
+ entry.no_marker = true;
518
+ }
418
519
  priv.expected_pops = 0;
419
520
  priv.expected_pop_hash = "";
420
521
  priv.pending_markers = [];
522
+ priv.markers_after_hashchange = false;
523
+ priv.restore_hash = "";
524
+ priv.skip_hash = "";
421
525
  if(priv.keydown_handler) {
422
526
  window.removeEventListener("keydown", priv.keydown_handler);
423
527
  priv.keydown_handler = null;
@@ -1531,6 +1635,7 @@ function navigate_to(gobj, route, depth, no_drain)
1531
1635
  * its Back/dismiss bookkeeping. */
1532
1636
  try {
1533
1637
  window.history.replaceState(window.history.state, "", target_hash);
1638
+ priv.last_hash = target_hash;
1534
1639
  /* This rewrote the hash of the CURRENT entry. When that
1535
1640
  * entry is a live marker (a replace-navigation issued
1536
1641
  * with an overlay open), its recorded hash just went
@@ -2652,6 +2757,10 @@ function push_overlay_history(gobj, close, opts)
2652
2757
  function push_overlay_marker(gobj, entry)
2653
2758
  {
2654
2759
  let priv = gobj.priv;
2760
+ /* The entry UNDER the marker is on this hash, and a back() off the
2761
+ * marker lands there -- even after a replace-navigation rewrote the
2762
+ * marker's own hash (retag_overlay_hash()). */
2763
+ entry.below_hash = window.location.hash;
2655
2764
  try {
2656
2765
  window.history.pushState({ __yui_overlay__: entry.id }, "");
2657
2766
  } catch(e) {
@@ -2669,8 +2778,8 @@ function push_overlay_marker(gobj, entry)
2669
2778
 
2670
2779
  /* A history.back() of ours that the browser dropped: nothing will land
2671
2780
  * for it, so it must not wait for a landing and swallow the reader's
2672
- * next Back. The overlays that waited for it get their markers now, on
2673
- * the hash they are on. */
2781
+ * next Back. The overlays that waited for it get their markers from the
2782
+ * caller (push_pending_markers()), after the traversal is handled. */
2674
2783
  function drop_expected_pops(gobj)
2675
2784
  {
2676
2785
  let priv = gobj.priv;
@@ -2683,7 +2792,8 @@ function drop_expected_pops(gobj)
2683
2792
  );
2684
2793
  priv.expected_pops = 0;
2685
2794
  priv.expected_pop_hash = "";
2686
- push_pending_markers(gobj);
2795
+ /* The caller pushes the waiting markers, once it has handled the
2796
+ * traversal that told it. */
2687
2797
  }
2688
2798
 
2689
2799
  /* The history.back() calls of ours have all landed: push the markers of
@@ -2721,7 +2831,7 @@ function overlay_dismissed(gobj, entry)
2721
2831
  return;
2722
2832
  }
2723
2833
  priv.overlay_stack.splice(idx, 1);
2724
- if(entry.marker_pending) {
2834
+ if(entry.marker_pending || entry.no_marker) {
2725
2835
  /* Its marker was never pushed: there is nothing to retire. */
2726
2836
  return;
2727
2837
  }
@@ -2739,7 +2849,12 @@ function overlay_dismissed(gobj, entry)
2739
2849
  let st = window.history.state;
2740
2850
  if(st && st.__yui_overlay__ === entry.id) {
2741
2851
  priv.expected_pops++;
2742
- priv.expected_pop_hash = window.location.hash;
2852
+ priv.expected_pop_hash = (typeof entry.below_hash === "string")?
2853
+ entry.below_hash : window.location.hash;
2854
+ /* A position written on the marker by a replace navigation is
2855
+ * put back once the back() lands under it (on_traversal). */
2856
+ priv.restore_hash = (window.location.hash !== priv.expected_pop_hash)?
2857
+ window.location.hash : "";
2743
2858
  try {
2744
2859
  window.history.back();
2745
2860
  } catch(e) {
@@ -81,6 +81,7 @@ import {
81
81
 
82
82
 
83
83
  import {attach_clear, refresh_clear} from "./yui_inputs.js";
84
+ import {text_node} from "./yui_text.js";
84
85
  import {register_c_g6_nodes_tree} from "./c_g6_nodes_tree.js";
85
86
  import {yui_toolbar, yui_toolbar_icon, YUI_TOOLBAR_ICON_SIZE} from "./yui_toolbar.js";
86
87
  import {
@@ -3084,7 +3085,7 @@ function refresh_legend(gobj)
3084
3085
  ['span', {class: 'GRAPH_LEGEND_NAME',
3085
3086
  style: (is_main? LEGEND_MAIN_NAME_STYLE : LEGEND_NAME_STYLE) +
3086
3087
  (hidden? ' text-decoration:line-through; opacity:.6;' : '')},
3087
- topic_name],
3088
+ text_node(topic_name)], // schema data, never markup
3088
3089
  ['span', {class: 'GRAPH_LEGEND_COUNT',
3089
3090
  style: LEGEND_COUNT_STYLE},
3090
3091
  hidden? `${entry.total}` : `${entry.visible}/${entry.total}`]
@@ -0,0 +1,63 @@
1
+ /***********************************************************************
2
+ * gclass_head_text.wiring.test.js
3
+ *
4
+ * The header of C_YUI_GCLASS shows what a REMOTE description says:
5
+ * the gclass name and the instance's current state are data, so
6
+ * they are text, never markup (REV-CLOUD V4.2).
7
+ *
8
+ * Copyright (c) 2026, ArtGins.
9
+ * All Rights Reserved.
10
+ ***********************************************************************/
11
+ import {describe, test, expect, beforeAll} from "vitest";
12
+ import {install_dom_double} from "../test/dom_double.js";
13
+
14
+ install_dom_double();
15
+
16
+ if(typeof globalThis.ResizeObserver === "undefined") {
17
+ globalThis.ResizeObserver = class {
18
+ observe() {
19
+ }
20
+ unobserve() {
21
+ }
22
+ disconnect() {
23
+ }
24
+ };
25
+ }
26
+
27
+ const {
28
+ SDATA_END, gclass_create,
29
+ gobj_start_up, gobj_create_yuno, gobj_create,
30
+ gobj_start, gobj_read_attr,
31
+ set_log_callback,
32
+ } = await import("@yuneta/gobj-js");
33
+ const {register_c_yui_gclass} = await import("./c_yui_gclass.js");
34
+
35
+ const EVIL = '<img src=x onerror="window.__pwned=1">';
36
+ let yuno = null;
37
+
38
+ beforeAll(async () => {
39
+ const i18next = (await import("i18next")).default;
40
+ await i18next.init({lng: "en", resources: {}});
41
+ gobj_start_up(null, null, null, null, null, null, null);
42
+ set_log_callback(() => {});
43
+ gclass_create("C_TEST_GCLASS_HOST", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
44
+ register_c_yui_gclass();
45
+ yuno = gobj_create_yuno("gclass_head_yuno", "C_TEST_GCLASS_HOST", {});
46
+ gobj_start(yuno);
47
+ });
48
+
49
+ describe("C_YUI_GCLASS header", () => {
50
+ test("the gclass name and the current state are text", () => {
51
+ const view = gobj_create("gclass_head", "C_YUI_GCLASS", {
52
+ description: {id: EVIL, gcflag: [], attrs: [], commands: [],
53
+ gclass_methods: [], internal_methods: [],
54
+ FSM: {events: [], states: {}}},
55
+ current_state: EVIL,
56
+ }, yuno);
57
+ gobj_start(view);
58
+ const $c = gobj_read_attr(view, "$container");
59
+ expect($c.querySelector(".GCLASS_NAME").textContent).toBe(EVIL);
60
+ expect($c.querySelector(".GCLASS_CURRENT").textContent).toBe(EVIL);
61
+ expect($c.querySelector("img")).toBe(null);
62
+ });
63
+ });