Skip to content

Fix: native jquery delegation - #538

Merged
jankapunkt merged 11 commits into
release-3.1.0from
fix/native-jquery-delegation
Nov 11, 2026
Merged

jankapunkt merged 11 commits into
release-3.1.0from
fix/native-jquery-delegation

Conversation

@jankapunkt

@jankapunkt jankapunkt commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Merging the jquery fixes from #525 (bundling #518 #521 #522) into our release 3.1.0 brangh

drone1 and others added 11 commits October 10, 2026 17:24
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
@jankapunkt
jankapunkt merged commit dac1e15 into release-3.1.0 Nov 11, 2026
3 checks passed
@dupontbertrand dupontbertrand mentioned this pull request Nov 11, 2026
31 of 33 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants