Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BlazorComponentsGuidance.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ validation guidance rather than defining a separate architecture.
- Preserve Blazor's inbound route precedence: compare segments from left to right; for simple segments, literal segments precede constrained parameters, unconstrained parameters, constrained catch-all parameters, and unconstrained catch-all parameters. Optionality is not a precedence tie-breaker: `/users/{id}` and `/users/{id?}` must be rejected as ambiguous rather than preferring the required parameter. Supplemental implementation/test references: [`RoutePrecedence.ComputeInbound`](../src/Http/Routing/src/Template/RoutePrecedence.cs) and [`RouteTableFactoryTests.ThrowsForOptionalParametersAndNonOptionalParameters`](../src/Components/Components/test/Routing/RouteTableFactoryTests.cs).
- Preserve exclusions for download and non-navigation links, real page loads for external replace-history navigation, and server/client `NavigationManager` synchronization.
- Enhanced form and navigation tests should use explicit promises, per-test storage IDs, and isolated hooks instead of timeouts or shared ambient state.
- Enhanced-navigation click interception stays on document so native document-level stopPropagation does not disable it. The shared renderer-listener registration hook keeps it after every renderer's click delegate, including active-listener re-registration. Reordering during a managed click is postponed until the navigation listener runs so it is not skipped for that click; keep static SSR navigation working after interactive circuits are disposed.

### JS interop, browser APIs, and serialization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { EventFieldInfo } from './EventFieldInfo';
import { eventNameAliasRegisteredCallbacks, getBrowserEventName, getEventNameAliases, getEventTypeOptions } from './EventTypes';
import { isRendererAttached, dispatchEvent } from '../WebRendererInteropMethods';
import { dispatchGlobalEvent, notifyGlobalListenerAdded } from './EventDelegatorListeners';

const nonBubblingEvents = toLookup([
'abort',
Expand Down Expand Up @@ -74,7 +75,8 @@ export class EventDelegator {
constructor(private browserRendererId: number) {
const eventDelegatorId = ++EventDelegator.nextEventDelegatorId;
this.eventsCollectionKey = `_blazorEvents_${eventDelegatorId}`;
this.eventInfoStore = new EventInfoStore(this.onGlobalEvent.bind(this));
const globalListener = this.onGlobalEvent.bind(this);
this.eventInfoStore = new EventInfoStore(event => dispatchGlobalEvent(event, globalListener));
}

public setListener(element: Element, eventName: string, eventHandlerId: number, renderingComponentId: number): void {
Expand Down Expand Up @@ -294,6 +296,7 @@ class EventInfoStore {
// We preserve the non-bubbling behavior by only dispatching such events to the targeted element.
const useCapture = Object.prototype.hasOwnProperty.call(nonBubblingEvents, eventName);
document.addEventListener(eventName, this.globalListener, useCapture);
notifyGlobalListenerAdded(eventName);
}
}

Expand All @@ -314,6 +317,7 @@ class EventInfoStore {
// We preserve the non-bubbling behavior by only dispatching such events to the targeted element.
const useCapture = Object.prototype.hasOwnProperty.call(nonBubblingEvents, eventName);
document.addEventListener(eventName, this.globalListener, { capture: useCapture, passive: false });
notifyGlobalListenerAdded(eventName);
}

public update(oldEventHandlerId: number, newEventHandlerId: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

let clickListenerRegisteredCallback: ((isDispatchingClick: boolean) => void) | undefined;
let clickDispatchDepth = 0;

export function setClickListenerRegisteredCallback(callback: typeof clickListenerRegisteredCallback): void {
clickListenerRegisteredCallback = callback;
}

export function notifyGlobalListenerAdded(eventName: string): void {
if (eventName === 'click') {
clickListenerRegisteredCallback?.(clickDispatchDepth > 0);
}
}

export function dispatchGlobalEvent(event: Event, listener: EventListener): void {
if (event.type !== 'click') {
listener(event);
return;
}

clickDispatchDepth++;
try {
listener(event);
} finally {
clickDispatchDepth--;
}
}
21 changes: 21 additions & 0 deletions src/Components/Web.JS/src/Services/NavigationEnhancement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { synchronizeDomContent } from '../Rendering/DomMerging/DomSync';
import { attachProgrammaticEnhancedNavigationHandler, handleClickForNavigationInterception, hasInteractiveRouter, isForSamePath, notifyEnhancedNavigationListeners, performScrollToElementOnTheSamePage, isSamePageWithHash } from './NavigationUtils';
import { scheduleScrollReset, ScrollResetSchedule } from '../Rendering/Renderer';
import { setClickListenerRegisteredCallback } from '../Rendering/Events/EventDelegatorListeners';

/*
In effect, we have two separate client-side navigation mechanisms:
Expand Down Expand Up @@ -37,6 +38,7 @@ const acceptHeader = 'text/html; blazor-enhanced-nav=on';
let currentEnhancedNavigationAbortController: AbortController | null;
let navigationEnhancementCallbacks: NavigationEnhancementCallbacks;
let performingEnhancedPageLoad: boolean;
let clickListenerReorderPending = false;

// This gets initialized to the current URL when we load.
// After that, it gets updated every time we successfully complete a navigation.
Expand All @@ -59,6 +61,7 @@ export function hasNeverStartedAnyEnhancedPageLoad() {

export function attachProgressivelyEnhancedNavigationListener(callbacks: NavigationEnhancementCallbacks) {
navigationEnhancementCallbacks = callbacks;
setClickListenerRegisteredCallback(onClickListenerRegistered);
document.addEventListener('click', onDocumentClick);
document.addEventListener('submit', onDocumentSubmit);
window.addEventListener('popstate', onPopState);
Expand All @@ -67,6 +70,8 @@ export function attachProgressivelyEnhancedNavigationListener(callbacks: Navigat
}

export function detachProgressivelyEnhancedNavigationListener() {
setClickListenerRegisteredCallback(undefined);
clickListenerReorderPending = false;
document.removeEventListener('click', onDocumentClick);
document.removeEventListener('submit', onDocumentSubmit);
window.removeEventListener('popstate', onPopState);
Expand All @@ -88,7 +93,23 @@ function performProgrammaticEnhancedNavigation(absoluteInternalHref: string, rep
performEnhancedPageLoad(absoluteInternalHref, /* interceptedLink */ false);
}

function onClickListenerRegistered(isDispatchingClick: boolean) {
// Removing a listener during dispatch skips it for that click. Let it run before reordering.
if (isDispatchingClick) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested clicks can bypass enhanced navigation

If a synchronous interactive click re-registers a renderer’s click listener, then triggers another element’s .click(), the inner navigation listener clears the pending reorder. This removes the navigation listener from the outer event’s dispatch snapshot. The outer link then performs a full page load, losing enhanced-navigation behavior.

This reproduced 10/10 times in Chromium using the PR’s event-delegation and navigation-listener code. Controls without the nested click retained interception. Managed interop and enhanced-page fetching were stubbed, so this is DOM-level evidence, not full Blazor E2E verification.

Reordering should wait until no outer managed click remains active. Add a Selenium regression covering this sequence.

clickListenerReorderPending = true;
return;
}

clickListenerReorderPending = false;
document.removeEventListener('click', onDocumentClick);
document.addEventListener('click', onDocumentClick);
}

function onDocumentClick(event: MouseEvent) {
if (clickListenerReorderPending) {
onClickListenerRegistered(false);
}

if (hasInteractiveRouter()) {
return;
}
Expand Down
Loading
Loading