@3sln/dodo 0.0.1 → 0.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/vdom.js +73 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@3sln/dodo",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A minimal, configurable virtual DOM library.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/vdom.js CHANGED
@@ -91,10 +91,10 @@ function getPathFromElement(element) {
91
91
  let current = element;
92
92
  while (current) {
93
93
  path.push(current);
94
- if (current.shadowRoot) {
95
- current = current.shadowRoot.host;
94
+ if (current.parentElement) {
95
+ current = current.parentElement;
96
96
  } else {
97
- current = current.parentNode;
97
+ current = current.getRootNode()?.host;
98
98
  }
99
99
  }
100
100
  return path;
@@ -448,21 +448,34 @@ export default (userSettings) => {
448
448
  return domNode;
449
449
  }
450
450
 
451
- function cleanupTarget(target) {
451
+ function cleanupTargetChildren(target) {
452
452
  if (target.children) {
453
453
  for (const child of target.children) {
454
454
  cleanupTarget(child);
455
455
  }
456
456
  }
457
+ }
457
458
 
459
+ function cleanupTarget(target) {
458
460
  const state = target[NODE_STATE];
459
461
  if (state) {
462
+
460
463
  switch (state.vdom.type) {
461
464
  case ELEMENT_NODE:
465
+ reconcileElementProps(target, {});
466
+ cleanupTargetChildren(target);
467
+ delete target[NODE_STATE];
468
+ break;
462
469
  case OPAQUE_NODE:
463
470
  reconcileElementProps(target, {});
471
+ delete target[NODE_STATE];
472
+ break;
473
+ case ALIAS_NODE:
474
+ cleanupTargetChildren(target);
475
+ delete target[NODE_STATE];
464
476
  break;
465
477
  case SPECIAL_NODE:
478
+ delete target[NODE_STATE];
466
479
  try {
467
480
  state.vdom.tag.detach?.(target);
468
481
  } catch (err) {
@@ -475,7 +488,6 @@ export default (userSettings) => {
475
488
  } catch (err) {
476
489
  console.error(err);
477
490
  }
478
- delete target[NODE_STATE];
479
491
  }
480
492
  }
481
493
 
@@ -565,41 +577,65 @@ export default (userSettings) => {
565
577
 
566
578
  const moveBefore = window.Element.prototype.moveBefore;
567
579
  const insertBefore = window.Element.prototype.insertBefore;
568
- if (target.isConnected && typeof moveBefore === 'function') {
569
- for (let i = 0; i < newDomChildren.length; i++) {
570
- const newChild = newDomChildren[i];
571
- const existingChildAtPosition = target.childNodes[i];
572
- if (newChild !== existingChildAtPosition) {
573
- (newChild.isConnected ? moveBefore : insertBefore).call(target, newChild, existingChildAtPosition);
580
+ if (target.isConnected) {
581
+ if (typeof moveBefore === 'function') {
582
+ for (let i = 0; i < newDomChildren.length; i++) {
583
+ const newChild = newDomChildren[i];
584
+ const existingChildAtPosition = target.childNodes[i];
585
+ if (newChild !== existingChildAtPosition) {
586
+ (newChild.isConnected ? moveBefore : insertBefore).call(target, newChild, existingChildAtPosition);
587
+ }
588
+ const state = newChild[NODE_STATE];
589
+ if (state?.newVdom) {
590
+ if (!state.vdom) {
591
+ try {
592
+ state.newVdom.hooks?.$attach?.(newChild);
593
+ if (state.newVdom.type === SPECIAL_NODE) {
594
+ state.newVdom.tag.attach?.(newChild);
595
+ }
596
+ } catch (err) {
597
+ console.error(err);
598
+ }
599
+ }
600
+ reconcileNode(newChild);
601
+ }
574
602
  }
575
- const state = newChild[NODE_STATE];
576
- if (state?.newVdom) {
577
- if (!state.vdom) {
578
- try {
579
- state.newVdom.hooks?.$attach?.(newChild);
580
- if (state.newVdom.type === SPECIAL_NODE) {
581
- state.newVdom.tag.attach?.(newChild);
603
+ } else {
604
+ const doc = target.ownerDocument;
605
+ let focusWithin = documentToFocusWithinSet.get(doc);
606
+ if (!focusWithin) {
607
+ focusWithin = installFocusTrackingForDocument(doc);
608
+ }
609
+ for (let i = 0; i < newDomChildren.length; i++) {
610
+ const newChild = newDomChildren[i];
611
+ const existingChildAtPosition = target.childNodes[i];
612
+ if (newChild !== existingChildAtPosition) {
613
+ if (!focusWithin.has(newChild)) {
614
+ insertBefore.call(target, newChild, existingChildAtPosition);
615
+ }
616
+ }
617
+ const state = newChild[NODE_STATE];
618
+ if (state?.newVdom) {
619
+ if (!state.vdom) {
620
+ try {
621
+ state.newVdom.hooks?.$attach?.(newChild);
622
+ if (state.newVdom.type === SPECIAL_NODE) {
623
+ state.newVdom.tag.attach?.(newChild);
624
+ }
625
+ } catch (err) {
626
+ console.error(err);
582
627
  }
583
- } catch (err) {
584
- console.error(err);
585
628
  }
629
+ reconcileNode(newChild);
586
630
  }
587
- reconcileNode(newChild);
588
631
  }
589
632
  }
590
633
  } else {
591
- const doc = target.ownerDocument;
592
- let focusWithin = documentToFocusWithinSet.get(doc);
593
- if (!focusWithin) {
594
- focusWithin = installFocusTrackingForDocument(doc);
595
- }
596
634
  for (let i = 0; i < newDomChildren.length; i++) {
597
635
  const newChild = newDomChildren[i];
598
636
  const existingChildAtPosition = target.childNodes[i];
599
637
  if (newChild !== existingChildAtPosition) {
600
- if (!focusWithin.has(newChild)) {
601
- insertBefore.call(target, newChild, existingChildAtPosition);
602
- }
638
+ insertBefore.call(target, newChild, existingChildAtPosition);
603
639
  }
604
640
  const state = newChild[NODE_STATE];
605
641
  if (state?.newVdom) {
@@ -620,8 +656,14 @@ export default (userSettings) => {
620
656
  }
621
657
 
622
658
  function reconcile(target, vdom) {
659
+ const state = target[NODE_STATE];
623
660
  if (vdom === null || vdom === undefined) {
624
- cleanupTarget(target);
661
+ if (state) {
662
+ cleanupTarget(target);
663
+ } else {
664
+ cleanupTargetChildren(target);
665
+ }
666
+ target.replaceChildren();
625
667
  return;
626
668
  }
627
669
 
@@ -631,7 +673,6 @@ export default (userSettings) => {
631
673
  }
632
674
 
633
675
  if (vdom instanceof VNode) {
634
- let state = target[NODE_STATE];
635
676
  if (state) {
636
677
  if (state.vdom.type === vdom.type) {
637
678
  if (shouldUpdate(state.vdom.args, vdom.args)) {
@@ -652,7 +693,7 @@ export default (userSettings) => {
652
693
  break;
653
694
  }
654
695
 
655
- target[NODE_STATE] = state = { originalProps: {}, newVdom: vdom };
696
+ target[NODE_STATE] = { originalProps: {}, newVdom: vdom };
656
697
  try {
657
698
  vdom.hooks?.$attach?.(target);
658
699
  if (vdom.type === SPECIAL_NODE) {