@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.
@@ -5401,6 +5401,68 @@ function safeSrc(url) {
5401
5401
  /***************************************************************************
5402
5402
  *
5403
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
+ }
5404
5466
  function createOneHtml(htmlString) {
5405
5467
  const template = document.createElement("template");
5406
5468
  template.innerHTML = htmlString.trim();
@@ -5562,6 +5624,7 @@ parent: document.body
5562
5624
  * if string begins with '<' then
5563
5625
  * it will be converted to HTMLElement
5564
5626
  * content = createOneHtml(string)
5627
+ * | html_markup(string): markup, said out loud (any mode)
5565
5628
  * ⚠️ So a string is MARKUP when it starts with '<': never
5566
5629
  * pass DATA (a value from a backend, a name a user typed)
5567
5630
  * as a bare string, or "<img src=x onerror=...>" runs.
@@ -5571,6 +5634,8 @@ parent: document.body
5571
5634
  * appended as it is and a string is always text -- but a
5572
5635
  * list whose first item is a string is read as ONE
5573
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().
5574
5639
  * if attrs['i18n'] is not empty then (or 'data-i18n' or 'data_i18next')
5575
5640
  * it will be overridden with the translation
5576
5641
  * content = translate(attrs['i18n']));
@@ -5613,19 +5678,24 @@ function createElement2(description, translate_fn) {
5613
5678
  el.setAttribute("data-i18n", data_i18next);
5614
5679
  }
5615
5680
  } else el.setAttribute(attr, attrs[attr]);
5616
- if (is_string(content)) {
5681
+ if (is_html_markup(content)) el.appendChild(markup_fragment(content));
5682
+ else if (is_string(content)) {
5617
5683
  content = content.trim();
5618
- if (content.length > 0 && content[0] === "<") el.appendChild(createOneHtml(content));
5619
- else if (translate_fn && data_i18next) {
5620
- let translated = translate_fn(data_i18next);
5621
- el.textContent = translated ? translated : content;
5622
- } 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
+ }
5623
5692
  } else if (is_array(content)) {
5624
5693
  if (content.length > 0) {
5625
5694
  if (is_string(content[0])) el.appendChild(createElement2(content, translate_fn));
5626
5695
  else for (let child of content) if (Array.isArray(child)) {
5627
5696
  if (child.length > 0) el.appendChild(createElement2(child, translate_fn));
5628
5697
  } else if (child instanceof Node) el.appendChild(child);
5698
+ else if (is_html_markup(child)) el.appendChild(markup_fragment(child));
5629
5699
  else if (is_string(child)) el.appendChild(document.createTextNode(child));
5630
5700
  else if (child !== null && child !== void 0 && child !== false) log_error(`createElement2: child ignored, type ${typeof child}`);
5631
5701
  }
@@ -7976,6 +8046,7 @@ exports.gobj_yuno = gobj_yuno;
7976
8046
  exports.gobj_yuno_id = gobj_yuno_id;
7977
8047
  exports.gobj_yuno_name = gobj_yuno_name;
7978
8048
  exports.gobj_yuno_role = gobj_yuno_role;
8049
+ exports.html_markup = html_markup;
7979
8050
  exports.id_index_in_obj_list = id_index_in_obj_list;
7980
8051
  exports.index_in_list = index_in_list;
7981
8052
  exports.is_array = is_array;
@@ -7983,6 +8054,7 @@ exports.is_boolean = is_boolean;
7983
8054
  exports.is_date = is_date;
7984
8055
  exports.is_function = is_function;
7985
8056
  exports.is_gobj = is_gobj;
8057
+ exports.is_html_markup = is_html_markup;
7986
8058
  exports.is_metadata_key = is_metadata_key;
7987
8059
  exports.is_null = is_null;
7988
8060
  exports.is_number = is_number;
@@ -8074,6 +8146,7 @@ exports.set_console_log_enabled = set_console_log_enabled;
8074
8146
  exports.set_console_log_filter = set_console_log_filter;
8075
8147
  exports.set_log_callback = set_log_callback;
8076
8148
  exports.set_remote_log_functions = set_remote_log_functions;
8149
+ exports.set_strings_are_text = set_strings_are_text;
8077
8150
  exports.set_timeout = set_timeout;
8078
8151
  exports.set_timeout_periodic = set_timeout_periodic;
8079
8152
  exports.sprintf = sprintf;
@@ -8081,6 +8154,7 @@ exports.stats_parser = stats_parser;
8081
8154
  exports.str_in_list = str_in_list;
8082
8155
  exports.strcasecmp = strcasecmp;
8083
8156
  exports.strcmp = strcmp;
8157
+ exports.strings_are_text = strings_are_text;
8084
8158
  exports.strncmp = strncmp;
8085
8159
  exports.strs_in_list = strs_in_list;
8086
8160
  exports.strstr = strstr;