@xuda.io/xuda-worker-bundle 1.3.2695 → 1.3.2696

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.
Files changed (2) hide show
  1. package/index.js +132 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -422,6 +422,128 @@ func.runtime.platform.set_cursor = function (element, cursor) {
422
422
  node.style.cursor = cursor;
423
423
  }
424
424
  };
425
+ func.runtime.program.normalize_doc_for_runtime = function (doc) {
426
+ if (!doc || doc.__xudaRuntimeNormalized || !Array.isArray(doc.progUi) || !doc.progUi.length) {
427
+ return doc;
428
+ }
429
+
430
+ const normalize_tag_name = function (tag_name) {
431
+ return `${tag_name || ''}`.trim().toLowerCase();
432
+ };
433
+ const merge_attributes = function (target, source) {
434
+ const merged = { ...(target || {}) };
435
+ const source_attributes = source || {};
436
+ const keys = Object.keys(source_attributes);
437
+
438
+ for (let index = 0; index < keys.length; index++) {
439
+ const key = keys[index];
440
+ const value = source_attributes[key];
441
+ if (typeof value === 'undefined') {
442
+ continue;
443
+ }
444
+
445
+ if (key === 'class' && merged.class && value) {
446
+ const next_value = `${merged.class} ${value}`.trim();
447
+ merged.class = Array.from(new Set(next_value.split(/\s+/).filter(Boolean))).join(' ');
448
+ continue;
449
+ }
450
+
451
+ if (key === 'style' && merged.style && value) {
452
+ merged.style = `${merged.style}; ${value}`.trim();
453
+ continue;
454
+ }
455
+
456
+ if (typeof merged[key] === 'undefined') {
457
+ merged[key] = value;
458
+ }
459
+ }
460
+
461
+ return merged;
462
+ };
463
+ const normalize_nodes = function (nodes, state) {
464
+ const normalized_nodes = [];
465
+
466
+ for (let index = 0; index < (nodes || []).length; index++) {
467
+ const node = nodes[index];
468
+ if (!node || typeof node !== 'object') {
469
+ continue;
470
+ }
471
+
472
+ const tag_name = normalize_tag_name(node.tagName);
473
+
474
+ if (tag_name === '!doctype') {
475
+ state.changed = true;
476
+ continue;
477
+ }
478
+
479
+ if (tag_name === 'html') {
480
+ state.changed = true;
481
+ state.root_attributes = merge_attributes(state.root_attributes, node.attributes);
482
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
483
+ continue;
484
+ }
485
+
486
+ if (tag_name === 'head') {
487
+ state.changed = true;
488
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
489
+ continue;
490
+ }
491
+
492
+ if (tag_name === 'body') {
493
+ state.changed = true;
494
+ state.root_attributes = merge_attributes(state.root_attributes, node.attributes);
495
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
496
+ continue;
497
+ }
498
+
499
+ let next_node = node;
500
+ if (Array.isArray(node.children) && node.children.length) {
501
+ const next_children = normalize_nodes(node.children, state);
502
+ if (next_children !== node.children) {
503
+ next_node = {
504
+ ...node,
505
+ children: next_children,
506
+ };
507
+ state.changed = true;
508
+ }
509
+ }
510
+
511
+ normalized_nodes.push(next_node);
512
+ }
513
+
514
+ return normalized_nodes;
515
+ };
516
+
517
+ const [root_node, ...extra_nodes] = doc.progUi;
518
+ if (!root_node || typeof root_node !== 'object') {
519
+ return doc;
520
+ }
521
+
522
+ const state = {
523
+ changed: false,
524
+ root_attributes: {},
525
+ };
526
+
527
+ const normalized_children = normalize_nodes([...(root_node.children || []), ...extra_nodes], state);
528
+ const merged_attributes = merge_attributes(root_node.attributes, state.root_attributes);
529
+
530
+ if (!state.changed && !Object.keys(state.root_attributes).length) {
531
+ doc.__xudaRuntimeNormalized = true;
532
+ return doc;
533
+ }
534
+
535
+ return {
536
+ ...doc,
537
+ __xudaRuntimeNormalized: true,
538
+ progUi: [
539
+ {
540
+ ...root_node,
541
+ attributes: merged_attributes,
542
+ children: normalized_children,
543
+ },
544
+ ],
545
+ };
546
+ };
425
547
 
426
548
  func.runtime.env = {
427
549
  get_url_params: function () {
@@ -6955,6 +7077,13 @@ func.utils.DOCS_OBJ = {};
6955
7077
  func.utils.DOCS_OBJ.get = async function (SESSION_ID, idP) {
6956
7078
  if (!idP || idP === '0') return;
6957
7079
 
7080
+ const normalize_runtime_doc = function (doc) {
7081
+ if (!doc || !func.runtime.program?.normalize_doc_for_runtime) {
7082
+ return doc;
7083
+ }
7084
+ return func.runtime.program.normalize_doc_for_runtime(doc);
7085
+ };
7086
+
6958
7087
  var _session = SESSION_OBJ[SESSION_ID];
6959
7088
  const _app_id = _session.app_id;
6960
7089
  if (!DOCS_OBJ[_app_id]) {
@@ -6976,7 +7105,8 @@ func.utils.DOCS_OBJ.get = async function (SESSION_ID, idP) {
6976
7105
  }
6977
7106
  let val = _session.project_data?.programs?.[idP];
6978
7107
  if (val) {
6979
- return (DOCS_OBJ[_app_id][idP] = val);
7108
+ DOCS_OBJ[_app_id][idP] = normalize_runtime_doc(val);
7109
+ return DOCS_OBJ[_app_id][idP];
6980
7110
  }
6981
7111
  }
6982
7112
 
@@ -6984,7 +7114,7 @@ func.utils.DOCS_OBJ.get = async function (SESSION_ID, idP) {
6984
7114
  const module = await func.common.get_module(SESSION_ID, `xuda-progs-loader-module.mjs`);
6985
7115
 
6986
7116
  if (idP !== 'system') {
6987
- DOCS_OBJ[_app_id][idP] = await module.DOCS_OBJ_get(SESSION_ID, idP);
7117
+ DOCS_OBJ[_app_id][idP] = normalize_runtime_doc(await module.DOCS_OBJ_get(SESSION_ID, idP));
6988
7118
  if (DOCS_OBJ[_app_id][idP] && xu_isEmpty(DOCS_OBJ[_app_id][idP])) {
6989
7119
  await func.utils.remove_cached_objects(SESSION_ID);
6990
7120
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle",
3
- "version": "1.3.2695",
3
+ "version": "1.3.2696",
4
4
  "description": "xuda framework",
5
5
  "main": "index.js",
6
6
  "scripts": {