@wsxjs/wsx-core 0.0.27 → 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.
package/dist/index.js CHANGED
@@ -97,6 +97,17 @@ function flattenChildren(children, skipHTMLDetection = false, depth = 0) {
97
97
  } else {
98
98
  result.push(child);
99
99
  }
100
+ } else if (child instanceof DocumentFragment) {
101
+ const fragmentChildren = Array.from(child.childNodes);
102
+ for (const fragChild of fragmentChildren) {
103
+ if (fragChild instanceof HTMLElement || fragChild instanceof SVGElement) {
104
+ result.push(fragChild);
105
+ } else if (fragChild.nodeType === Node.TEXT_NODE) {
106
+ result.push(fragChild.textContent || "");
107
+ } else if (fragChild instanceof DocumentFragment) {
108
+ result.push(...flattenChildren([fragChild], skipHTMLDetection, depth + 1));
109
+ }
110
+ }
100
111
  } else {
101
112
  result.push(child);
102
113
  }
@@ -197,7 +208,7 @@ function isCreatedByH(element) {
197
208
  }
198
209
  function shouldPreserveElement(element) {
199
210
  if (!(element instanceof HTMLElement || element instanceof SVGElement)) {
200
- return true;
211
+ return false;
201
212
  }
202
213
  if (!isCreatedByH(element)) {
203
214
  return true;
@@ -648,7 +659,9 @@ function appendChildrenToElement(element, children) {
648
659
  return;
649
660
  }
650
661
  if (typeof child === "string" || typeof child === "number") {
651
- element.appendChild(document.createTextNode(String(child)));
662
+ const textNode = document.createTextNode(String(child));
663
+ textNode.__wsxManaged = true;
664
+ element.appendChild(textNode);
652
665
  } else if (child instanceof HTMLElement || child instanceof SVGElement) {
653
666
  element.appendChild(child);
654
667
  } else if (child instanceof DocumentFragment) {
@@ -668,6 +681,9 @@ function collectPreservedElements(element) {
668
681
  const preserved = [];
669
682
  for (let i = 0; i < element.childNodes.length; i++) {
670
683
  const child = element.childNodes[i];
684
+ if (child.nodeType === Node.TEXT_NODE) {
685
+ continue;
686
+ }
671
687
  if (shouldPreserveElement(child)) {
672
688
  preserved.push(child);
673
689
  }
@@ -701,38 +717,40 @@ function findElementNode(oldChild, parent) {
701
717
  }
702
718
  return null;
703
719
  }
704
- function findTextNode(parent, domIndex) {
705
- while (domIndex.value < parent.childNodes.length) {
706
- const node = parent.childNodes[domIndex.value];
707
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === parent) {
708
- const textNode = node;
709
- domIndex.value++;
710
- return textNode;
720
+ function findTextNode(parent, domIndex, processedNodes) {
721
+ for (let i = domIndex.value; i < parent.childNodes.length; i++) {
722
+ const node = parent.childNodes[i];
723
+ if (node.nodeType === Node.TEXT_NODE && node.__wsxManaged === true && !processedNodes.has(node)) {
724
+ domIndex.value = i + 1;
725
+ return node;
711
726
  }
712
- domIndex.value++;
713
727
  }
714
728
  return null;
715
729
  }
716
- function updateOrCreateTextNode(parent, oldNode, newText) {
730
+ function updateOrCreateTextNode(parent, oldNode, newText, insertBeforeNode) {
731
+ if (shouldPreserveElement(parent)) {
732
+ if (oldNode && oldNode.nodeType === Node.TEXT_NODE) {
733
+ return oldNode;
734
+ }
735
+ return document.createTextNode(newText);
736
+ }
717
737
  if (oldNode && oldNode.nodeType === Node.TEXT_NODE) {
718
738
  if (oldNode.textContent !== newText) {
719
739
  oldNode.textContent = newText;
720
740
  }
721
- return oldNode;
722
- } else {
723
- if (!oldNode) {
724
- for (let i = 0; i < parent.childNodes.length; i++) {
725
- const node = parent.childNodes[i];
726
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === parent && node.textContent === newText) {
727
- return node;
728
- }
741
+ if (insertBeforeNode !== void 0) {
742
+ if (oldNode !== insertBeforeNode && oldNode.nextSibling !== insertBeforeNode) {
743
+ parent.insertBefore(oldNode, insertBeforeNode);
729
744
  }
730
745
  }
746
+ return oldNode;
747
+ } else {
731
748
  const newTextNode = document.createTextNode(newText);
749
+ newTextNode.__wsxManaged = true;
732
750
  if (oldNode && !shouldPreserveElement(oldNode)) {
733
751
  parent.replaceChild(newTextNode, oldNode);
734
752
  } else {
735
- parent.insertBefore(newTextNode, oldNode || null);
753
+ parent.insertBefore(newTextNode, insertBeforeNode ?? null);
736
754
  }
737
755
  return newTextNode;
738
756
  }
@@ -742,53 +760,13 @@ function removeNodeIfNotPreserved(parent, node) {
742
760
  parent.removeChild(node);
743
761
  }
744
762
  }
745
- function replaceOrInsertElement(parent, newChild, oldNode) {
746
- const targetNextSibling = oldNode && shouldPreserveElement(oldNode) ? oldNode : oldNode?.nextSibling || null;
747
- replaceOrInsertElementAtPosition(parent, newChild, oldNode, targetNextSibling);
748
- }
749
- function replaceOrInsertElementAtPosition(parent, newChild, oldNode, targetNextSibling) {
750
- if (newChild.parentNode && newChild.parentNode !== parent) {
751
- newChild.parentNode.removeChild(newChild);
752
- }
753
- const isInCorrectPosition = newChild.parentNode === parent && newChild.nextSibling === targetNextSibling;
754
- if (isInCorrectPosition) {
755
- return;
756
- }
757
- if (newChild.parentNode === parent) {
758
- parent.insertBefore(newChild, targetNextSibling);
759
- return;
760
- }
761
- if (oldNode && oldNode.parentNode === parent && !shouldPreserveElement(oldNode)) {
762
- if (oldNode !== newChild) {
763
- parent.replaceChild(newChild, oldNode);
764
- }
765
- } else {
766
- if (newChild.parentNode === parent) {
767
- return;
768
- }
769
- const newChildCacheKey = getElementCacheKey(newChild);
770
- if (!newChildCacheKey) {
771
- const newChildContent = newChild.textContent || "";
772
- const newChildTag = newChild.tagName.toLowerCase();
773
- for (let i = 0; i < parent.childNodes.length; i++) {
774
- const existingNode = parent.childNodes[i];
775
- if (existingNode instanceof HTMLElement || existingNode instanceof SVGElement) {
776
- const existingCacheKey = getElementCacheKey(existingNode);
777
- if (!existingCacheKey && existingNode.tagName.toLowerCase() === newChildTag && existingNode.textContent === newChildContent && existingNode !== newChild) {
778
- return;
779
- }
780
- }
781
- }
782
- }
783
- parent.insertBefore(newChild, targetNextSibling);
784
- }
785
- }
786
763
  function appendNewChild(parent, child, processedNodes) {
787
764
  if (child === null || child === void 0 || child === false) {
788
765
  return;
789
766
  }
790
767
  if (typeof child === "string" || typeof child === "number") {
791
768
  const newTextNode = document.createTextNode(String(child));
769
+ newTextNode.__wsxManaged = true;
792
770
  parent.appendChild(newTextNode);
793
771
  if (processedNodes) {
794
772
  processedNodes.add(newTextNode);
@@ -805,6 +783,11 @@ function appendNewChild(parent, child, processedNodes) {
805
783
  processedNodes.add(child);
806
784
  }
807
785
  } else if (child instanceof DocumentFragment) {
786
+ if (processedNodes) {
787
+ for (let i = 0; i < child.childNodes.length; i++) {
788
+ processedNodes.add(child.childNodes[i]);
789
+ }
790
+ }
808
791
  parent.appendChild(child);
809
792
  }
810
793
  }
@@ -825,21 +808,32 @@ function buildNewChildrenMaps(flatNew) {
825
808
  return { elementSet, cacheKeyMap };
826
809
  }
827
810
  function shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes) {
811
+ if (node.nodeType === Node.TEXT_NODE) {
812
+ if (node.__wsxManaged === true) {
813
+ if (processedNodes && processedNodes.has(node)) {
814
+ return false;
815
+ }
816
+ return true;
817
+ }
818
+ const parent = node.parentNode;
819
+ if (parent && (parent instanceof HTMLElement || parent instanceof SVGElement)) {
820
+ if (shouldPreserveElement(parent)) {
821
+ return false;
822
+ }
823
+ }
824
+ return true;
825
+ } else {
826
+ if (shouldPreserveElement(node)) {
827
+ return false;
828
+ }
829
+ }
830
+ const isProcessed = processedNodes && processedNodes.has(node);
828
831
  if (shouldPreserveElement(node)) {
829
832
  return false;
830
833
  }
831
- if (node.nodeType === Node.TEXT_NODE && processedNodes && processedNodes.has(node)) {
834
+ if (node.nodeType === Node.TEXT_NODE && isProcessed) {
832
835
  return false;
833
836
  }
834
- if (node.nodeType === Node.TEXT_NODE && processedNodes) {
835
- let parent = node.parentNode;
836
- while (parent) {
837
- if (processedNodes.has(parent) && parent.parentNode) {
838
- return false;
839
- }
840
- parent = parent.parentNode;
841
- }
842
- }
843
837
  if (node instanceof HTMLElement || node instanceof SVGElement || node instanceof DocumentFragment) {
844
838
  if (elementSet.has(node)) {
845
839
  return false;
@@ -881,7 +875,8 @@ function collectNodesToRemove(parent, elementSet, cacheKeyMap, processedNodes) {
881
875
  const nodesToRemove = [];
882
876
  for (let i = 0; i < parent.childNodes.length; i++) {
883
877
  const node = parent.childNodes[i];
884
- if (shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes)) {
878
+ const removed = shouldRemoveNode(node, elementSet, cacheKeyMap, processedNodes);
879
+ if (removed) {
885
880
  nodesToRemove.push(node);
886
881
  }
887
882
  }
@@ -1083,6 +1078,7 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1083
1078
  const preservedElements = collectPreservedElements(element);
1084
1079
  const minLength = Math.min(flatOld.length, flatNew.length);
1085
1080
  const domIndex = { value: 0 };
1081
+ const insertionIndex = { value: 0 };
1086
1082
  const processedNodes = /* @__PURE__ */ new Set();
1087
1083
  for (let i = 0; i < minLength; i++) {
1088
1084
  const oldChild = flatOld[i];
@@ -1097,48 +1093,54 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1097
1093
  }
1098
1094
  }
1099
1095
  } else if (typeof oldChild === "string" || typeof oldChild === "number") {
1100
- oldNode = findTextNode(element, domIndex);
1101
- if (!oldNode && element.childNodes.length > 0) {
1102
- const oldText = String(oldChild);
1103
- for (let j = domIndex.value; j < element.childNodes.length; j++) {
1104
- const node = element.childNodes[j];
1105
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === element && node.textContent === oldText) {
1106
- oldNode = node;
1107
- domIndex.value = j + 1;
1108
- break;
1096
+ if (shouldPreserveElement(element)) {
1097
+ oldNode = null;
1098
+ } else {
1099
+ oldNode = findTextNode(element, domIndex, processedNodes);
1100
+ if (oldNode) {
1101
+ const nodeIndex = Array.from(element.childNodes).indexOf(oldNode);
1102
+ if (nodeIndex !== -1 && nodeIndex >= domIndex.value) {
1103
+ domIndex.value = nodeIndex + 1;
1109
1104
  }
1110
1105
  }
1111
1106
  }
1112
1107
  }
1113
1108
  if (typeof oldChild === "string" || typeof oldChild === "number") {
1114
1109
  if (typeof newChild === "string" || typeof newChild === "number") {
1115
- const oldText = String(oldChild);
1116
1110
  const newText = String(newChild);
1117
- const needsUpdate = oldText !== newText || oldNode && oldNode.nodeType === Node.TEXT_NODE && oldNode.textContent !== newText;
1118
- if (needsUpdate) {
1119
- const updatedNode = updateOrCreateTextNode(element, oldNode, newText);
1120
- if (updatedNode && !processedNodes.has(updatedNode)) {
1121
- processedNodes.add(updatedNode);
1122
- }
1123
- } else {
1124
- if (oldNode && oldNode.parentNode === element) {
1125
- processedNodes.add(oldNode);
1126
- } else if (!oldNode && oldText === newText) {
1127
- for (let j = domIndex.value; j < element.childNodes.length; j++) {
1128
- const node = element.childNodes[j];
1129
- if (node.nodeType === Node.TEXT_NODE && node.parentNode === element && node.textContent === newText && !processedNodes.has(node)) {
1130
- processedNodes.add(node);
1131
- break;
1132
- }
1133
- }
1134
- }
1111
+ if (shouldPreserveElement(element)) {
1112
+ continue;
1113
+ }
1114
+ const insertBeforeNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1115
+ const updatedNode = updateOrCreateTextNode(
1116
+ element,
1117
+ oldNode,
1118
+ newText,
1119
+ insertBeforeNode
1120
+ );
1121
+ if (updatedNode) {
1122
+ processedNodes.add(updatedNode);
1123
+ insertionIndex.value++;
1135
1124
  }
1136
1125
  } else {
1137
- removeNodeIfNotPreserved(element, oldNode);
1126
+ const targetNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1138
1127
  if (newChild instanceof HTMLElement || newChild instanceof SVGElement) {
1139
- replaceOrInsertElement(element, newChild, oldNode);
1128
+ if (oldNode && oldNode === targetNode && oldNode.parentNode === element) {
1129
+ element.replaceChild(newChild, oldNode);
1130
+ } else {
1131
+ element.insertBefore(newChild, targetNode);
1132
+ removeNodeIfNotPreserved(element, oldNode);
1133
+ }
1134
+ processedNodes.add(newChild);
1135
+ insertionIndex.value++;
1140
1136
  } else if (newChild instanceof DocumentFragment) {
1141
- element.insertBefore(newChild, oldNode || null);
1137
+ if (processedNodes) {
1138
+ for (let i2 = 0; i2 < newChild.childNodes.length; i2++) {
1139
+ processedNodes.add(newChild.childNodes[i2]);
1140
+ }
1141
+ }
1142
+ element.insertBefore(newChild, targetNode);
1143
+ removeNodeIfNotPreserved(element, oldNode);
1142
1144
  }
1143
1145
  }
1144
1146
  } else if (oldChild instanceof HTMLElement || oldChild instanceof SVGElement) {
@@ -1146,49 +1148,37 @@ function updateChildren(element, oldChildren, newChildren, _cacheManager) {
1146
1148
  continue;
1147
1149
  }
1148
1150
  if (newChild instanceof HTMLElement || newChild instanceof SVGElement) {
1149
- let targetNextSibling = null;
1150
- let foundPreviousElement = false;
1151
- for (let j = i - 1; j >= 0; j--) {
1152
- const prevChild = flatNew[j];
1153
- if (prevChild instanceof HTMLElement || prevChild instanceof SVGElement) {
1154
- if (prevChild.parentNode === element) {
1155
- targetNextSibling = prevChild.nextSibling;
1156
- foundPreviousElement = true;
1157
- break;
1158
- }
1151
+ const insertBeforeNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1152
+ if (newChild === oldNode) {
1153
+ if (newChild.nextSibling !== insertBeforeNode) {
1154
+ element.insertBefore(newChild, insertBeforeNode);
1159
1155
  }
1160
- }
1161
- if (!foundPreviousElement) {
1162
- const firstChild = Array.from(element.childNodes).find(
1163
- (node) => !shouldPreserveElement(node) && !processedNodes.has(node)
1164
- );
1165
- targetNextSibling = firstChild || null;
1166
- }
1167
- const isInCorrectPosition = newChild.parentNode === element && newChild.nextSibling === targetNextSibling;
1168
- if (newChild === oldChild && isInCorrectPosition) {
1169
- if (oldNode) processedNodes.add(oldNode);
1170
- processedNodes.add(newChild);
1171
- continue;
1172
- }
1173
- const referenceNode = oldNode && oldNode.parentNode === element ? oldNode : null;
1174
- replaceOrInsertElementAtPosition(
1175
- element,
1176
- newChild,
1177
- referenceNode,
1178
- targetNextSibling
1179
- );
1180
- if (oldNode && oldNode !== newChild) {
1181
- processedNodes.delete(oldNode);
1156
+ } else {
1157
+ element.insertBefore(newChild, insertBeforeNode);
1182
1158
  }
1183
1159
  processedNodes.add(newChild);
1160
+ insertionIndex.value++;
1184
1161
  } else {
1185
- removeNodeIfNotPreserved(element, oldNode);
1162
+ const targetNode = insertionIndex.value < element.childNodes.length ? element.childNodes[insertionIndex.value] : null;
1186
1163
  if (typeof newChild === "string" || typeof newChild === "number") {
1187
1164
  const newTextNode = document.createTextNode(String(newChild));
1188
- element.insertBefore(newTextNode, oldNode?.nextSibling || null);
1165
+ newTextNode.__wsxManaged = true;
1166
+ if (oldNode && oldNode === targetNode && oldNode.parentNode === element) {
1167
+ element.replaceChild(newTextNode, oldNode);
1168
+ } else {
1169
+ element.insertBefore(newTextNode, targetNode);
1170
+ removeNodeIfNotPreserved(element, oldNode);
1171
+ }
1189
1172
  processedNodes.add(newTextNode);
1173
+ insertionIndex.value++;
1190
1174
  } else if (newChild instanceof DocumentFragment) {
1191
- element.insertBefore(newChild, oldNode?.nextSibling || null);
1175
+ if (processedNodes) {
1176
+ for (let i2 = 0; i2 < newChild.childNodes.length; i2++) {
1177
+ processedNodes.add(newChild.childNodes[i2]);
1178
+ }
1179
+ }
1180
+ element.insertBefore(newChild, targetNode);
1181
+ removeNodeIfNotPreserved(element, oldNode);
1192
1182
  }
1193
1183
  }
1194
1184
  }
@@ -1310,7 +1300,9 @@ function Fragment(_props, children) {
1310
1300
  return;
1311
1301
  }
1312
1302
  if (typeof child === "string" || typeof child === "number") {
1313
- fragment.appendChild(document.createTextNode(String(child)));
1303
+ const textNode = document.createTextNode(String(child));
1304
+ textNode.__wsxManaged = true;
1305
+ fragment.appendChild(textNode);
1314
1306
  } else if (child instanceof HTMLElement || child instanceof SVGElement) {
1315
1307
  fragment.appendChild(child);
1316
1308
  } else if (child instanceof DocumentFragment) {
@@ -2211,7 +2203,7 @@ var WebComponent = class extends BaseComponent {
2211
2203
  if (!isContentAlreadyInShadowRoot) {
2212
2204
  this.shadowRoot.appendChild(content);
2213
2205
  }
2214
- const oldChildren = Array.from(this.shadowRoot.children).filter((child) => {
2206
+ const oldNodes = Array.from(this.shadowRoot.childNodes).filter((child) => {
2215
2207
  if (child === content) {
2216
2208
  return false;
2217
2209
  }
@@ -2223,7 +2215,7 @@ var WebComponent = class extends BaseComponent {
2223
2215
  }
2224
2216
  return true;
2225
2217
  });
2226
- oldChildren.forEach((child) => child.remove());
2218
+ oldNodes.forEach((node) => node.remove());
2227
2219
  const hasStylesAfterDOM = this.shadowRoot.adoptedStyleSheets && this.shadowRoot.adoptedStyleSheets.length > 0;
2228
2220
  const hasStyleElementAfterDOM = Array.from(this.shadowRoot.children).some(
2229
2221
  (child) => child instanceof HTMLStyleElement
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  createLogger,
5
5
  h,
6
6
  shouldPreserveElement
7
- } from "./chunk-DYG2LIOG.mjs";
7
+ } from "./chunk-34PNC5CJ.mjs";
8
8
 
9
9
  // src/styles/style-manager.ts
10
10
  var StyleManager = class {
@@ -897,7 +897,7 @@ var WebComponent = class extends BaseComponent {
897
897
  if (!isContentAlreadyInShadowRoot) {
898
898
  this.shadowRoot.appendChild(content);
899
899
  }
900
- const oldChildren = Array.from(this.shadowRoot.children).filter((child) => {
900
+ const oldNodes = Array.from(this.shadowRoot.childNodes).filter((child) => {
901
901
  if (child === content) {
902
902
  return false;
903
903
  }
@@ -909,7 +909,7 @@ var WebComponent = class extends BaseComponent {
909
909
  }
910
910
  return true;
911
911
  });
912
- oldChildren.forEach((child) => child.remove());
912
+ oldNodes.forEach((node) => node.remove());
913
913
  const hasStylesAfterDOM = this.shadowRoot.adoptedStyleSheets && this.shadowRoot.adoptedStyleSheets.length > 0;
914
914
  const hasStyleElementAfterDOM = Array.from(this.shadowRoot.children).some(
915
915
  (child) => child instanceof HTMLStyleElement