-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug-Fix: Modified click trigger on form elements prevent default behaviour of clicked element #2771
base: dev
Are you sure you want to change the base?
Bug-Fix: Modified click trigger on form elements prevent default behaviour of clicked element #2771
Conversation
Hey,
As we don't want to introduce breaking changes to the lib, and as As for the failing tests, these timeouts are sometimes annoying, maybe try increasing the timeout values if you have a not-so-great internet connection? As those tests are making requests to an external domain to test the security features |
Hi @Telroshan thank you very much for your feedback. It's my first PR I highly appreciate it :) |
Bumps [ws](https://github.com/websockets/ws) from 8.14.2 to 8.17.1. - [Release notes](https://github.com/websockets/ws/releases) - [Commits](websockets/ws@8.14.2...8.17.1) --- updated-dependencies: - dependency-name: ws dependency-type: direct:development ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* Update reference.md * Update htmx.js * Update reference.md
Update README.md fix url to htmx extensions
Update hx-trigger.js
* docs: update jetbrains sponsor logo * docs: support light and dark theme for jetbrains logo --------- Co-authored-by: Jan-Niklas Wortmann <[email protected]>
73e57b9
to
5389f07
Compare
Hi, @Telroshan I rebased it against dev.
You are absolutely right but I think in this case it's acceptable. What did I change?
Why do I had to change existing tests? Line 97 in 45d4bec
So I had to add to those tests the a target object: In short: Yes I had to change existing tests BUT I only added a object property that in reality is present. About the failing tests: I assume it has something to do with the mocha-chrome lib. When I start the tests I get this error:
So I don't know how to proceed - I am confident the failing and pendig tests have nothing todo with my changes because the same fail when I run them on master or dev of the main repo. Thanks for your help |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @pfiadDi , seems the rebase went wrong as other unrelated commits are still here 😬
Got it, you're right, didn't notice these tests were incorrectly mocking an event's properties.
|
||
var form = make("<form><input id='i1' type='submit'></form>") | ||
var input = byId('i1') | ||
htmx._('shouldCancel')({ type: 'click' }, input).should.equal(true) | ||
htmx._('shouldCancel')({ type: 'click', target: document.createElement('input') }, input).should.equal(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we use the input
var here though instead of creating a dummy element on the fly? As input
is the clicked element, the event's target would be that very element in a real situation right?
Same goes for the form
above, and the button
after
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you're right
@@ -2313,7 +2313,7 @@ var htmx = (function() { | |||
return false | |||
} | |||
if (evt.type === 'submit' || evt.type === 'click') { | |||
if (elt.tagName === 'FORM') { | |||
if (elt.tagName === 'FORM' && asElement(evt.target)?.tagName === 'FORM') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering ; couldn't we simply check if (asElement(evt.target) || elt).tagName === 'FORM'
in this case?
So that, if defined, evt.target
takes precedence, otherwise we keep the existing behavior. You would probably not even need to add the target
property to mock the events of the test suite in this case? As elt
is the fallback
Btw, should we use the same technique for the other checks in this same function? Anchors for ex. Maybe we should simply change toconst elt = asElement(evt.target) || asElement(node)
at the start of the function and not need to modify anything else inside?
Just some thoughts, completely untested, let me know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Telroshan the problem is, the event target is an EventTarget and an EventTarget can be a Dom element but doesn't have to be. So the property tagname can be undefined. (It was my first approach but the type checker complaint). That's why I chose this way to ensure we only check properties which exist and don't run into any errors when trying to access undefined properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an EventTarget can be a Dom element but doesn't have to be. So the property tagname can be undefined.
Hence my suggestion @pfiadDi : const elt = asElement(evt.target) || asElement(node)
Unless I'm mistaked (could totally be, didn't test this 😆 ), this will get the evt.target
if it's a DOM element, but if it's not an element, will fallback to node
.
Then we have the already present if
statement that returns early, in case neither evt.target
nor node
were elements.
I believe you shouldn't get any type checker complaint with that approach as we cast to Element
thanks to asElement
?
Description
The
shouldCancle
function prevents the default behaviour for form elements when clicked. The problem is, when a dynamically added element should trigger the form and the user adds something like:hx-trigger="click[event.target.matches('button#dynamicallyAddedElement')] from:body"
the default behaviour of other elements are prevented as well.A checkbox can't be checked anymore for example.
I added a check so that we only cancel the event when the node with the trigger is a form AND the event target is also one:
Old:
if (elt.tagName === 'FORM') { return true }
New:
if (elt.tagName === 'FORM' && asElement(evt.target)?.tagName === 'FORM') { return true }
Corresponding issue: #2755
Testing
Manually:
New tests:
Checklist
master
for website changes,dev
forsource changes)
approved via an issue
npm run test
) and verified that it succeeded -> YES and NO:I had troubles with the test suite also before working on the bug, I always got this result:
641 passing (9s)
3 pending
2 failing
Core htmx Parameter Handling
form includes last focused button:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Core htmx Parameter Handling
form includes last focused submit:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
After adding my fix and the new tests, my new tests were successful and the same two failed...