From e7e7b1cb375f7122a46f673d2858e5e7b59d16d8 Mon Sep 17 00:00:00 2001 From: carson Date: Fri, 12 Nov 2021 16:43:28 -0700 Subject: [PATCH] Only cancel clicks and submits fixes https://github.com/bigskysoftware/htmx/issues/588 --- src/htmx.js | 21 +++++++++++++++------ test/attributes/hx-boost.js | 5 +++++ test/core/internals.js | 12 ++++++------ test/scratch.html | 6 +----- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 3ccb2b74..61d3ad2a 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -989,11 +989,20 @@ return (function () { } } - function shouldCancel(elt) { - return elt.tagName === "FORM" || - (matches(elt, 'input[type="submit"], button') && closest(elt, 'form') !== null) || - (elt.tagName === "A" && elt.href && (elt.getAttribute('href') === '#' || - elt.getAttribute('href').indexOf("#") !== 0)); + function shouldCancel(evt, elt) { + if (evt.type === "submit" || evt.type === "click") { + if (elt.tagName === "FORM") { + return true; + } + if (matches(elt, 'input[type="submit"], button') && closest(elt, 'form') !== null) { + return true; + } + if (elt.tagName === "A" && elt.href && + (elt.getAttribute('href') === '#' || elt.getAttribute('href').indexOf("#") !== 0)) { + return true; + } + } + return false; } function ignoreBoostedAnchorCtrlClick(elt, evt) { @@ -1029,7 +1038,7 @@ return (function () { if (ignoreBoostedAnchorCtrlClick(elt, evt)) { return; } - if (explicitCancel || shouldCancel(elt)) { + if (explicitCancel || shouldCancel(evt, elt)) { evt.preventDefault(); } if (maybeFilterEvent(triggerSpec, evt)) { diff --git a/test/attributes/hx-boost.js b/test/attributes/hx-boost.js index 252c22e2..29cff593 100644 --- a/test/attributes/hx-boost.js +++ b/test/attributes/hx-boost.js @@ -70,6 +70,11 @@ describe("hx-boost attribute", function() { } }) + it('anchors w/ explicit targets are not boosted', function () { + var a = make('Foo'); + var internalData = htmx._("getInternalData")(a); + should.equal(undefined, internalData.boosted); + }) it('includes an HX-Boosted Header', function() { diff --git a/test/core/internals.js b/test/core/internals.js index 8a9d5940..2da08457 100644 --- a/test/core/internals.js +++ b/test/core/internals.js @@ -87,24 +87,24 @@ describe("Core htmx internals Tests", function() { it("tags respond correctly to shouldCancel", function() { var anchorThatShouldCancel = make(""); - htmx._("shouldCancel")(anchorThatShouldCancel).should.equal(true); + htmx._("shouldCancel")({type:'click'}, anchorThatShouldCancel).should.equal(true); var anchorThatShouldCancel = make(""); - htmx._("shouldCancel")(anchorThatShouldCancel).should.equal(true); + htmx._("shouldCancel")({type:'click'}, anchorThatShouldCancel).should.equal(true); var anchorThatShouldNotCancel = make(""); - htmx._("shouldCancel")(anchorThatShouldNotCancel).should.equal(false); + htmx._("shouldCancel")({type:'click'}, anchorThatShouldNotCancel).should.equal(false); var form = make("
"); - htmx._("shouldCancel")(form).should.equal(true); + htmx._("shouldCancel")({type:'submit'}, form).should.equal(true); var form = make("
"); var input = byId("i1"); - htmx._("shouldCancel")(input).should.equal(true); + htmx._("shouldCancel")({type:'click'}, input).should.equal(true); var form = make("
+ Asdf