Skip to content

Commit cd045c3

Browse files
fix issue with hx-sync and htmx:abort with shadow DOM (#3424)
1 parent 69ecb9f commit cd045c3

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/htmx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5120,7 +5120,7 @@ var htmx = (function() {
51205120
"[hx-trigger='restored'],[data-hx-trigger='restored']"
51215121
)
51225122
body.addEventListener('htmx:abort', function(evt) {
5123-
const target = evt.target
5123+
const target = (/** @type {CustomEvent} */(evt)).detail.elt || evt.target
51245124
const internalData = getInternalData(target)
51255125
if (internalData && internalData.xhr) {
51265126
internalData.xhr.abort()

test/core/shadowdom.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,4 +1341,64 @@ describe('Core htmx Shadow DOM Tests', function() {
13411341
getWorkArea().innerHTML.should.equal('Clicked!')
13421342
chai.expect(getWorkArea().shadowRoot).to.be.a('null')
13431343
})
1344+
1345+
it('hx-sync works properly in shadow DOM', function() {
1346+
var count = 0
1347+
this.server.respondWith('GET', '/test', function(xhr) {
1348+
xhr.respond(200, {}, 'Click ' + count++)
1349+
})
1350+
make('<div hx-sync="this:drop"><button id="b1" hx-get="/test">Initial</button>' +
1351+
' <button id="b2" hx-get="/test">Initial</button></div>')
1352+
var b1 = byId('b1')
1353+
var b2 = byId('b2')
1354+
b1.click()
1355+
b2.click()
1356+
this.server.respond()
1357+
this.server.respond()
1358+
b1.innerHTML.should.equal('Click 0')
1359+
b2.innerHTML.should.equal('Initial')
1360+
})
1361+
1362+
it('can abort a request programmatically in shadow DOM', function() {
1363+
var count = 0
1364+
var abortedCount = 0
1365+
this.server.respondWith('GET', '/test', function(xhr) {
1366+
xhr.respond(200, {}, 'Click ' + count++)
1367+
})
1368+
make('<div><button id="b1" hx-get="/test">Initial</button>' +
1369+
' <button id="b2" hx-get="/test">Initial</button></div>')
1370+
var b1 = byId('b1')
1371+
var b2 = byId('b2')
1372+
1373+
// Listen for abort events to verify they're working
1374+
htmx.on(b1, 'htmx:abort', function() { abortedCount++ })
1375+
1376+
b1.click()
1377+
b2.click()
1378+
1379+
htmx.trigger(b1, 'htmx:abort')
1380+
1381+
this.server.respond()
1382+
this.server.respond()
1383+
b1.innerHTML.should.equal('Initial')
1384+
b2.innerHTML.should.equal('Click 0')
1385+
abortedCount.should.equal(1)
1386+
})
1387+
1388+
it('hx-sync abort strategy works in shadow DOM', function() {
1389+
var count = 0
1390+
this.server.respondWith('GET', '/test', function(xhr) {
1391+
xhr.respond(200, {}, 'Click ' + count++)
1392+
})
1393+
make('<div hx-sync="this"><button hx-sync="closest div:abort" id="b1" hx-get="/test">Initial</button>' +
1394+
' <button id="b2" hx-get="/test">Initial</button></div>')
1395+
var b1 = byId('b1')
1396+
var b2 = byId('b2')
1397+
b1.click()
1398+
b2.click()
1399+
this.server.respond()
1400+
this.server.respond()
1401+
b1.innerHTML.should.equal('Initial')
1402+
b2.innerHTML.should.equal('Click 0')
1403+
})
13441404
})

0 commit comments

Comments
 (0)