mirror of
https://github.com/bigskysoftware/htmx.git
synced 2026-03-23 11:25:33 +00:00
handle bad scroll selectors gracefully
This commit is contained in:
12
src/htmx.js
12
src/htmx.js
@@ -1128,15 +1128,17 @@ var htmx = (() => {
|
||||
__handleScroll(swapSpec, target) {
|
||||
if (swapSpec.scroll) {
|
||||
let scrollTarget = swapSpec.scrollTarget ? this.__findExt(swapSpec.scrollTarget) : target;
|
||||
if (swapSpec.scroll === 'top') {
|
||||
scrollTarget.scrollTop = 0;
|
||||
} else if (swapSpec.scroll === 'bottom'){
|
||||
scrollTarget.scrollTop = scrollTarget.scrollHeight;
|
||||
if (scrollTarget) {
|
||||
if (swapSpec.scroll === 'top') {
|
||||
scrollTarget.scrollTop = 0;
|
||||
} else if (swapSpec.scroll === 'bottom'){
|
||||
scrollTarget.scrollTop = scrollTarget.scrollHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (swapSpec.show) {
|
||||
let showTarget = swapSpec.showTarget ? this.__findExt(swapSpec.showTarget) : target;
|
||||
showTarget.scrollIntoView(swapSpec.show === 'top')
|
||||
showTarget?.scrollIntoView(swapSpec.show === 'top')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -258,6 +258,40 @@ describe('__issueRequest unit tests', function() {
|
||||
assert.isTrue(ctx.request.signal.aborted)
|
||||
})
|
||||
|
||||
it('does not crash when scroll target selector matches nothing', async function () {
|
||||
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML scroll:top scrollTarget:#nonexistent"></div>')
|
||||
let ctx = htmx.__createRequestContext(div, new Event('click'))
|
||||
|
||||
ctx.fetch = async () => ({
|
||||
status: 200,
|
||||
headers: new Headers(),
|
||||
text: async () => '<div>Response</div>'
|
||||
})
|
||||
|
||||
let errorFired = false
|
||||
div.addEventListener('htmx:error', () => errorFired = true)
|
||||
|
||||
await htmx.__issueRequest(ctx)
|
||||
assert.isFalse(errorFired)
|
||||
})
|
||||
|
||||
it('does not crash when show target selector matches nothing', async function () {
|
||||
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML show:top showTarget:#nonexistent"></div>')
|
||||
let ctx = htmx.__createRequestContext(div, new Event('click'))
|
||||
|
||||
ctx.fetch = async () => ({
|
||||
status: 200,
|
||||
headers: new Headers(),
|
||||
text: async () => '<div>Response</div>'
|
||||
})
|
||||
|
||||
let errorFired = false
|
||||
div.addEventListener('htmx:error', () => errorFired = true)
|
||||
|
||||
await htmx.__issueRequest(ctx)
|
||||
assert.isFalse(errorFired)
|
||||
})
|
||||
|
||||
it('throws clean error for unknown swap style with no extensions', async function () {
|
||||
let div = createProcessedHTML('<div hx-get="/test" hx-swap="foobar"></div>')
|
||||
let ctx = htmx.__createRequestContext(div, new Event('click'))
|
||||
|
||||
Reference in New Issue
Block a user