Răsfoiți Sursa

Fix Morph missing next sibling when replacing element (#2725)

* Remove only on previous test

* Add failing test for inline elements

* Fix debugging output

* Add fix for missing next sibling
Josh 3 ani în urmă
părinte
comite
fdfc4e1dc5
2 a modificat fișierele cu 25 adăugiri și 3 ștergeri
  1. 5 2
      packages/morph/src/morph.js
  2. 20 1
      tests/cypress/integration/plugins/morph.spec.js

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

@@ -207,7 +207,7 @@ async function patchChildren(from, to) {
             } else {
             } else {
                 let added = addNodeTo(currentTo, from) || {}
                 let added = addNodeTo(currentTo, from) || {}
 
 
-                await breakpoint('Add element: ' + added.outerHTML || added.nodeValue)
+                await breakpoint('Add element: ' + (added.outerHTML || added.nodeValue))
 
 
                 currentTo = dom(currentTo).nodes().next()
                 currentTo = dom(currentTo).nodes().next()
 
 
@@ -278,11 +278,14 @@ async function patchChildren(from, to) {
             }
             }
         }
         }
 
 
+        // Get next from sibling before patching in case the node is replaced
+        let currentFromNext = currentFrom && dom(currentFrom).nodes().next()
+
         // Patch elements
         // Patch elements
         await patch(currentFrom, currentTo)
         await patch(currentFrom, currentTo)
 
 
         currentTo = currentTo && dom(currentTo).nodes().next()
         currentTo = currentTo && dom(currentTo).nodes().next()
-        currentFrom = currentFrom && dom(currentFrom).nodes().next()
+        currentFrom = currentFromNext
     }
     }
 
 
     // Cleanup extra froms.
     // Cleanup extra froms.

+ 20 - 1
tests/cypress/integration/plugins/morph.spec.js

@@ -256,7 +256,7 @@ test('can morph text nodes',
     },
     },
 )
 )
 
 
-test.only('can morph with added element before and siblings are different',
+test('can morph with added element before and siblings are different',
     [html`
     [html`
         <button>
         <button>
             <div>
             <div>
@@ -286,3 +286,22 @@ test.only('can morph with added element before and siblings are different',
             `))
             `))
     },
     },
 )
 )
+
+test('can morph different inline nodes',
+    [html`
+    <div id="from">
+        Hello <span>World</span>
+    </div>
+    `],
+    ({ get }, reload, window, document) => {
+        let toHtml = html`
+        <div id="to">
+            Welcome <b>Person</b>!
+        </div>
+        `
+
+        get('div').then(([el]) => window.Alpine.morph(el, toHtml))
+
+        get('div').should(haveHtml('\n            Welcome <b>Person</b>!\n        '))
+    },
+)