@yuneta/gobj-js 7.26.1 → 7.26.3

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.
@@ -908,7 +908,7 @@ function json2item(gobj, sdata, it, jn_value_) {
908
908
  }
909
909
  if (jn_value2 === void 0) log_error(`${gobj_short_name(gobj)}: attr undefined: ${it.name}`);
910
910
  sdata[it.name] = jn_value2;
911
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(it.name)) gobj.priv[it.name] = jn_value2;
911
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, it.name)) gobj.priv[it.name] = jn_value2;
912
912
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
913
913
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, it.name);
914
914
  }
@@ -2111,7 +2111,7 @@ function gobj_has_attr(gobj, name) {
2111
2111
  function gobj_read_attr(gobj, name, src) {
2112
2112
  let jn_attrs = gobj.jn_attrs;
2113
2113
  if (name in jn_attrs) {
2114
- if (jn_attrs.hasOwnProperty(name)) {
2114
+ if (Object.prototype.hasOwnProperty.call(jn_attrs, name)) {
2115
2115
  let value = jn_attrs[name];
2116
2116
  if (gobj.gclass.gmt.mt_reading) return gobj.gclass.gmt.mt_reading(gobj, name, value);
2117
2117
  return value;
@@ -2217,9 +2217,9 @@ function gobj_write_attr(gobj, path, value, src) {
2217
2217
  if (ss.length <= 1) {
2218
2218
  let key = path;
2219
2219
  if (key in jn_attrs) {
2220
- if (jn_attrs.hasOwnProperty(key)) {
2220
+ if (Object.prototype.hasOwnProperty.call(jn_attrs, key)) {
2221
2221
  jn_attrs[key] = value;
2222
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(key)) gobj.priv[key] = value;
2222
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, key)) gobj.priv[key] = value;
2223
2223
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
2224
2224
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, key);
2225
2225
  }
@@ -2240,9 +2240,9 @@ function gobj_write_attr(gobj, path, value, src) {
2240
2240
  }
2241
2241
  let jn_attrs = gobj.jn_attrs;
2242
2242
  if (key in jn_attrs) {
2243
- if (jn_attrs.hasOwnProperty(key)) {
2243
+ if (Object.prototype.hasOwnProperty.call(jn_attrs, key)) {
2244
2244
  jn_attrs[key] = value;
2245
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(key)) gobj.priv[key] = value;
2245
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, key)) gobj.priv[key] = value;
2246
2246
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
2247
2247
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, key);
2248
2248
  }
@@ -2269,7 +2269,7 @@ function sdata_write_default_values(gobj, include_flag, exclude_flag) {
2269
2269
  if (exclude_flag && it.flag & exclude_flag) continue;
2270
2270
  if (include_flag === -1 || it.flag & include_flag) {
2271
2271
  set_default(gobj, gobj.jn_attrs, it);
2272
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(it.name)) gobj.priv[it.name] = gobj.jn_attrs[it.name];
2272
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, it.name)) gobj.priv[it.name] = gobj.jn_attrs[it.name];
2273
2273
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, it.name);
2274
2274
  }
2275
2275
  }
@@ -2303,7 +2303,7 @@ function gobj_write_bool_attr(gobj, name, value) {
2303
2303
  let hs = gobj_hsdata2(gobj, name, false);
2304
2304
  if (hs) {
2305
2305
  hs[name] = parseBoolean(value);
2306
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(name)) gobj.priv[name] = hs[name];
2306
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, name)) gobj.priv[name] = hs[name];
2307
2307
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
2308
2308
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, name);
2309
2309
  }
@@ -2319,7 +2319,7 @@ function gobj_write_integer_attr(gobj, name, value) {
2319
2319
  let hs = gobj_hsdata2(gobj, name, false);
2320
2320
  if (hs) {
2321
2321
  hs[name] = parseInt(value);
2322
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(name)) gobj.priv[name] = hs[name];
2322
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, name)) gobj.priv[name] = hs[name];
2323
2323
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
2324
2324
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, name);
2325
2325
  }
@@ -2335,7 +2335,7 @@ function gobj_write_str_attr(gobj, name, value) {
2335
2335
  let hs = gobj_hsdata2(gobj, name, false);
2336
2336
  if (hs) {
2337
2337
  hs[name] = String(value);
2338
- if (is_object(gobj.priv) && gobj.priv.hasOwnProperty(name)) gobj.priv[name] = hs[name];
2338
+ if (is_object(gobj.priv) && Object.prototype.hasOwnProperty.call(gobj.priv, name)) gobj.priv[name] = hs[name];
2339
2339
  if (gobj.obflag & obflag_t.obflag_created && !(gobj.obflag & obflag_t.obflag_destroyed)) {
2340
2340
  if (gobj.gclass.gmt.mt_writing) gobj.gclass.gmt.mt_writing(gobj, name);
2341
2341
  }
@@ -2723,8 +2723,8 @@ function _subscription_match_kw(kw, event) {
2723
2723
  kw,
2724
2724
  renamed_event: ""
2725
2725
  };
2726
- let kw_match = Object.assign({}, kw);
2727
- let config_match = Object.assign({}, __config__);
2726
+ let kw_match = json_object_update({}, kw);
2727
+ let config_match = json_object_update({}, __config__);
2728
2728
  kw_match.__config__ = config_match;
2729
2729
  let renamed_event = "";
2730
2730
  let rename = config_match.__rename_event_name__;
@@ -2732,7 +2732,7 @@ function _subscription_match_kw(kw, event) {
2732
2732
  if (event_type) {
2733
2733
  renamed_event = event_type.event_name;
2734
2734
  delete config_match.__rename_event_name__;
2735
- if (event && json_size(kw_match.__global__) > 0) kw_match.__global__ = Object.assign({}, kw_match.__global__, { __original_event_name__: event });
2735
+ if (event && json_size(kw_match.__global__) > 0) kw_match.__global__ = json_object_update(json_object_update({}, kw_match.__global__), { __original_event_name__: event });
2736
2736
  }
2737
2737
  delete config_match.__hard_subscription__;
2738
2738
  delete config_match.__own_event__;
@@ -3108,7 +3108,7 @@ function gobj_publish_event(publisher, event, kw) {
3108
3108
  }
3109
3109
  }
3110
3110
  let kw2publish = kw;
3111
- if (json_size(__local__) > 0 || json_size(__global__) > 0) kw2publish = Object.assign({}, kw);
3111
+ if (json_size(__local__) > 0 || json_size(__global__) > 0) kw2publish = json_object_update({}, kw);
3112
3112
  if (json_size(__local__) > 0) kw_pop(kw2publish, __local__);
3113
3113
  if (__config__) {}
3114
3114
  if (json_size(__global__) > 0) json_object_update(kw2publish, json_deep_copy(__global__));
@@ -3645,10 +3645,7 @@ function json_deep_copy(obj) {
3645
3645
  ************************************************************/
3646
3646
  function duplicate_objects(...sourceObjects) {
3647
3647
  const result = {};
3648
- for (const obj of sourceObjects) {
3649
- const clonedObj = structuredClone(obj);
3650
- Object.assign(result, clonedObj);
3651
- }
3648
+ for (const obj of sourceObjects) json_object_update(result, structuredClone(obj));
3652
3649
  return result;
3653
3650
  }
3654
3651
  /************************************************************
@@ -4021,6 +4018,13 @@ function kw_find_path(gobj, kw, path, verbose) {
4021
4018
  let ss = path.split("`");
4022
4019
  let len = ss.length;
4023
4020
  for (let i = 0; i < len; i++) {
4021
+ if (is_array(kw)) {
4022
+ if (!/^[0-9]+$/.test(ss[i])) {
4023
+ if (verbose) log_error(`${prefix}path not found: '${path}'`);
4024
+ return;
4025
+ }
4026
+ ss[i] = String(Number(ss[i]));
4027
+ }
4024
4028
  if (is_prototype_segment(kw, ss[i])) {
4025
4029
  if (verbose) log_error(`${prefix}path not found: '${path}'`);
4026
4030
  return;
@@ -4438,7 +4442,7 @@ function kw_set_subdict_value(gobj, kw, path, key, value) {
4438
4442
  log_error("subdict is not an object");
4439
4443
  return -1;
4440
4444
  }
4441
- subdict[key] = value;
4445
+ set_own_key(subdict, key, value);
4442
4446
  return 0;
4443
4447
  }
4444
4448
  /***************************************************************************
@@ -4746,7 +4750,7 @@ function kwid_new_dict(gobj, kw, path) {
4746
4750
  else if (is_array(kw)) for (let i = 0; i < kw.length; i++) {
4747
4751
  let kv = kw[i];
4748
4752
  let id = kw_get_str(gobj, kv, "id", "", kw_flag_t.KW_REQUIRED);
4749
- if (!empty_string(id)) new_dict[id] = kv;
4753
+ if (!empty_string(id)) set_own_key(new_dict, id, kv);
4750
4754
  }
4751
4755
  else log_error(`${gobj_short_name(gobj)} kwid_new_dict: data type unknown`);
4752
4756
  return new_dict;
@@ -5192,11 +5196,11 @@ function jdb_update(jdb, topic_name, path, kw) {
5192
5196
  }
5193
5197
  if (ids.length === 0 && v) {
5194
5198
  if (is_array(v)) v.push(kw);
5195
- else if (v["id"] === kw["id"]) Object.assign(v, kw);
5199
+ else if (v["id"] === kw["id"]) json_object_update(v, kw);
5196
5200
  else {
5197
5201
  let v_ = kw_get_dict_value(null, v, jdb.hook, [], 1);
5198
5202
  let v__ = _jdb_get(v_, jdb.hook, kw["id"], false);
5199
- if (v__) Object.assign(v__, kw);
5203
+ if (v__) json_object_update(v__, kw);
5200
5204
  else v_.push(kw);
5201
5205
  }
5202
5206
  }
@@ -5397,6 +5401,68 @@ function safeSrc(url) {
5397
5401
  /***************************************************************************
5398
5402
  *
5399
5403
  ***************************************************************************/
5404
+ /***************************************************************************
5405
+ * Markup that createElement2() inserts AS HTML, said out loud.
5406
+ *
5407
+ * createElement2() reads a string that starts with '<' as markup. That
5408
+ * decision depends on the CONTENT of the string, not on who wrote it, so
5409
+ * a value from a backend or typed by a user that starts with '<' runs as
5410
+ * html. html_markup() is the explicit way to ask for markup: the string
5411
+ * is the caller's own (an icon, an svg), never data.
5412
+ *
5413
+ * ['span', {class: 'icon'}, html_markup('<i class="yi-copy"></i>')]
5414
+ *
5415
+ * With set_strings_are_text(true) a plain string is ALWAYS text and
5416
+ * html_markup() is the only way to insert markup.
5417
+ ***************************************************************************/
5418
+ var HtmlMarkup = class {
5419
+ constructor(html) {
5420
+ this.html = html;
5421
+ Object.freeze(this);
5422
+ }
5423
+ toString() {
5424
+ return this.html;
5425
+ }
5426
+ };
5427
+ function html_markup(html) {
5428
+ if (typeof html !== "string") {
5429
+ log_error(`html_markup: not a string, type ${typeof html}`);
5430
+ return new HtmlMarkup("");
5431
+ }
5432
+ return new HtmlMarkup(html);
5433
+ }
5434
+ function is_html_markup(v) {
5435
+ return v instanceof HtmlMarkup;
5436
+ }
5437
+ function markup_fragment(markup) {
5438
+ const template = document.createElement("template");
5439
+ template.innerHTML = markup.html.trim();
5440
+ return template.content;
5441
+ }
5442
+ var __strings_are_text__ = false;
5443
+ var __markup_strings_warned__ = /* @__PURE__ */ new Set();
5444
+ /***************************************************************************
5445
+ * The mode of createElement2() for a plain string that starts with '<':
5446
+ * false (default): it is markup, as it always was.
5447
+ * true: it is TEXT; markup is asked for with html_markup().
5448
+ * An app turns it on once its own code and the libraries it mounts pass
5449
+ * every deliberate markup through html_markup(). A string that starts
5450
+ * with '<' is then logged once (a deliberate one not migrated yet, or a
5451
+ * datum that used to run as html).
5452
+ ***************************************************************************/
5453
+ function set_strings_are_text(on) {
5454
+ __strings_are_text__ = !!on;
5455
+ }
5456
+ function strings_are_text() {
5457
+ return __strings_are_text__;
5458
+ }
5459
+ function warn_markup_string(content) {
5460
+ if (__markup_strings_warned__.size >= 100) return;
5461
+ const key = content.slice(0, 80);
5462
+ if (__markup_strings_warned__.has(key)) return;
5463
+ __markup_strings_warned__.add(key);
5464
+ log_warning(`createElement2: a string that starts with '<' is TEXT (set_strings_are_text): use html_markup() if it is markup: ${JSON.stringify(key)}`);
5465
+ }
5400
5466
  function createOneHtml(htmlString) {
5401
5467
  const template = document.createElement("template");
5402
5468
  template.innerHTML = htmlString.trim();
@@ -5558,11 +5624,18 @@ parent: document.body
5558
5624
  * if string begins with '<' then
5559
5625
  * it will be converted to HTMLElement
5560
5626
  * content = createOneHtml(string)
5627
+ * | html_markup(string): markup, said out loud (any mode)
5561
5628
  * ⚠️ So a string is MARKUP when it starts with '<': never
5562
5629
  * pass DATA (a value from a backend, a name a user typed)
5563
5630
  * as a bare string, or "<img src=x onerror=...>" runs.
5564
5631
  * Pass it as a Text node, which is always text:
5565
5632
  * ['span', {}, document.createTextNode(value)]
5633
+ * In a LIST of children a node (element, svg, Text) is
5634
+ * appended as it is and a string is always text -- but a
5635
+ * list whose first item is a string is read as ONE
5636
+ * description (the string is its tag).
5637
+ * With set_strings_are_text(true) a plain string is
5638
+ * always text, and markup only comes in html_markup().
5566
5639
  * if attrs['i18n'] is not empty then (or 'data-i18n' or 'data_i18next')
5567
5640
  * it will be overridden with the translation
5568
5641
  * content = translate(attrs['i18n']));
@@ -5605,18 +5678,26 @@ function createElement2(description, translate_fn) {
5605
5678
  el.setAttribute("data-i18n", data_i18next);
5606
5679
  }
5607
5680
  } else el.setAttribute(attr, attrs[attr]);
5608
- if (is_string(content)) {
5681
+ if (is_html_markup(content)) el.appendChild(markup_fragment(content));
5682
+ else if (is_string(content)) {
5609
5683
  content = content.trim();
5610
- if (content.length > 0 && content[0] === "<") el.appendChild(createOneHtml(content));
5611
- else if (translate_fn && data_i18next) {
5612
- let translated = translate_fn(data_i18next);
5613
- el.textContent = translated ? translated : content;
5614
- } else el.textContent = content;
5684
+ if (content.length > 0 && content[0] === "<" && !__strings_are_text__) el.appendChild(createOneHtml(content));
5685
+ else {
5686
+ if (content.length > 0 && content[0] === "<") warn_markup_string(content);
5687
+ if (translate_fn && data_i18next) {
5688
+ let translated = translate_fn(data_i18next);
5689
+ el.textContent = translated ? translated : content;
5690
+ } else el.textContent = content;
5691
+ }
5615
5692
  } else if (is_array(content)) {
5616
5693
  if (content.length > 0) {
5617
5694
  if (is_string(content[0])) el.appendChild(createElement2(content, translate_fn));
5618
- else for (let child of content) if (Array.isArray(child) && child.length > 0) el.appendChild(createElement2(child, translate_fn));
5619
- else if (child instanceof HTMLElement) el.appendChild(child);
5695
+ else for (let child of content) if (Array.isArray(child)) {
5696
+ if (child.length > 0) el.appendChild(createElement2(child, translate_fn));
5697
+ } else if (child instanceof Node) el.appendChild(child);
5698
+ else if (is_html_markup(child)) el.appendChild(markup_fragment(child));
5699
+ else if (is_string(child)) el.appendChild(document.createTextNode(child));
5700
+ else if (child !== null && child !== void 0 && child !== false) log_error(`createElement2: child ignored, type ${typeof child}`);
5620
5701
  }
5621
5702
  } else if (content instanceof HTMLElement || content instanceof Text || content instanceof SVGElement) el.appendChild(content);
5622
5703
  else if (content != null) {
@@ -6049,12 +6130,12 @@ function flat_diff(flat1, flat2) {
6049
6130
  removed: {},
6050
6131
  changed: {}
6051
6132
  };
6052
- for (const key of Object.keys(a)) if (!Object.prototype.hasOwnProperty.call(b, key)) diff.removed[key] = a[key];
6053
- else if (!flat_value_equal(a[key], b[key])) diff.changed[key] = {
6133
+ for (const key of Object.keys(a)) if (!Object.prototype.hasOwnProperty.call(b, key)) set_own_key(diff.removed, key, a[key]);
6134
+ else if (!flat_value_equal(a[key], b[key])) set_own_key(diff.changed, key, {
6054
6135
  from: a[key],
6055
6136
  to: b[key]
6056
- };
6057
- for (const key of Object.keys(b)) if (!Object.prototype.hasOwnProperty.call(a, key)) diff.added[key] = b[key];
6137
+ });
6138
+ for (const key of Object.keys(b)) if (!Object.prototype.hasOwnProperty.call(a, key)) set_own_key(diff.added, key, b[key]);
6058
6139
  return diff;
6059
6140
  }
6060
6141
  /************************************************************
@@ -7162,7 +7243,7 @@ function mt_inject_event(gobj, event, kw, src) {
7162
7243
  window.console.error(`Not in session`);
7163
7244
  return -1;
7164
7245
  }
7165
- kw = Object.assign({}, is_object(kw) ? kw : {});
7246
+ kw = json_object_update({}, is_object(kw) ? kw : {});
7166
7247
  if (is_object(kw["__md_iev__"])) kw["__md_iev__"] = json_deep_copy(kw["__md_iev__"]);
7167
7248
  if (msg_iev_get_stack(gobj, kw, IEVENT_STACK_ID)) {} else {
7168
7249
  let jn_ievent_id = build_cli_ievent_request(gobj, gobj_name(src), kw_get_str(gobj, kw, "__service__", 0, 0));
@@ -7220,6 +7301,13 @@ function setup_websocket(gobj) {
7220
7301
  websocket = new WebSocket(url);
7221
7302
  } catch (e) {
7222
7303
  log_error(`${gobj_short_name(gobj)}: Cannot open WebSocket to '${url}', Error: ${e.message}`);
7304
+ gobj_post_event(gobj, "EV_ON_CLOSE", {
7305
+ url,
7306
+ ws_seq,
7307
+ code: 1006,
7308
+ reason: e.message,
7309
+ was_clean: false
7310
+ }, gobj);
7223
7311
  return null;
7224
7312
  }
7225
7313
  if (!websocket) {
@@ -7478,6 +7566,7 @@ function ac_on_open(gobj, event, kw, src) {
7478
7566
  function ac_on_close(gobj, event, kw, src) {
7479
7567
  let priv = gobj.priv;
7480
7568
  let url = kw.url;
7569
+ if (kw.ws_seq !== void 0 && kw.ws_seq !== priv.ws_seq) return 0;
7481
7570
  set_remote_log_functions();
7482
7571
  log_warning(`${gobj_short_name(gobj)}: WebSocket closed ${url} (Code: ${kw.code}, Reason: "${kw.reason}", Clean: ${kw.was_clean})`);
7483
7572
  if (priv.websocket) {
@@ -7495,7 +7584,7 @@ function ac_on_close(gobj, event, kw, src) {
7495
7584
  remote_yuno_role: priv.remote_yuno_role,
7496
7585
  remote_yuno_service: priv.remote_yuno_service
7497
7586
  });
7498
- } else {
7587
+ } else if (!gobj_is_running(gobj)) {} else {
7499
7588
  log_warning(`${gobj_short_name(gobj)}: EV_ON_OPEN_ERROR cannot open ${url} (Code: ${kw.code}, Reason: "${kw.reason}", Clean: ${kw.was_clean})`);
7500
7589
  gobj_publish_event(gobj, "EV_ON_OPEN_ERROR", {
7501
7590
  url,
@@ -7957,6 +8046,7 @@ exports.gobj_yuno = gobj_yuno;
7957
8046
  exports.gobj_yuno_id = gobj_yuno_id;
7958
8047
  exports.gobj_yuno_name = gobj_yuno_name;
7959
8048
  exports.gobj_yuno_role = gobj_yuno_role;
8049
+ exports.html_markup = html_markup;
7960
8050
  exports.id_index_in_obj_list = id_index_in_obj_list;
7961
8051
  exports.index_in_list = index_in_list;
7962
8052
  exports.is_array = is_array;
@@ -7964,6 +8054,7 @@ exports.is_boolean = is_boolean;
7964
8054
  exports.is_date = is_date;
7965
8055
  exports.is_function = is_function;
7966
8056
  exports.is_gobj = is_gobj;
8057
+ exports.is_html_markup = is_html_markup;
7967
8058
  exports.is_metadata_key = is_metadata_key;
7968
8059
  exports.is_null = is_null;
7969
8060
  exports.is_number = is_number;
@@ -8055,6 +8146,7 @@ exports.set_console_log_enabled = set_console_log_enabled;
8055
8146
  exports.set_console_log_filter = set_console_log_filter;
8056
8147
  exports.set_log_callback = set_log_callback;
8057
8148
  exports.set_remote_log_functions = set_remote_log_functions;
8149
+ exports.set_strings_are_text = set_strings_are_text;
8058
8150
  exports.set_timeout = set_timeout;
8059
8151
  exports.set_timeout_periodic = set_timeout_periodic;
8060
8152
  exports.sprintf = sprintf;
@@ -8062,6 +8154,7 @@ exports.stats_parser = stats_parser;
8062
8154
  exports.str_in_list = str_in_list;
8063
8155
  exports.strcasecmp = strcasecmp;
8064
8156
  exports.strcmp = strcmp;
8157
+ exports.strings_are_text = strings_are_text;
8065
8158
  exports.strncmp = strncmp;
8066
8159
  exports.strs_in_list = strs_in_list;
8067
8160
  exports.strstr = strstr;