Repository navigation
Conversation
hx-on handlers no longer prevent a plain form's submit or a link's navigation; a modifier-key click on an hx-get link opens the new tab. Make the modifier-click tests dispatch cancelable events.
paulscan
force-pushed
the
fix/hx-on-default-prevention
branch
from
November 5, 2026 18:24
43f5a5b to
7cc2789
Compare
Author
|
A side-by-side demo of this one, since it's blocking 4.0.1 for several of my projects: Left frame loads 4.0.0, right frame loads 4.0.1, same markup in both. Click Save and the docs link in each column, then cmd/ctrl/shift-click the hx-get link.
None of these handlers use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
In 4.0.1 an
hx-on:submiton a plain form stops the form submitting, anhx-on:clickon a plain link stops it navigating, and a cmd/ctrl/shift-click on<a href hx-get>is cancelled with no request sent (#4187). #4062 moved the#shouldCancelcheck to the first line of the guardedspec.handlerin#onTrigger, whichhx-onhandlers also run through, and ahead of the modifier-key check in#handleTriggerEvent.#initializeTriggersnow passes a fourth argument to#onTrigger,request, and the first line becomesif (request && !this.#isModifierKeyClick(evt) && this.#shouldCancel(evt)) evt.preventDefault();. The check stays first, so a delayed request trigger still cancels the native submit synchronously (#4057), and out-of-scopefrom:/target:request triggers still cancel.hx-onprevents only through itspreventandhaltmodifiers, in the filtered form too. Extension triggers registered throughapi.onTriggerare treated likehx-on; hx-ws already prevents form submits itself.Fixes #4187
Testing
cancel-behavior.jsdispatchedMouseEvents withoutcancelable: true.preventDefault()is a no-op on a non-cancelable event anddefaultPreventedstaysfalse, so the tests passed whether or not htmx cancelled the click. They now dispatchcancelable: true, as a real click is, and also assert that no request was sent, so a build that lets the browser open the tab but still fires the request fails too. On 4.0.1 they fail withexpected true to equal false.hx-on.js:hx-on:submitwithoutpreventon a plainform does not prevent the native submit. It fails on 4.0.1.
npm testin Chromium: everything passes except the twoscroll restoration on history traversaltests, which fail for me onfour-devwithout this change too. The suite was also run in WebKit and Firefox through Playwright.Checklist
four-dev(htmx 4 source)Disclosure: an AI agent (Claude Code / Fable 5.1) found the regression during a run of my private browser test suite against 4.0.1 and suggested the fix. I accepted and audited the source change + the test, and ran the suite myself.