@yuneta/gobj-js 7.26.2 → 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.
@@ -5402,6 +5402,68 @@ var gobjJs = (function(exports) {
5402
5402
  /***************************************************************************
5403
5403
  *
5404
5404
  ***************************************************************************/
5405
+ /***************************************************************************
5406
+ * Markup that createElement2() inserts AS HTML, said out loud.
5407
+ *
5408
+ * createElement2() reads a string that starts with '<' as markup. That
5409
+ * decision depends on the CONTENT of the string, not on who wrote it, so
5410
+ * a value from a backend or typed by a user that starts with '<' runs as
5411
+ * html. html_markup() is the explicit way to ask for markup: the string
5412
+ * is the caller's own (an icon, an svg), never data.
5413
+ *
5414
+ * ['span', {class: 'icon'}, html_markup('<i class="yi-copy"></i>')]
5415
+ *
5416
+ * With set_strings_are_text(true) a plain string is ALWAYS text and
5417
+ * html_markup() is the only way to insert markup.
5418
+ ***************************************************************************/
5419
+ var HtmlMarkup = class {
5420
+ constructor(html) {
5421
+ this.html = html;
5422
+ Object.freeze(this);
5423
+ }
5424
+ toString() {
5425
+ return this.html;
5426
+ }
5427
+ };
5428
+ function html_markup(html) {
5429
+ if (typeof html !== "string") {
5430
+ log_error(`html_markup: not a string, type ${typeof html}`);
5431
+ return new HtmlMarkup("");
5432
+ }
5433
+ return new HtmlMarkup(html);
5434
+ }
5435
+ function is_html_markup(v) {
5436
+ return v instanceof HtmlMarkup;
5437
+ }
5438
+ function markup_fragment(markup) {
5439
+ const template = document.createElement("template");
5440
+ template.innerHTML = markup.html.trim();
5441
+ return template.content;
5442
+ }
5443
+ var __strings_are_text__ = false;
5444
+ var __markup_strings_warned__ = /* @__PURE__ */ new Set();
5445
+ /***************************************************************************
5446
+ * The mode of createElement2() for a plain string that starts with '<':
5447
+ * false (default): it is markup, as it always was.
5448
+ * true: it is TEXT; markup is asked for with html_markup().
5449
+ * An app turns it on once its own code and the libraries it mounts pass
5450
+ * every deliberate markup through html_markup(). A string that starts
5451
+ * with '<' is then logged once (a deliberate one not migrated yet, or a
5452
+ * datum that used to run as html).
5453
+ ***************************************************************************/
5454
+ function set_strings_are_text(on) {
5455
+ __strings_are_text__ = !!on;
5456
+ }
5457
+ function strings_are_text() {
5458
+ return __strings_are_text__;
5459
+ }
5460
+ function warn_markup_string(content) {
5461
+ if (__markup_strings_warned__.size >= 100) return;
5462
+ const key = content.slice(0, 80);
5463
+ if (__markup_strings_warned__.has(key)) return;
5464
+ __markup_strings_warned__.add(key);
5465
+ log_warning(`createElement2: a string that starts with '<' is TEXT (set_strings_are_text): use html_markup() if it is markup: ${JSON.stringify(key)}`);
5466
+ }
5405
5467
  function createOneHtml(htmlString) {
5406
5468
  const template = document.createElement("template");
5407
5469
  template.innerHTML = htmlString.trim();
@@ -5563,6 +5625,7 @@ var gobjJs = (function(exports) {
5563
5625
  * if string begins with '<' then
5564
5626
  * it will be converted to HTMLElement
5565
5627
  * content = createOneHtml(string)
5628
+ * | html_markup(string): markup, said out loud (any mode)
5566
5629
  * ⚠️ So a string is MARKUP when it starts with '<': never
5567
5630
  * pass DATA (a value from a backend, a name a user typed)
5568
5631
  * as a bare string, or "<img src=x onerror=...>" runs.
@@ -5572,6 +5635,8 @@ var gobjJs = (function(exports) {
5572
5635
  * appended as it is and a string is always text -- but a
5573
5636
  * list whose first item is a string is read as ONE
5574
5637
  * description (the string is its tag).
5638
+ * With set_strings_are_text(true) a plain string is
5639
+ * always text, and markup only comes in html_markup().
5575
5640
  * if attrs['i18n'] is not empty then (or 'data-i18n' or 'data_i18next')
5576
5641
  * it will be overridden with the translation
5577
5642
  * content = translate(attrs['i18n']));
@@ -5614,19 +5679,24 @@ var gobjJs = (function(exports) {
5614
5679
  el.setAttribute("data-i18n", data_i18next);
5615
5680
  }
5616
5681
  } else el.setAttribute(attr, attrs[attr]);
5617
- if (is_string(content)) {
5682
+ if (is_html_markup(content)) el.appendChild(markup_fragment(content));
5683
+ else if (is_string(content)) {
5618
5684
  content = content.trim();
5619
- if (content.length > 0 && content[0] === "<") el.appendChild(createOneHtml(content));
5620
- else if (translate_fn && data_i18next) {
5621
- let translated = translate_fn(data_i18next);
5622
- el.textContent = translated ? translated : content;
5623
- } else el.textContent = content;
5685
+ if (content.length > 0 && content[0] === "<" && !__strings_are_text__) el.appendChild(createOneHtml(content));
5686
+ else {
5687
+ if (content.length > 0 && content[0] === "<") warn_markup_string(content);
5688
+ if (translate_fn && data_i18next) {
5689
+ let translated = translate_fn(data_i18next);
5690
+ el.textContent = translated ? translated : content;
5691
+ } else el.textContent = content;
5692
+ }
5624
5693
  } else if (is_array(content)) {
5625
5694
  if (content.length > 0) {
5626
5695
  if (is_string(content[0])) el.appendChild(createElement2(content, translate_fn));
5627
5696
  else for (let child of content) if (Array.isArray(child)) {
5628
5697
  if (child.length > 0) el.appendChild(createElement2(child, translate_fn));
5629
5698
  } else if (child instanceof Node) el.appendChild(child);
5699
+ else if (is_html_markup(child)) el.appendChild(markup_fragment(child));
5630
5700
  else if (is_string(child)) el.appendChild(document.createTextNode(child));
5631
5701
  else if (child !== null && child !== void 0 && child !== false) log_error(`createElement2: child ignored, type ${typeof child}`);
5632
5702
  }
@@ -7977,6 +8047,7 @@ var gobjJs = (function(exports) {
7977
8047
  exports.gobj_yuno_id = gobj_yuno_id;
7978
8048
  exports.gobj_yuno_name = gobj_yuno_name;
7979
8049
  exports.gobj_yuno_role = gobj_yuno_role;
8050
+ exports.html_markup = html_markup;
7980
8051
  exports.id_index_in_obj_list = id_index_in_obj_list;
7981
8052
  exports.index_in_list = index_in_list;
7982
8053
  exports.is_array = is_array;
@@ -7984,6 +8055,7 @@ var gobjJs = (function(exports) {
7984
8055
  exports.is_date = is_date;
7985
8056
  exports.is_function = is_function;
7986
8057
  exports.is_gobj = is_gobj;
8058
+ exports.is_html_markup = is_html_markup;
7987
8059
  exports.is_metadata_key = is_metadata_key;
7988
8060
  exports.is_null = is_null;
7989
8061
  exports.is_number = is_number;
@@ -8075,6 +8147,7 @@ var gobjJs = (function(exports) {
8075
8147
  exports.set_console_log_filter = set_console_log_filter;
8076
8148
  exports.set_log_callback = set_log_callback;
8077
8149
  exports.set_remote_log_functions = set_remote_log_functions;
8150
+ exports.set_strings_are_text = set_strings_are_text;
8078
8151
  exports.set_timeout = set_timeout;
8079
8152
  exports.set_timeout_periodic = set_timeout_periodic;
8080
8153
  exports.sprintf = sprintf;
@@ -8082,6 +8155,7 @@ var gobjJs = (function(exports) {
8082
8155
  exports.str_in_list = str_in_list;
8083
8156
  exports.strcasecmp = strcasecmp;
8084
8157
  exports.strcmp = strcmp;
8158
+ exports.strings_are_text = strings_are_text;
8085
8159
  exports.strncmp = strncmp;
8086
8160
  exports.strs_in_list = strs_in_list;
8087
8161
  exports.strstr = strstr;