Skip to content

Commit 952b7fa

Browse files
committed
wip
1 parent c01a70a commit 952b7fa

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

packages/morph/src/morph.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,19 @@ export function morphBetween(startMarker, endMarker, toHtml, options = {}) {
312312
let fromBlock = new Block(startMarker, endMarker)
313313

314314
// Create a temporary container for the new content
315-
let tempContainer = document.createElement('div')
315+
let tempContainer = typeof toHtml === 'string'
316+
? (() => {
317+
let container = document.createElement('div')
318+
container.insertAdjacentHTML('beforeend', toHtml)
319+
return container
320+
})()
321+
: toHtml
316322

317323
// Add markers around the new content to create a Block
318324
let toStartMarker = document.createComment('[morph-start]')
319325
let toEndMarker = document.createComment('[morph-end]')
320326

321-
tempContainer.appendChild(toStartMarker)
322-
tempContainer.insertAdjacentHTML('beforeend', toHtml)
327+
tempContainer.insertBefore(toStartMarker, tempContainer.firstChild)
323328
tempContainer.appendChild(toEndMarker)
324329

325330
let toBlock = new Block(toStartMarker, toEndMarker)

tests/cypress/integration/plugins/morph.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ test('morphBetween preserves Alpine state',
727727
`)
728728

729729
get('p').should(haveText('2'))
730-
get('div').should(haveText('New element'))
730+
get('div:not([x-data]) div').should(haveText('New element'))
731731
get('input').should(haveValue('2'))
732732
get('input').clear().type('5')
733733
get('p').should(haveText('5'))
@@ -772,8 +772,10 @@ test('morphBetween with keyed elements',
772772
get('li:nth-of-type(1)').should(haveText('baz'))
773773
get('li:nth-of-type(2)').should(haveText('foo'))
774774
get('li:nth-of-type(3)').should(haveText('bar'))
775-
get('li:nth-of-type(2) input').should(haveValue('first'))
776-
get('li:nth-of-type(3) input').should(haveValue('second'))
775+
// Need to verify by the key attribute since the elements have been reordered
776+
get('li[key="1"] input').should(haveValue('first'))
777+
get('li[key="2"] input').should(haveValue('second'))
778+
get('li[key="3"] input').should(haveValue(''))
777779
},
778780
)
779781

@@ -863,7 +865,7 @@ test('morphBetween with hooks',
863865
})
864866

865867
get('p').should(haveText('New paragraph'))
866-
get('div').should(haveText('New div'))
868+
get('body > div > div').should(haveText('New div'))
867869

868870
// Check hooks were called
869871
cy.wrap(removedElements).should('deep.equal', ['SPAN'])

0 commit comments

Comments
 (0)