Skip to content

Commit

Permalink
fix #2932: check parent is null for swap delete (#2933)
Browse files Browse the repository at this point in the history
* fix #2932: check parent is null for swap

* fix test in swap when parent elt deleted
  • Loading branch information
jackielii authored Oct 3, 2024
1 parent 4a81723 commit 4916ce4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1663,9 +1663,13 @@ var htmx = (function() {
/** @type {Node} */
let newElt
const eltBeforeNewContent = target.previousSibling
insertNodesBefore(parentElt(target), target, fragment, settleInfo)
const parentNode = parentElt(target)
if (!parentNode) { // when parent node disappears, we can't do anything
return
}
insertNodesBefore(parentNode, target, fragment, settleInfo)
if (eltBeforeNewContent == null) {
newElt = parentElt(target).firstChild
newElt = parentNode.firstChild
} else {
newElt = eltBeforeNewContent.nextSibling
}
Expand Down Expand Up @@ -1727,7 +1731,10 @@ var htmx = (function() {
*/
function swapDelete(target) {
cleanUpElement(target)
return parentElt(target).removeChild(target)
const parent = parentElt(target)
if (parent) {
return parent.removeChild(target)
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,28 @@ describe('Core htmx API test', function() {
output.innerHTML.should.be.equal('<div>Swapped!</div>')
oobDiv.innerHTML.should.be.equal('OOB Swapped!')
})

it('swap delete works when parent is removed', function() {
this.server.respondWith('DELETE', '/test', 'delete')

var parent = make('<div><div id="d1" hx-swap="delete" hx-delete="/test">click me</div></div>')
var div = htmx.find(parent, '#d1')
div.click()
div.remove()
parent.remove()
this.server.respond()
parent.children.length.should.equal(0)
})

it('swap outerHTML works when parent is removed', function() {
this.server.respondWith('GET', '/test', 'delete')

var parent = make('<div><div id="d1" hx-swap="outerHTML" hx-get="/test">click me</div></div>')
var div = htmx.find(parent, '#d1')
div.click()
div.remove()
parent.remove()
this.server.respond()
parent.children.length.should.equal(0)
})
})

0 comments on commit 4916ce4

Please sign in to comment.