native-document 1.0.118 → 1.0.120

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.
@@ -372,7 +372,7 @@ var NativeComponents = (function (exports) {
372
372
  disable() {}
373
373
  };
374
374
  }
375
- var DebugManager = DebugManager$1;
375
+ var DebugManager$2 = DebugManager$1;
376
376
 
377
377
  /**
378
378
  *
@@ -846,17 +846,17 @@ var NativeComponents = (function (exports) {
846
846
  const method = methods[name];
847
847
 
848
848
  if (typeof method !== 'function') {
849
- DebugManager.warn('NDElement.extend', `"${name}" is not a function, skipping`);
849
+ DebugManager$2.warn('NDElement.extend', `"${name}" is not a function, skipping`);
850
850
  continue;
851
851
  }
852
852
 
853
853
  if (protectedMethods.has(name)) {
854
- DebugManager.error('NDElement.extend', `Cannot override protected method "${name}"`);
854
+ DebugManager$2.error('NDElement.extend', `Cannot override protected method "${name}"`);
855
855
  throw new NativeDocumentError(`Cannot override protected method "${name}"`);
856
856
  }
857
857
 
858
858
  if (NDElement.prototype[name]) {
859
- DebugManager.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
859
+ DebugManager$2.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
860
860
  }
861
861
 
862
862
  NDElement.prototype[name] = method;
@@ -1081,7 +1081,7 @@ var NativeComponents = (function (exports) {
1081
1081
  }
1082
1082
  }
1083
1083
  if (cleanedCount > 0) {
1084
- DebugManager.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
1084
+ DebugManager$2.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
1085
1085
  }
1086
1086
  }
1087
1087
  };
@@ -1486,6 +1486,8 @@ var NativeComponents = (function (exports) {
1486
1486
  };
1487
1487
  ObservableItem.prototype.trigger = noneTrigger;
1488
1488
 
1489
+
1490
+ const $setOperation = { action: 'set' };
1489
1491
  ObservableItem.prototype.$updateWithNewValue = function(newValue) {
1490
1492
  newValue = newValue?.__$isObservable ? newValue.val() : newValue;
1491
1493
  if(this.$currentValue === newValue) {
@@ -1493,7 +1495,7 @@ var NativeComponents = (function (exports) {
1493
1495
  }
1494
1496
  this.$previousValue = this.$currentValue;
1495
1497
  this.$currentValue = newValue;
1496
- this.trigger();
1498
+ this.trigger($setOperation);
1497
1499
  this.$previousValue = null;
1498
1500
  };
1499
1501
 
@@ -2093,6 +2095,10 @@ var NativeComponents = (function (exports) {
2093
2095
  return ElementCreator.createStaticTextNode(null, this);
2094
2096
  };
2095
2097
 
2098
+ Number.prototype.toNdElement = function () {
2099
+ return ElementCreator.createStaticTextNode(null, this.toString());
2100
+ };
2101
+
2096
2102
  Element.prototype.toNdElement = function () {
2097
2103
  return this;
2098
2104
  };
@@ -2351,8 +2357,124 @@ var NativeComponents = (function (exports) {
2351
2357
  processStyleAttribute: bindStyleAttribute,
2352
2358
  };
2353
2359
 
2360
+ function AnchorWithSentinel(name) {
2361
+ const instance = Reflect.construct(DocumentFragment, [], AnchorWithSentinel);
2362
+ const sentinel = document.createComment((name || '') + ' Anchor Sentinel');
2363
+ const events = {};
2364
+
2365
+ instance.appendChild(sentinel);
2366
+
2367
+ const observer = new MutationObserver(() => {
2368
+ if (sentinel.parentNode !== instance && !(sentinel.parentNode instanceof DocumentFragment)) {
2369
+ events.connected && events.connected(sentinel.parentNode);
2370
+ }
2371
+ });
2372
+
2373
+ observer.observe(document, { childList: true, subtree: true });
2374
+
2375
+
2376
+ instance.$sentinel = sentinel;
2377
+ instance.$observer = observer;
2378
+ instance.$events = events;
2379
+
2380
+ return instance;
2381
+ }
2382
+
2383
+ AnchorWithSentinel.prototype = Object.create(DocumentFragment.prototype);
2384
+ AnchorWithSentinel.prototype.constructor = AnchorWithSentinel;
2385
+
2386
+ AnchorWithSentinel.prototype.onConnected = function(callback) {
2387
+ this.$events.connected = callback;
2388
+ return this;
2389
+ };
2390
+
2391
+ AnchorWithSentinel.prototype.onConnectedOnce = function(callback) {
2392
+ this.$events.connected = (parent) => {
2393
+ callback(parent);
2394
+ this.$observer.disconnect();
2395
+ this.$events.connectedOnce = null;
2396
+ };
2397
+ };
2398
+
2399
+ function oneChildAnchorOverwriting(anchor, parent) {
2400
+
2401
+ anchor.remove = () => {
2402
+ anchor.append.apply(anchor, parent.childNodes);
2403
+ };
2404
+ anchor.getParent = () => parent;
2405
+
2406
+ anchor.appendChild = (child) => {
2407
+ child = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2408
+ parent.appendChild(child);
2409
+ };
2410
+
2411
+ anchor.appendChildRaw = parent.appendChild.bind(parent);
2412
+ anchor.append = anchor.appendChild;
2413
+ anchor.appendRaw = anchor.appendChildRaw;
2414
+
2415
+ anchor.insertAtStart = (child) => {
2416
+ child = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2417
+ parent.firstChild ? parent.insertBefore(child, parent.firstChild) : parent.appendChild(child);
2418
+ };
2419
+ anchor.insertAtStartRaw = (child) => {
2420
+ parent.firstChild ? parent.insertBefore(child, parent.firstChild) : parent.appendChild(child);
2421
+ };
2422
+
2423
+ anchor.appendElement = anchor.appendChild;
2424
+
2425
+ anchor.removeChildren = () => {
2426
+ parent.textContent = '';
2427
+ };
2428
+
2429
+ anchor.replaceContent = function(content) {
2430
+ const child = Validator.isElement(content) ? content : ElementCreator.getChild(content);
2431
+ parent.replaceChildren(child);
2432
+ };
2433
+
2434
+ anchor.replaceContentRaw = function(child) {
2435
+ parent.replaceChildren(child);
2436
+ };
2437
+ anchor.setContent = anchor.replaceContent;
2438
+
2439
+ anchor.insertBefore = (child, anchor) => {
2440
+ child = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2441
+ parent.insertBefore(child, anchor);
2442
+ };
2443
+ anchor.insertBeforeRaw = (child, anchor) => {
2444
+ parent.insertBefore(child, anchor);
2445
+ };
2446
+
2447
+ anchor.appendChildBefore = anchor.insertBefore;
2448
+ anchor.appendChildBeforeRaw = anchor.insertBeforeRaw;
2449
+
2450
+ anchor.clear = anchor.remove;
2451
+ anchor.detach = anchor.remove;
2452
+
2453
+ anchor.replaceChildren = function() {
2454
+ parent.replaceChildren(...arguments);
2455
+ };
2456
+
2457
+ anchor.getByIndex = (index) => {
2458
+ return parent.childNodes[index];
2459
+ };
2460
+ }
2461
+
2354
2462
  function Anchor(name, isUniqueChild = false) {
2355
- const anchorFragment = document.createDocumentFragment();
2463
+ const anchorFragment = new AnchorWithSentinel(name);
2464
+
2465
+ /**
2466
+ * State :
2467
+ * 1. Not injected in the DOM
2468
+ * 2. Injected in the DOM and should be the only child of parent
2469
+ * 3. Injected in the DOM and the parent may have other children
2470
+ */
2471
+
2472
+ anchorFragment.onConnectedOnce((parent) => {
2473
+ if(isUniqueChild) {
2474
+ oneChildAnchorOverwriting(anchorFragment, parent);
2475
+ }
2476
+ });
2477
+
2356
2478
  anchorFragment.__Anchor__ = true;
2357
2479
 
2358
2480
  const anchorStart = document.createComment('Anchor Start : '+name);
@@ -2366,30 +2488,32 @@ var NativeComponents = (function (exports) {
2366
2488
  anchorFragment.nativeAppend = anchorFragment.append;
2367
2489
 
2368
2490
  const isParentUniqueChild = isUniqueChild
2369
- ? () => true
2370
- : (parent) => (parent.firstChild === anchorStart && parent.lastChild === anchorEnd);
2491
+ ? () => true: (parent) => (parent.firstChild === anchorStart && parent.lastChild === anchorEnd);
2371
2492
 
2372
2493
  const insertBefore = function(parent, child, target) {
2373
2494
  const childElement = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2495
+ insertBeforeRaw(parent, childElement, target);
2496
+ };
2497
+
2498
+ const insertBeforeRaw = function(parent, child, target) {
2374
2499
  if(parent === anchorFragment) {
2375
- parent.nativeInsertBefore(childElement, target);
2500
+ parent.nativeInsertBefore(child, target);
2376
2501
  return;
2377
2502
  }
2378
2503
  if(isParentUniqueChild(parent) && target === anchorEnd) {
2379
- parent.append(childElement, target);
2504
+ parent.append(child, target);
2380
2505
  return;
2381
2506
  }
2382
- parent.insertBefore(childElement, target);
2507
+ parent.insertBefore(child, target);
2383
2508
  };
2384
2509
 
2385
- anchorFragment.appendElement = function(child, before = null) {
2510
+ anchorFragment.appendElement = function(child) {
2386
2511
  const parentNode = anchorStart.parentNode;
2387
- const targetBefore = before || anchorEnd;
2388
2512
  if(parentNode === anchorFragment) {
2389
- parentNode.nativeInsertBefore(child, targetBefore);
2513
+ parentNode.nativeInsertBefore(child, anchorEnd);
2390
2514
  return;
2391
2515
  }
2392
- parentNode?.insertBefore(child, targetBefore);
2516
+ parentNode.insertBefore(child, anchorEnd);
2393
2517
  };
2394
2518
 
2395
2519
  anchorFragment.appendChild = function(child, before = null) {
@@ -2402,8 +2526,32 @@ var NativeComponents = (function (exports) {
2402
2526
  insertBefore(parent, child, before);
2403
2527
  };
2404
2528
 
2405
- anchorFragment.append = function(...args ) {
2406
- return anchorFragment.appendChild(args);
2529
+ anchorFragment.appendChildRaw = function(child, before = null) {
2530
+ const parent = anchorEnd.parentNode;
2531
+ if(!parent) {
2532
+ DebugManager.error('Anchor', 'Anchor : parent not found', child);
2533
+ return;
2534
+ }
2535
+ before = before ?? anchorEnd;
2536
+ insertBeforeRaw(parent, child, before);
2537
+ };
2538
+
2539
+ anchorFragment.getParent = () => anchorEnd.parentNode;
2540
+ anchorFragment.append = anchorFragment.appendChild;
2541
+ anchorFragment.appendRaw = anchorFragment.appendChildRaw;
2542
+
2543
+ anchorFragment.insertAtStart = function(child) {
2544
+ child = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2545
+ anchorFragment.insertAtStartRaw(child);
2546
+ };
2547
+
2548
+ anchorFragment.insertAtStartRaw = function(child) {
2549
+ const parentNode = anchorStart.parentNode;
2550
+ if(parentNode === anchorFragment) {
2551
+ parentNode.nativeInsertBefore(child, anchorStart);
2552
+ return;
2553
+ }
2554
+ parentNode.insertBefore(child, anchorStart);
2407
2555
  };
2408
2556
 
2409
2557
  anchorFragment.removeChildren = function() {
@@ -2430,6 +2578,7 @@ var NativeComponents = (function (exports) {
2430
2578
  return;
2431
2579
  }
2432
2580
  if(isParentUniqueChild(parent)) {
2581
+ anchorFragment.append.apply(anchorFragment, parent.childNodes);
2433
2582
  parent.replaceChildren(anchorStart, anchorEnd);
2434
2583
  return;
2435
2584
  }
@@ -2449,6 +2598,10 @@ var NativeComponents = (function (exports) {
2449
2598
 
2450
2599
  anchorFragment.replaceContent = function(child) {
2451
2600
  const childElement = Validator.isElement(child) ? child : ElementCreator.getChild(child);
2601
+ anchorFragment.replaceContentRaw(childElement);
2602
+ };
2603
+
2604
+ anchorFragment.replaceContentRaw = function(child) {
2452
2605
  const parent = anchorEnd.parentNode;
2453
2606
  if(!parent) {
2454
2607
  return;
@@ -2462,10 +2615,10 @@ var NativeComponents = (function (exports) {
2462
2615
  };
2463
2616
 
2464
2617
  anchorFragment.setContent = anchorFragment.replaceContent;
2618
+ anchorFragment.setContentRaw = anchorFragment.replaceContentRaw;
2465
2619
 
2466
- anchorFragment.insertBefore = function(child, anchor = null) {
2467
- anchorFragment.appendChild(child, anchor);
2468
- };
2620
+ anchorFragment.insertBefore = anchorFragment.appendChild;
2621
+ anchorFragment.insertBeforeRaw = anchorFragment.appendChildRaw;
2469
2622
 
2470
2623
  anchorFragment.endElement = function() {
2471
2624
  return anchorEnd;
@@ -2474,9 +2627,11 @@ var NativeComponents = (function (exports) {
2474
2627
  anchorFragment.startElement = function() {
2475
2628
  return anchorStart;
2476
2629
  };
2630
+
2477
2631
  anchorFragment.restore = function() {
2478
2632
  anchorFragment.appendChild(anchorFragment);
2479
2633
  };
2634
+
2480
2635
  anchorFragment.clear = anchorFragment.remove;
2481
2636
  anchorFragment.detach = anchorFragment.remove;
2482
2637
 
@@ -2804,9 +2959,10 @@ var NativeComponents = (function (exports) {
2804
2959
  * @returns {Text}
2805
2960
  */
2806
2961
  const createTextNode = (value) => {
2807
- return (Validator.isObservable(value))
2808
- ? ElementCreator.createObservableNode(null, value)
2809
- : ElementCreator.createStaticTextNode(null, value);
2962
+ if(value) {
2963
+ return value.toNdElement();
2964
+ }
2965
+ return ElementCreator.createTextNode();
2810
2966
  };
2811
2967
 
2812
2968
 
@@ -2900,8 +3056,9 @@ var NativeComponents = (function (exports) {
2900
3056
  const methods = Object.keys(this.$ndMethods);
2901
3057
  if(methods.length === 1) {
2902
3058
  const methodName = methods[0];
3059
+ const callback = this.$ndMethods[methodName];
2903
3060
  steps.push((clonedNode, data) => {
2904
- clonedNode.nd[methodName](this.$ndMethods[methodName].bind(clonedNode, ...data));
3061
+ clonedNode.nd[methodName](callback.bind(clonedNode, ...data));
2905
3062
  });
2906
3063
  } else {
2907
3064
  steps.push((clonedNode, data) => {
@@ -3152,6 +3309,8 @@ var NativeComponents = (function (exports) {
3152
3309
  };
3153
3310
  });
3154
3311
 
3312
+ const $clearEvent = { action: 'clear' };
3313
+
3155
3314
  /**
3156
3315
  * Removes all items from the array and triggers an update.
3157
3316
  *
@@ -3165,7 +3324,7 @@ var NativeComponents = (function (exports) {
3165
3324
  return;
3166
3325
  }
3167
3326
  this.$currentValue.length = 0;
3168
- this.trigger({ action: 'clear' });
3327
+ this.trigger($clearEvent);
3169
3328
  return true;
3170
3329
  };
3171
3330
 
@@ -3787,7 +3946,7 @@ var NativeComponents = (function (exports) {
3787
3946
  const $getStoreOrThrow = (method, name) => {
3788
3947
  const item = $stores.get(name);
3789
3948
  if (!item) {
3790
- DebugManager.error('Store', `Store.${method}('${name}') : store not found. Did you call Store.create('${name}') first?`);
3949
+ DebugManager$2.error('Store', `Store.${method}('${name}') : store not found. Did you call Store.create('${name}') first?`);
3791
3950
  throw new NativeDocumentError(
3792
3951
  `Store.${method}('${name}') : store not found.`
3793
3952
  );
@@ -3800,7 +3959,7 @@ var NativeComponents = (function (exports) {
3800
3959
  */
3801
3960
  const $applyReadOnly = (observer, name, context) => {
3802
3961
  const readOnlyError = (method) => () => {
3803
- DebugManager.error('Store', `Store.${context}('${name}') is read-only. '${method}()' is not allowed.`);
3962
+ DebugManager$2.error('Store', `Store.${context}('${name}') is read-only. '${method}()' is not allowed.`);
3804
3963
  throw new NativeDocumentError(
3805
3964
  `Store.${context}('${name}') is read-only.`
3806
3965
  );
@@ -3831,7 +3990,7 @@ var NativeComponents = (function (exports) {
3831
3990
  */
3832
3991
  create(name, value) {
3833
3992
  if ($stores.has(name)) {
3834
- DebugManager.warn('Store', `Store.create('${name}') : a store with this name already exists. Use Store.get('${name}') to retrieve it.`);
3993
+ DebugManager$2.warn('Store', `Store.create('${name}') : a store with this name already exists. Use Store.get('${name}') to retrieve it.`);
3835
3994
  throw new NativeDocumentError(
3836
3995
  `Store.create('${name}') : a store with this name already exists.`
3837
3996
  );
@@ -3852,7 +4011,7 @@ var NativeComponents = (function (exports) {
3852
4011
  */
3853
4012
  createResettable(name, value) {
3854
4013
  if ($stores.has(name)) {
3855
- DebugManager.warn('Store', `Store.createResettable('${name}') : a store with this name already exists.`);
4014
+ DebugManager$2.warn('Store', `Store.createResettable('${name}') : a store with this name already exists.`);
3856
4015
  throw new NativeDocumentError(
3857
4016
  `Store.createResettable('${name}') : a store with this name already exists.`
3858
4017
  );
@@ -3888,7 +4047,7 @@ var NativeComponents = (function (exports) {
3888
4047
  */
3889
4048
  createComposed(name, computation, dependencies) {
3890
4049
  if ($stores.has(name)) {
3891
- DebugManager.warn('Store', `Store.createComposed('${name}') : a store with this name already exists.`);
4050
+ DebugManager$2.warn('Store', `Store.createComposed('${name}') : a store with this name already exists.`);
3892
4051
  throw new NativeDocumentError(
3893
4052
  `Store.createComposed('${name}') : a store with this name already exists.`
3894
4053
  );
@@ -3911,7 +4070,7 @@ var NativeComponents = (function (exports) {
3911
4070
  }
3912
4071
  const depItem = $stores.get(depName);
3913
4072
  if (!depItem) {
3914
- DebugManager.error('Store', `Store.createComposed('${name}') : dependency '${depName}' not found. Create it first.`);
4073
+ DebugManager$2.error('Store', `Store.createComposed('${name}') : dependency '${depName}' not found. Create it first.`);
3915
4074
  throw new NativeDocumentError(
3916
4075
  `Store.createComposed('${name}') : dependency store '${depName}' not found.`
3917
4076
  );
@@ -3945,13 +4104,13 @@ var NativeComponents = (function (exports) {
3945
4104
  reset(name) {
3946
4105
  const item = $getStoreOrThrow('reset', name);
3947
4106
  if (item.composed) {
3948
- DebugManager.error('Store', `Store.reset('${name}') : composed stores cannot be reset. Their value is derived from dependencies.`);
4107
+ DebugManager$2.error('Store', `Store.reset('${name}') : composed stores cannot be reset. Their value is derived from dependencies.`);
3949
4108
  throw new NativeDocumentError(
3950
4109
  `Store.reset('${name}') : composed stores cannot be reset.`
3951
4110
  );
3952
4111
  }
3953
4112
  if (!item.resettable) {
3954
- DebugManager.error('Store', `Store.reset('${name}') : this store is not resettable. Use Store.createResettable('${name}', value) instead of Store.create().`);
4113
+ DebugManager$2.error('Store', `Store.reset('${name}') : this store is not resettable. Use Store.createResettable('${name}', value) instead of Store.create().`);
3955
4114
  throw new NativeDocumentError(
3956
4115
  `Store.reset('${name}') : this store is not resettable. Use Store.createResettable('${name}', value) instead of Store.create().`
3957
4116
  );
@@ -3972,7 +4131,7 @@ var NativeComponents = (function (exports) {
3972
4131
  const item = $getStoreOrThrow('use', name);
3973
4132
 
3974
4133
  if (item.composed) {
3975
- DebugManager.error('Store', `Store.use('${name}') : composed stores are read-only. Use Store.follow('${name}') instead.`);
4134
+ DebugManager$2.error('Store', `Store.use('${name}') : composed stores are read-only. Use Store.follow('${name}') instead.`);
3976
4135
  throw new NativeDocumentError(
3977
4136
  `Store.use('${name}') : composed stores are read-only. Use Store.follow('${name}') instead.`
3978
4137
  );
@@ -4039,7 +4198,7 @@ var NativeComponents = (function (exports) {
4039
4198
  get(name) {
4040
4199
  const item = $stores.get(name);
4041
4200
  if (!item) {
4042
- DebugManager.warn('Store', `Store.get('${name}') : store not found.`);
4201
+ DebugManager$2.warn('Store', `Store.get('${name}') : store not found.`);
4043
4202
  return null;
4044
4203
  }
4045
4204
  return item.observer;
@@ -4061,7 +4220,7 @@ var NativeComponents = (function (exports) {
4061
4220
  delete(name) {
4062
4221
  const item = $stores.get(name);
4063
4222
  if (!item) {
4064
- DebugManager.warn('Store', `Store.delete('${name}') : store not found, nothing to delete.`);
4223
+ DebugManager$2.warn('Store', `Store.delete('${name}') : store not found, nothing to delete.`);
4065
4224
  return;
4066
4225
  }
4067
4226
  item.subscribers.forEach(follower => follower.destroy());
@@ -4163,7 +4322,7 @@ var NativeComponents = (function (exports) {
4163
4322
  return undefined;
4164
4323
  },
4165
4324
  set(target, prop, value) {
4166
- DebugManager.error('Store', `Forbidden: You cannot overwrite the store key '${String(prop)}'. Use .use('${String(prop)}').set(value) instead.`);
4325
+ DebugManager$2.error('Store', `Forbidden: You cannot overwrite the store key '${String(prop)}'. Use .use('${String(prop)}').set(value) instead.`);
4167
4326
  throw new NativeDocumentError(`Store structure is immutable. Use .set() on the observable.`);
4168
4327
  },
4169
4328
  deleteProperty(target, prop) {