Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Handle missing selectors for closest/find/next/previous #2915

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,14 @@ var htmx = (function() {
}
}

/**
* @param {Node|null} node
* @returns {Node[]}
*/
function toNodeArray(node) {
return node ? [node] : []
}

/**
* @param {Node|Element|Document|string} elt
* @param {string} selector
Expand All @@ -1142,17 +1150,17 @@ var htmx = (function() {
function querySelectorAllExt(elt, selector, global) {
elt = resolveTarget(elt)
if (selector.indexOf('closest ') === 0) {
return [closest(asElement(elt), normalizeSelector(selector.substr(8)))]
return toNodeArray(closest(asElement(elt), normalizeSelector(selector.substr(8))))
} else if (selector.indexOf('find ') === 0) {
return [find(asParentNode(elt), normalizeSelector(selector.substr(5)))]
return toNodeArray(find(asParentNode(elt), normalizeSelector(selector.substr(5))))
} else if (selector === 'next') {
return [asElement(elt).nextElementSibling]
} else if (selector.indexOf('next ') === 0) {
return [scanForwardQuery(elt, normalizeSelector(selector.substr(5)), !!global)]
return toNodeArray(scanForwardQuery(elt, normalizeSelector(selector.substr(5)), !!global))
} else if (selector === 'previous') {
return [asElement(elt).previousElementSibling]
} else if (selector.indexOf('previous ') === 0) {
return [scanBackwardsQuery(elt, normalizeSelector(selector.substr(9)), !!global)]
return toNodeArray(scanBackwardsQuery(elt, normalizeSelector(selector.substr(9)), !!global))
} else if (selector === 'document') {
return [document]
} else if (selector === 'window') {
Expand Down
20 changes: 20 additions & 0 deletions test/attributes/hx-disabled-elt.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,24 @@ describe('hx-disabled-elt attribute', function() {
b2.hasAttribute('disabled').should.equal(false)
b3.hasAttribute('disabled').should.equal(false)
})

it('closest/find/next/previous handle nothing to find without exception', function() {
this.server.respondWith('GET', '/test', 'Clicked!')
var btn1 = make('<button hx-get="/test" hx-disabled-elt="closest input">Click Me!</button>')
var btn2 = make('<button hx-get="/test" hx-disabled-elt="find input">Click Me!</button>')
var btn3 = make('<button hx-get="/test" hx-disabled-elt="next input">Click Me!</button>')
var btn4 = make('<button hx-get="/test" hx-disabled-elt="previous input">Click Me!</button>')
btn1.click()
btn1.hasAttribute('disabled').should.equal(false)
this.server.respond()
btn2.click()
btn2.hasAttribute('disabled').should.equal(false)
this.server.respond()
btn3.click()
btn3.hasAttribute('disabled').should.equal(false)
this.server.respond()
btn4.click()
btn4.hasAttribute('disabled').should.equal(false)
this.server.respond()
})
})