Repository navigation
Fix: native jquery delegation - #538
Merged
Merged
Conversation
The native (no-jQuery) bindEventCapturer reused the bubbling delegation wrapper, which walks from event.target up to the closest ancestor matching the selector. Capturing is what Blaze uses for non-bubbling events such as mouseenter/mouseleave, which the browser dispatches to every element the pointer crosses. Walking up therefore fired a 'mouseleave .card' handler, with currentTarget set to .card, when the pointer merely left a child of .card (and 'mouseenter .card' when it entered one), so hover states built on these handlers flicker or drop. Match the jQuery branch of bindEventCapturer instead: call the handler only when event.target itself is one of $(elem).find(selector), still evaluating the selector rooted at the delegation element. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The native (no-jQuery) delegateEvents registered one listener per handler, and each listener only called its handler for the closest ancestor-or-self of event.target matching the selector. jQuery's delegated dispatch instead walks from event.target up to the delegation root and, at every element on the way, runs each handler whose selector matches it, innermost element first and in bind order within an element. As a result, on the native backend: - 'click .item' with nested .item elements fired once instead of once per matching element; - with 'click .row' bound before 'click .row .del', clicking .del ran the .row handler first, and stopPropagation() in the .del handler did not keep the .row handler from running; - returning false called stopImmediatePropagation(), so other handlers matching the same element never ran (jQuery only stops the climb). Delegate through one listener per element and event type that builds the same handler queue as jQuery.event.dispatch and runs it level by level, stopping the climb on stopPropagation() or a false return value and everything on stopImmediatePropagation(). Selectors are still evaluated rooted at the delegation element (:scope), and focus/blur are still delegated as focusin/focusout. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…runs The native (no-jQuery) backend shows handlers the matched element as currentTarget by defining an own currentTarget property on the native event, and never removed it. Every later listener of the same dispatch, e.g. a document-level addEventListener, then read the element the Blaze handler matched instead of its own currentTarget, and the event kept reporting that element after dispatch, where the native getter returns null. In jQuery mode handlers receive a jQuery event object and the native event is never modified. Delete the own property once the handler returns (also when it throws), so the native getter is visible again. This covers both delegated and capturing handlers, which share createWrapper. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The unscoped `node.matches(delegate.selector)` prefilter threw a SyntaxError for selectors that start with a combinator, such as 'click > .card', which jQuery accepts and roots at the delegation element, and which the beta's native backend handled through `:scope > .card`. Drop the prefilter: the scoped querySelectorAll set is already computed at most once per selector per dispatch, which is no more work than beta.0's per-handler querySelectorAll per event. Adds a test binding 'click > .card' that expects a .card directly under the delegation element to fire and a nested one not to. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Deleting the own currentTarget once the handler returned also took it away from the handler itself: an async handler that read evt.currentTarget after an `await` got null on the native backend, while the jQuery.Event it receives in jQuery mode keeps the matched element. The handler's event and the native event are the same object here, so currentTarget is now an own getter instead: the matched element while the handler runs and once the dispatch is over (eventPhase NONE), and the native value in between, so later listeners of the same dispatch still see their own currentTarget. The native getter is looked up on the event's own prototype chain, so events from a same-origin iframe use their realm's getter. The two existing tests now expect the matched element after the dispatch, and a new test reads evt.currentTarget after an awaited timer. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
# Conflicts: # packages/blaze/dombackend.js
# Conflicts: # packages/blaze/dombackend.js # packages/spacebars-tests/template_tests.html # packages/spacebars-tests/template_tests.js
…522 Merging the three PRs leaves createWrapper unused: delegated events go through dispatchDelegatedEvent (#521) and capturing events through createCaptureWrapper (#518). Remove it and route both paths through withCurrentTarget (#522), so handlers see the matched element as currentTarget, later listeners of the same dispatch see their own, and async handlers can still read it after an await. Convert the comments of the new helpers to JSDoc, as requested in the review of #518.
The four helpers documented with JSDoc in the resolution commit are module-private, but `npm run docs:api` publishes every JSDoc block of packages/blaze as a global function in site/source/api/blaze.md. Mark them @Private, which jsdoc-to-markdown leaves out, so the generated API page stays unchanged.
…ispatch The native dispatch wraps `stopPropagation` and `stopImmediatePropagation` on the event to track the calls, then deleted both own properties. When another library had replaced either method on that event, to record the calls for instance, its version was lost for the listeners that run after the dispatch. Save the event's own descriptors before wrapping and restore them in `finally`; delete only when the event had none. The test replaces both methods from a capturing listener on `document`, calls them from a bubbling listener after the dispatch, and checks that both replacements are reached.
…ion-bundle thanks to @drone1 and @dupontbertrand - fix(blaze): bundle the native backend event fixes (bundle of #518, #521, #522) by @dupontbertrand - fix(blaze): only fire capturing event handlers for a matching target by @drone1 - fix(blaze): run native delegated handlers for every nested match, innermost element first, like jQuery by @drone1 - fix(blaze): restore the native event's currentTarget after a native-backend handler runs @drone1
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.
Merging the jquery fixes from #525 (bundling #518 #521 #522) into our release 3.1.0 brangh