Browse Source

Only use lookahead when elements aren't the same

Caleb Porzio 1 year ago
parent
commit
1396c74ddd
2 changed files with 35 additions and 11 deletions
  1. 33 9
      morph.html
  2. 2 2
      packages/morph/src/morph.js

+ 33 - 9
morph.html

@@ -5,19 +5,43 @@
 
 <div id="before">
 <!-- Before markup goes here: -->
-<ul>
-    <li key="1">foo<input></li>
-    <li key="2">bar<input></li>
-    <li key="3">baz<input></li>
-</ul>
+<div>
+    <div>
+    </div>
+
+    <button type="button" wire:click="$refresh" dusk="refresh">
+        Refresh
+    </button>
+
+    <div dusk="child" key="foo">
+        <input type="text">
+        Child
+    </div>
+
+    <div>
+    </div>
+</div>
 </div>
 
 <div id="after" style="display: none;">
 <!-- After markup goes here: -->
-<ul>
-    <li key="1">foo<input></li>
-    <li key="3">baz<input></li>
-</ul>
+<div>
+    <div>
+    </div>
+
+    <button type="button" wire:click="$refresh" dusk="refresh">
+        Refresh
+    </button>
+
+    <div dusk="child" key="foo">
+        <input type="text">
+        Child
+    </div>
+
+    <div>
+    </div>
+</div>
+
 </div>
 
     <div style="display: flex;">

+ 2 - 2
packages/morph/src/morph.js

@@ -218,13 +218,13 @@ export function morph(from, toHtml, options) {
             }
 
             // Lookaheads should only apply to non-text-or-comment elements...
-            if (currentFrom.nodeType === 1 && lookahead) {
+            if (currentFrom.nodeType === 1 && lookahead && ! currentFrom.isEqualNode(currentTo)) {
                 let nextToElementSibling = dom.next(toChildren, currentTo)
 
                 let found = false
 
                 while (! found && nextToElementSibling) {
-                    if (currentFrom.isEqualNode(nextToElementSibling)) {
+                    if (nextToElementSibling.nodeType === 1 && currentFrom.isEqualNode(nextToElementSibling)) {
                         found = true; // This ";" needs to be here...
 
                         [fromChildren, currentFrom] = addNodeBefore(fromChildren, currentTo, currentFrom)