diff options
Diffstat (limited to 'www/static')
-rw-r--r-- | www/static/src/htmx.js | 6 | ||||
-rw-r--r-- | www/static/test/core/internals.js | 5 | ||||
-rw-r--r-- | www/static/test/core/regressions.js | 42 |
3 files changed, 51 insertions, 2 deletions
diff --git a/www/static/src/htmx.js b/www/static/src/htmx.js index 69348460..4c8203ef 100644 --- a/www/static/src/htmx.js +++ b/www/static/src/htmx.js @@ -289,7 +289,7 @@ var htmx = (function() { location, /** @type {typeof internalEval} */ _: null, - version: '2.0.5' + version: '2.0.6' } // Tsc madness part 2 htmx.onLoad = onLoadHelper @@ -2435,7 +2435,9 @@ var htmx = (function() { if (elt.form && elt.type === 'submit') { return true } - if (elt instanceof HTMLAnchorElement && elt.href && + elt = elt.closest('a') + // @ts-ignore check for a link wrapping the event elt or if elt is a link. elt will be link so href check is fine + if (elt && elt.href && (elt.getAttribute('href') === '#' || elt.getAttribute('href').indexOf('#') !== 0)) { return true } diff --git a/www/static/test/core/internals.js b/www/static/test/core/internals.js index e3d4f96d..9b6d0b97 100644 --- a/www/static/test/core/internals.js +++ b/www/static/test/core/internals.js @@ -107,6 +107,11 @@ describe('Core htmx internals Tests', function() { htmx._('shouldCancel')({ type: 'submit', target: anchorThatShouldNotCancel }, form).should.equal(false) htmx._('shouldCancel')({ type: 'click', target: divThatShouldNotCancel }, form).should.equal(false) + // check elements inside links getting click events should cancel parent links + var anchorWithButton = make("<a href='/foo'><button></button></a>") + htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton).should.equal(true) + htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton.firstChild).should.equal(true) + form = make('<form id="f1">' + '<input id="insideInput" type="submit">' + '<button id="insideFormBtn"></button>' + diff --git a/www/static/test/core/regressions.js b/www/static/test/core/regressions.js index 015b5815..91a35a0e 100644 --- a/www/static/test/core/regressions.js +++ b/www/static/test/core/regressions.js @@ -291,4 +291,46 @@ describe('Core htmx Regression Tests', function() { byId('datefield').click() }) + + it('a button clicked inside an htmx enabled link will prevent the link from navigating on click', function(done) { + var defaultPrevented = 'unset' + var link = make('<a href="/foo" hx-get="/foo"><button>test</button></a>') + var button = link.firstChild + + htmx.on(link, 'click', function(evt) { + // we need to wait so the state of the evt is finalized + setTimeout(() => { + defaultPrevented = evt.defaultPrevented + try { + defaultPrevented.should.equal(true) + done() + } catch (err) { + done(err) + } + }, 0) + }) + + button.click() + }) + + it('a htmx enabled button clicked inside a link will prevent the link from navigating on click', function(done) { + var defaultPrevented = 'unset' + var link = make('<a href="/foo"><button hx-get="/foo">test</button></a>') + var button = link.firstChild + + htmx.on(link, 'click', function(evt) { + // we need to wait so the state of the evt is finalized + setTimeout(() => { + defaultPrevented = evt.defaultPrevented + try { + defaultPrevented.should.equal(true) + done() + } catch (err) { + done(err) + } + }, 0) + }) + + button.click() + }) }) |