diff --git a/src/htmx.js b/src/htmx.js
index 41ae9b382..6263a9a6c 100644
--- a/src/htmx.js
+++ b/src/htmx.js
@@ -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
@@ -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') {
diff --git a/test/attributes/hx-disabled-elt.js b/test/attributes/hx-disabled-elt.js
index 4e147568c..0f8c1b4b9 100644
--- a/test/attributes/hx-disabled-elt.js
+++ b/test/attributes/hx-disabled-elt.js
@@ -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('')
+ var btn2 = make('')
+ var btn3 = make('')
+ var btn4 = make('')
+ 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()
+ })
})