From 2df477e8a34855a35b0a2c943d3165167b15ce67 Mon Sep 17 00:00:00 2001 From: MichaelWest22 Date: Wed, 18 Sep 2024 09:21:46 +1200 Subject: [PATCH 1/2] Add test --- src/htmx.js | 19 +++++++++++++++---- test/attributes/hx-disabled-elt.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 41ae9b382..2ec4e1755 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -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 @@ -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') { 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() + }) }) From 36ce230772cdb56471817b1ef372f8dc73862146 Mon Sep 17 00:00:00 2001 From: MichaelWest22 Date: Wed, 18 Sep 2024 09:27:50 +1200 Subject: [PATCH 2/2] simplify --- src/htmx.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 2ec4e1755..6263a9a6c 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1138,10 +1138,7 @@ var htmx = (function() { * @returns {Node[]} */ function toNodeArray(node) { - if (node) { - return [node] - } - return [] + return node ? [node] : [] } /**