scan through all siblings (not just until the first non-element) when doing an outerHTML swap to add things to settle

fixes https://github.com/bigskysoftware/htmx/issues/2787
This commit is contained in:
Carson Gross 2024-08-05 13:12:13 -06:00
parent df16ed8e96
commit 0ace4a731c
2 changed files with 21 additions and 3 deletions

View File

@ -1639,13 +1639,13 @@ var htmx = (function() {
newElt = eltBeforeNewContent.nextSibling
}
settleInfo.elts = settleInfo.elts.filter(function(e) { return e !== target })
// scan through all newly added content and add all elements to the settle info so we trigger
// events properly on them
while (newElt && newElt !== target) {
if (newElt instanceof Element) {
settleInfo.elts.push(newElt)
newElt = newElt.nextElementSibling
} else {
newElt = null
}
newElt = newElt.nextSibling
}
cleanUpElement(target)
if (target instanceof Element) {

View File

@ -163,6 +163,24 @@ describe('Core htmx Events', function() {
}
})
it('htmx:afterSwap is called when replacing outerHTML, new line content', function() {
var called = false
var handler = htmx.on('htmx:afterSwap', function(evt) {
called = true
})
try {
this.server.respondWith('POST', '/test', function(xhr) {
xhr.respond(200, {}, '\n<button>Bar</button>')
})
var div = make("<button hx-post='/test' hx-swap='outerHTML'>Foo</button>")
div.click()
this.server.respond()
should.equal(called, true)
} finally {
htmx.off('htmx:afterSwap', handler)
}
})
it('htmx:oobBeforeSwap is called before swap', function() {
var called = false
var handler = htmx.on('htmx:oobBeforeSwap', function(evt) {