Skip to content

Commit c04c8ee

Browse files
committed
Add error logging for bad oob swap modifers
1 parent 6185a8a commit c04c8ee

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/htmx.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,9 @@ var htmx = (function() {
14671467
swapSpec = { swapStyle: oobValue === 'true' ? 'outerHTML' : oobValue }
14681468
}
14691469
}
1470+
if (WHITESPACE.test(swapSpec.swapStyle)) {
1471+
logError('invalid modifier in hx-swap-oob: ' + oobValue)
1472+
}
14701473

14711474
oobElement.removeAttribute('hx-swap-oob')
14721475
oobElement.removeAttribute('data-hx-swap-oob')

test/attributes/hx-swap-oob.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,35 @@ describe('hx-swap-oob attribute', function() {
485485
it('handles textContent swap style', function() {
486486
this.server.respondWith('GET', '/test', '<div id="d2" hx-swap-oob="textContent"><p>Swapped16</p><p>!</p></div>')
487487
var div = make('<div hx-get="/test">click me</div>')
488-
var div2 = make('<div id="d2"><div>')
488+
var div2 = make('<div id="d2"></div>')
489489
div.click()
490490
this.server.respond()
491491
div2.innerHTML.should.equal('Swapped16!')
492492
})
493+
494+
it('invalid swap modifers with ":" will prevent oob swap and log error', function() {
495+
this.server.respondWith('GET', '/test', '<div id="d2" hx-swap-oob="innerHTML swapp:1s">Swapped17</div>')
496+
var div = make('<div hx-get="/test">click me</div>')
497+
var div2 = make('<div id="d2"></div>')
498+
var errorSpy = sinon.spy(console, 'error')
499+
try {
500+
div.click()
501+
this.server.respond()
502+
} catch (e) {}
503+
errorSpy.called.should.equal(true)
504+
div2.innerHTML.should.equal('')
505+
errorSpy.restore()
506+
})
507+
508+
it('invalid swap modifers without ":" will fall back to innerHTML swap and log error', function() {
509+
this.server.respondWith('GET', '/test', '<div id="d2" hx-swap-oob="outerHTML swapp">Swapped18</div>')
510+
var div = make('<div hx-get="/test">click me</div>')
511+
var div2 = make('<div id="d2"></div>')
512+
var errorSpy = sinon.spy(console, 'error')
513+
div.click()
514+
this.server.respond()
515+
errorSpy.called.should.equal(true)
516+
div2.innerHTML.should.equal('Swapped18')
517+
errorSpy.restore()
518+
})
493519
})

www/content/attributes/hx-swap-oob.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The following modifers from [`hx-swap`](@/attributes/hx-swap.md) can now be incl
5050
* `strip:` - Override the stripping or not of the oob elements encapsulating tag pair
5151
* `target:` - Set a custom CSS selector to use as the target
5252

53-
Before modifers were supported the swap strategy value could not contain spaces and the target selector was placed after a colon like `innerHTML:#status`. Now when the `hx-swap-oob` value is parsed and checked to see if is a valid swap specification with modifers before using it. If this parsing fails it falls back to the old legacy way and treats the first `:` as a seperator between a swap style and CSS selector. Any invalid or incorrectly spelt modifers used will prevent it using a valid selector preventing the oob swap.
53+
Before modifers were supported the swap strategy value could not contain spaces and the target selector was placed after a colon like `innerHTML:#status`. Now the `hx-swap-oob` value is parsed and checked to see if is a valid swap specification with modifers before using it. If this parsing fails it falls back to the old legacy method and treats the first `:` as a seperator between a swap style and CSS selector. Any invalid or incorrectly spelt modifers will log errors and will either fail or fall back to an innerHTML oob swap.
5454

5555
### Using alternate swap strategies
5656

0 commit comments

Comments
 (0)