Skip to content

Commit

Permalink
fix #2932: check parent is null for swap
Browse files Browse the repository at this point in the history
  • Loading branch information
jackielii committed Sep 26, 2024
1 parent d528c9d commit 857f99a
Show file tree
Hide file tree
Showing 2 changed files with 32 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 @@ -1661,9 +1661,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 @@ -1725,7 +1729,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
22 changes: 22 additions & 0 deletions test/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,26 @@ 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()
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()
parent.remove()
this.server.respond()
parent.children.length.should.equal(0)
})
})

0 comments on commit 857f99a

Please sign in to comment.