Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelWest22 committed Sep 17, 2024
1 parent 326ff3b commit 2df477e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,17 @@ var htmx = (function() {
}
}

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

/**
* @param {Node|Element|Document|string} elt
* @param {string} selector
Expand All @@ -1142,17 +1153,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()
})
})

0 comments on commit 2df477e

Please sign in to comment.