@wsxjs/wsx-core 0.0.26 → 0.0.28

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.
@@ -86,6 +86,17 @@ function flattenChildren(children, skipHTMLDetection = false, depth = 0) {
86
86
  } else {
87
87
  result.push(child);
88
88
  }
89
+ } else if (child instanceof DocumentFragment) {
90
+ const fragmentChildren = Array.from(child.childNodes);
91
+ for (const fragChild of fragmentChildren) {
92
+ if (fragChild instanceof HTMLElement || fragChild instanceof SVGElement) {
93
+ result.push(fragChild);
94
+ } else if (fragChild.nodeType === Node.TEXT_NODE) {
95
+ result.push(fragChild.textContent || "");
96
+ } else if (fragChild instanceof DocumentFragment) {
97
+ result.push(...flattenChildren([fragChild], skipHTMLDetection, depth + 1));
98
+ }
99
+ }
89
100
  } else {
90
101
  result.push(child);
91
102
  }
@@ -186,7 +197,7 @@ function isCreatedByH(element) {
186
197
  }
187
198
  function shouldPreserveElement(element) {
188
199
  if (!(element instanceof HTMLElement || element instanceof SVGElement)) {
189
- return true;
200
+ return false;
190
201
  }
191
202
  if (!isCreatedByH(element)) {
192
203
  return true;
@@ -550,7 +561,7 @@ function setSmartProperty(element, key, value, tag) {
550
561
 
551
562
  // src/utils/element-creation.ts
552
563
  function applySingleProp(element, key, value, tag, isSVG) {
553
- if (value === null || value === void 0 || value === false) {
564
+ if (value === null || value === void 0) {
554
565
  return;
555
566
  }
556
567
  if (key === "ref" && typeof value === "function") {
@@ -579,6 +590,31 @@ function applySingleProp(element, key, value, tag, isSVG) {
579
590
  if (typeof value === "boolean") {
580
591
  if (value) {
581
592
  element.setAttribute(key, "");
593
+ if (element instanceof HTMLInputElement) {
594
+ if (key === "checked") {
595
+ element.checked = true;
596
+ } else if (key === "disabled") {
597
+ element.disabled = true;
598
+ } else if (key === "readonly") {
599
+ element.readOnly = true;
600
+ }
601
+ } else if (element instanceof HTMLOptionElement && key === "selected") {
602
+ element.selected = true;
603
+ }
604
+ } else {
605
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
606
+ element.removeAttribute(attributeName);
607
+ if (element instanceof HTMLInputElement) {
608
+ if (key === "checked") {
609
+ element.checked = false;
610
+ } else if (key === "disabled") {
611
+ element.disabled = false;
612
+ } else if (key === "readonly") {
613
+ element.readOnly = false;
614
+ }
615
+ } else if (element instanceof HTMLOptionElement && key === "selected") {
616
+ element.selected = false;
617
+ }
582
618
  }
583
619
  return;
584
620
  }
@@ -612,7 +648,9 @@ function appendChildrenToElement(element, children) {
612
648
  return;
613
649
  }
614
650
  if (typeof child === "string" || typeof child === "number") {
615
- element.appendChild(document.createTextNode(String(child)));
651
+ const textNode = document.createTextNode(String(child));
652
+ textNode.__wsxManaged = true;
653
+ element.appendChild(textNode);
616
654
  } else if (child instanceof HTMLElement || child instanceof SVGElement) {
617
655
  element.appendChild(child);
618
656
  } else if (child instanceof DocumentFragment) {
@@ -632,6 +670,9 @@ function collectPreservedElements(element) {
632
670
  const preserved = [];
633
671
  for (let i = 0; i < element.childNodes.length; i++) {
634
672
  const child = element.childNodes[i];
673
+ if (child.nodeType === Node.TEXT_NODE) {
674
+ continue;
675
+ }
635
676
  if (shouldPreserveElement(child)) {
636
677
  preserved.push(child);
637
678
  }
@@ -665,38 +706,40 @@ function findElementNode(oldChild, parent) {
665
706
  }
666
707
  return null;
667
708
  }
668
- function findTextNode(parent, domIndex) {
669
- while (domIndex.value < parent.childNodes.length) {
670
- const node = parent.childNodes[domIndex.value];
671
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === parent) {
672
- const textNode = node;
673
- domIndex.value++;
674
- return textNode;
709
+ function findTextNode(parent, domIndex, processedNodes) {
710
+ for (let i = domIndex.value; i < parent.childNodes.length; i++) {
711
+ const node = parent.childNodes[i];
712
+ if (node.nodeType === Node.TEXT_NODE && node.__wsxManaged === true && !processedNodes.has(node)) {
713
+ domIndex.value = i + 1;
714
+ return node;
675
715
  }
676
- domIndex.value++;
677
716
  }
678
717
  return null;
679
718
  }
680
- function updateOrCreateTextNode(parent, oldNode, newText) {
719
+ function updateOrCreateTextNode(parent, oldNode, newText, insertBeforeNode) {
720
+ if (shouldPreserveElement(parent)) {
721
+ if (oldNode && oldNode.nodeType === Node.TEXT_NODE) {
722
+ return oldNode;
723
+ }
724
+ return document.createTextNode(newText);
725
+ }
681
726
  if (oldNode && oldNode.nodeType === Node.TEXT_NODE) {
682
727
  if (oldNode.textContent !== newText) {
683
728
  oldNode.textContent = newText;
684
729
  }
685
- return oldNode;
686
- } else {
687
- if (!oldNode) {
688
- for (let i = 0; i < parent.childNodes.length; i++) {
689
- const node = parent.childNodes[i];
690
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === parent && node.textContent === newText) {
691
- return node;
692
- }
730
+ if (insertBeforeNode !== void 0) {
731
+ if (oldNode !== insertBeforeNode && oldNode.nextSibling !== insertBeforeNode) {
732
+ parent.insertBefore(oldNode, insertBeforeNode);
693
733
  }
694
734
  }
735
+ return oldNode;
736
+ } else {
695
737
  const newTextNode = document.createTextNode(newText);
738
+ newTextNode.__wsxManaged = true;
696
739
  if (oldNode && !shouldPreserveElement(oldNode)) {
697
740
  parent.replaceChild(newTextNode, oldNode);
698
741
  } else {
699
- parent.insertBefore(newTextNode, oldNode || null);
742
+ parent.insertBefore(newTextNode, insertBeforeNode ?? null);
700
743
  }
701
744
  return newTextNode;
702
745
  }
@@ -706,53 +749,13 @@ function removeNodeIfNotPreserved(parent, node) {
706
749
  parent.removeChild(node);
707
750
  }
708
751
  }
709
- function replaceOrInsertElement(parent, newChild, oldNode) {
710
- const targetNextSibling = oldNode && shouldPreserveElement(oldNode) ? oldNode : oldNode?.nextSibling || null;
711
- replaceOrInsertElementAtPosition(parent, newChild, oldNode, targetNextSibling);
712
- }
713
- function replaceOrInsertElementAtPosition(parent, newChild, oldNode, targetNextSibling) {
714
- if (newChild.parentNode && newChild.parentNode !== parent) {
715
- newChild.parentNode.removeChild(newChild);
716
- }
717
- const isInCorrectPosition = newChild.parentNode === parent && newChild.nextSibling === targetNextSibling;
718
- if (isInCorrectPosition) {
719
- return;
720
- }
721
- if (newChild.parentNode === parent) {
722
- parent.insertBefore(newChild, targetNextSibling);
723
- return;
724
- }
725
- if (oldNode && oldNode.parentNode === parent && !shouldPreserveElement(oldNode)) {
726
- if (oldNode !== newChild) {
727
- parent.replaceChild(newChild, oldNode);
728
- }
729
- } else {
730
- if (newChild.parentNode === parent) {
731
- return;
732
- }
733
- const newChildCacheKey = getElementCacheKey(newChild);
734
- if (!newChildCacheKey) {
735
- const newChildContent = newChild.textContent || "";
736
- const newChildTag = newChild.tagName.toLowerCase();
737
- for (let i = 0; i < parent.childNodes.length; i++) {
738
- const existingNode = parent.childNodes[i];
739
- if (existingNode instanceof HTMLElement || existingNode instanceof SVGElement) {
740
- const existingCacheKey = getElementCacheKey(existingNode);
741
- if (!existingCacheKey && existingNode.tagName.toLowerCase() === newChildTag && existingNode.textContent === newChildContent && existingNode !== newChild) {
742
- return;
743
- }
744
- }
745
- }
746
- }
747
- parent.insertBefore(newChild, targetNextSibling);
748
- }
749
- }
750
752
  function appendNewChild(parent, child, processedNodes) {
751
753
  if (child === null || child === void 0 || child === false) {
752
754
  return;
753
755
  }
754
756
  if (typeof child === "string" || typeof child === "number") {
755
757
  const newTextNode = document.createTextNode(String(child));
758
+ newTextNode.__wsxManaged = true;
756
759
  parent.appendChild(newTextNode);
757
760
  if (processedNodes) {
758
761
  processedNodes.add(newTextNode);
@@ -769,6 +772,11 @@ function appendNewChild(parent, child, processedNodes) {
769
772
  processedNodes.add(child);
770
773
  }
771
774
  } else if (child instanceof DocumentFragment) {
775
+ if (processedNodes) {
776
+ for (let i = 0; i < child.childNodes.length; i++) {
777
+ processedNodes.add(child.childNodes[i]);
778
+ }
779
+ }
772
780
  parent.appendChild(child);
773
781
  }
774
782
  }
@@ -789,21 +797,32 @@ function buildNewChildrenMaps(flatNew) {
789
797
  return { elementSet, cacheKeyMap };
790
798
  }
791
799
  function shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes) {
800
+ if (node.nodeType === Node.TEXT_NODE) {
801
+ if (node.__wsxManaged === true) {
802
+ if (processedNodes && processedNodes.has(node)) {
803
+ return false;
804
+ }
805
+ return true;
806
+ }
807
+ const parent = node.parentNode;
808
+ if (parent && (parent instanceof HTMLElement || parent instanceof SVGElement)) {
809
+ if (shouldPreserveElement(parent)) {
810
+ return false;
811
+ }
812
+ }
813
+ return true;
814
+ } else {
815
+ if (shouldPreserveElement(node)) {
816
+ return false;
817
+ }
818
+ }
819
+ const isProcessed = processedNodes && processedNodes.has(node);
792
820
  if (shouldPreserveElement(node)) {
793
821
  return false;
794
822
  }
795
- if (node.nodeType === Node.TEXT_NODE && processedNodes && processedNodes.has(node)) {
823
+ if (node.nodeType === Node.TEXT_NODE && isProcessed) {
796
824
  return false;
797
825
  }
798
- if (node.nodeType === Node.TEXT_NODE && processedNodes) {
799
- let parent = node.parentNode;
800
- while (parent) {
801
- if (processedNodes.has(parent) && parent.parentNode) {
802
- return false;
803
- }
804
- parent = parent.parentNode;
805
- }
806
- }
807
826
  if (node instanceof HTMLElement || node instanceof SVGElement || node instanceof DocumentFragment) {
808
827
  if (elementSet.has(node)) {
809
828
  return false;
@@ -845,7 +864,8 @@ function collectNodesToRemove(parent, elementSet, cacheKeyMap, processedNodes) {
845
864
  const nodesToRemove = [];
846
865
  for (let i = 0; i < parent.childNodes.length; i++) {
847
866
  const node = parent.childNodes[i];
848
- if (shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes)) {
867
+ const removed = shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes);
868
+ if (removed) {
849
869
  nodesToRemove.push(node);
850
870
  }
851
871
  }
@@ -934,7 +954,7 @@ function removeProp(element, key, oldValue, tag) {
934
954
  }
935
955
  }
936
956
  function applySingleProp2(element, key, value, tag, isSVG) {
937
- if (value === null || value === void 0 || value === false) {
957
+ if (value === null || value === void 0) {
938
958
  return;
939
959
  }
940
960
  if (key === "ref" && typeof value === "function") {
@@ -967,6 +987,31 @@ function applySingleProp2(element, key, value, tag, isSVG) {
967
987
  if (typeof value === "boolean") {
968
988
  if (value) {
969
989
  element.setAttribute(key, "");
990
+ if (element instanceof HTMLInputElement) {
991
+ if (key === "checked") {
992
+ element.checked = true;
993
+ } else if (key === "disabled") {
994
+ element.disabled = true;
995
+ } else if (key === "readonly") {
996
+ element.readOnly = true;
997
+ }
998
+ } else if (element instanceof HTMLOptionElement && key === "selected") {
999
+ element.selected = true;
1000
+ }
1001
+ } else {
1002
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
1003
+ element.removeAttribute(attributeName);
1004
+ if (element instanceof HTMLInputElement) {
1005
+ if (key === "checked") {
1006
+ element.checked = false;
1007
+ } else if (key === "disabled") {
1008
+ element.disabled = false;
1009
+ } else if (key === "readonly") {
1010
+ element.readOnly = false;
1011
+ }
1012
+ } else if (element instanceof HTMLOptionElement && key === "selected") {
1013
+ element.selected = false;
1014
+ }
970
1015
  }
971
1016
  return;
972
1017
  }
@@ -1022,6 +1067,7 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1022
1067
  const preservedElements = collectPreservedElements(element);
1023
1068
  const minLength = Math.min(flatOld.length, flatNew.length);
1024
1069
  const domIndex = { value: 0 };
1070
+ const insertionIndex = { value: 0 };
1025
1071
  const processedNodes = /* @__PURE__ */ new Set();
1026
1072
  for (let i = 0; i < minLength; i++) {
1027
1073
  const oldChild = flatOld[i];
@@ -1036,40 +1082,54 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1036
1082
  }
1037
1083
  }
1038
1084
  } else if (typeof oldChild === "string" || typeof oldChild === "number") {
1039
- oldNode = findTextNode(element, domIndex);
1040
- if (!oldNode && element.childNodes.length > 0) {
1041
- const oldText = String(oldChild);
1042
- for (let j = domIndex.value; j < element.childNodes.length; j++) {
1043
- const node = element.childNodes[j];
1044
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === element && node.textContent === oldText) {
1045
- oldNode = node;
1046
- domIndex.value = j + 1;
1047
- break;
1085
+ if (shouldPreserveElement(element)) {
1086
+ oldNode = null;
1087
+ } else {
1088
+ oldNode = findTextNode(element, domIndex, processedNodes);
1089
+ if (oldNode) {
1090
+ const nodeIndex = Array.from(element.childNodes).indexOf(oldNode);
1091
+ if (nodeIndex !== -1 && nodeIndex >= domIndex.value) {
1092
+ domIndex.value = nodeIndex + 1;
1048
1093
  }
1049
1094
  }
1050
1095
  }
1051
1096
  }
1052
1097
  if (typeof oldChild === "string" || typeof oldChild === "number") {
1053
1098
  if (typeof newChild === "string" || typeof newChild === "number") {
1054
- const oldText = String(oldChild);
1055
1099
  const newText = String(newChild);
1056
- const needsUpdate = oldText !== newText || oldNode && oldNode.nodeType === Node.TEXT_NODE && oldNode.textContent !== newText;
1057
- if (needsUpdate) {
1058
- const updatedNode = updateOrCreateTextNode(element, oldNode, newText);
1059
- if (updatedNode && !processedNodes.has(updatedNode)) {
1060
- processedNodes.add(updatedNode);
1061
- }
1062
- } else {
1063
- if (oldNode && oldNode.parentNode === element) {
1064
- processedNodes.add(oldNode);
1065
- }
1100
+ if (shouldPreserveElement(element)) {
1101
+ continue;
1102
+ }
1103
+ const insertBeforeNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1104
+ const updatedNode = updateOrCreateTextNode(
1105
+ element,
1106
+ oldNode,
1107
+ newText,
1108
+ insertBeforeNode
1109
+ );
1110
+ if (updatedNode) {
1111
+ processedNodes.add(updatedNode);
1112
+ insertionIndex.value++;
1066
1113
  }
1067
1114
  } else {
1068
- removeNodeIfNotPreserved(element, oldNode);
1115
+ const targetNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1069
1116
  if (newChild instanceof HTMLElement || newChild instanceof SVGElement) {
1070
- replaceOrInsertElement(element, newChild, oldNode);
1117
+ if (oldNode && oldNode === targetNode && oldNode.parentNode === element) {
1118
+ element.replaceChild(newChild, oldNode);
1119
+ } else {
1120
+ element.insertBefore(newChild, targetNode);
1121
+ removeNodeIfNotPreserved(element, oldNode);
1122
+ }
1123
+ processedNodes.add(newChild);
1124
+ insertionIndex.value++;
1071
1125
  } else if (newChild instanceof DocumentFragment) {
1072
- element.insertBefore(newChild, oldNode || null);
1126
+ if (processedNodes) {
1127
+ for (let i2 = 0; i2 < newChild.childNodes.length; i2++) {
1128
+ processedNodes.add(newChild.childNodes[i2]);
1129
+ }
1130
+ }
1131
+ element.insertBefore(newChild, targetNode);
1132
+ removeNodeIfNotPreserved(element, oldNode);
1073
1133
  }
1074
1134
  }
1075
1135
  } else if (oldChild instanceof HTMLElement || oldChild instanceof SVGElement) {
@@ -1077,49 +1137,37 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1077
1137
  continue;
1078
1138
  }
1079
1139
  if (newChild instanceof HTMLElement || newChild instanceof SVGElement) {
1080
- let targetNextSibling = null;
1081
- let foundPreviousElement = false;
1082
- for (let j = i - 1; j >= 0; j--) {
1083
- const prevChild = flatNew[j];
1084
- if (prevChild instanceof HTMLElement || prevChild instanceof SVGElement) {
1085
- if (prevChild.parentNode === element) {
1086
- targetNextSibling = prevChild.nextSibling;
1087
- foundPreviousElement = true;
1088
- break;
1089
- }
1140
+ const insertBeforeNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1141
+ if (newChild === oldNode) {
1142
+ if (newChild.nextSibling !== insertBeforeNode) {
1143
+ element.insertBefore(newChild, insertBeforeNode);
1090
1144
  }
1091
- }
1092
- if (!foundPreviousElement) {
1093
- const firstChild = Array.from(element.childNodes).find(
1094
- (node) => !shouldPreserveElement(node) && !processedNodes.has(node)
1095
- );
1096
- targetNextSibling = firstChild || null;
1097
- }
1098
- const isInCorrectPosition = newChild.parentNode === element && newChild.nextSibling === targetNextSibling;
1099
- if (newChild === oldChild && isInCorrectPosition) {
1100
- if (oldNode) processedNodes.add(oldNode);
1101
- processedNodes.add(newChild);
1102
- continue;
1103
- }
1104
- const referenceNode = oldNode && oldNode.parentNode === element ? oldNode : null;
1105
- replaceOrInsertElementAtPosition(
1106
- element,
1107
- newChild,
1108
- referenceNode,
1109
- targetNextSibling
1110
- );
1111
- if (oldNode && oldNode !== newChild) {
1112
- processedNodes.delete(oldNode);
1145
+ } else {
1146
+ element.insertBefore(newChild, insertBeforeNode);
1113
1147
  }
1114
1148
  processedNodes.add(newChild);
1149
+ insertionIndex.value++;
1115
1150
  } else {
1116
- removeNodeIfNotPreserved(element, oldNode);
1151
+ const targetNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1117
1152
  if (typeof newChild === "string" || typeof newChild === "number") {
1118
1153
  const newTextNode = document.createTextNode(String(newChild));
1119
- element.insertBefore(newTextNode, oldNode?.nextSibling || null);
1154
+ newTextNode.__wsxManaged = true;
1155
+ if (oldNode && oldNode === targetNode && oldNode.parentNode === element) {
1156
+ element.replaceChild(newTextNode, oldNode);
1157
+ } else {
1158
+ element.insertBefore(newTextNode, targetNode);
1159
+ removeNodeIfNotPreserved(element, oldNode);
1160
+ }
1120
1161
  processedNodes.add(newTextNode);
1162
+ insertionIndex.value++;
1121
1163
  } else if (newChild instanceof DocumentFragment) {
1122
- element.insertBefore(newChild, oldNode?.nextSibling || null);
1164
+ if (processedNodes) {
1165
+ for (let i2 = 0; i2 < newChild.childNodes.length; i2++) {
1166
+ processedNodes.add(newChild.childNodes[i2]);
1167
+ }
1168
+ }
1169
+ element.insertBefore(newChild, targetNode);
1170
+ removeNodeIfNotPreserved(element, oldNode);
1123
1171
  }
1124
1172
  }
1125
1173
  }
@@ -1241,7 +1289,9 @@ function Fragment(_props, children) {
1241
1289
  return;
1242
1290
  }
1243
1291
  if (typeof child === "string" || typeof child === "number") {
1244
- fragment.appendChild(document.createTextNode(String(child)));
1292
+ const textNode = document.createTextNode(String(child));
1293
+ textNode.__wsxManaged = true;
1294
+ fragment.appendChild(textNode);
1245
1295
  } else if (child instanceof HTMLElement || child instanceof SVGElement) {
1246
1296
  fragment.appendChild(child);
1247
1297
  } else if (child instanceof DocumentFragment) {
@@ -2,7 +2,7 @@ import {
2
2
  Fragment,
3
3
  jsx,
4
4
  jsxs
5
- } from "./chunk-5Q2VEEUH.mjs";
5
+ } from "./chunk-34PNC5CJ.mjs";
6
6
  export {
7
7
  Fragment,
8
8
  jsx,