Skip to content

Commit 1b36f6e

Browse files
committed
refactor(common): Add hashchange event for traversals (angular#60682)
This commit adds the logic to emit the `hashchange` event for traversals. PR Close angular#60682
1 parent 8880ed6 commit 1b36f6e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/core/primitives/dom-navigation/testing/fake_navigation.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,17 @@ export class FakeNavigation implements Navigation {
493493
* @internal
494494
*/
495495
userAgentTraverse(navigateEvent: InternalFakeNavigateEvent) {
496+
const oldUrl = this.currentEntry.url!;
496497
this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
497498
// Happens as part of "updating the document" steps https://whatpr.org/html/10919/browsing-the-web.html#updating-the-document
498499
const popStateEvent = createPopStateEvent({
499500
state: navigateEvent.destination.getHistoryState(),
500501
});
501502
this.window.dispatchEvent(popStateEvent);
502-
// TODO(atscott): If oldURL's fragment is not equal to entry's URL's fragment, then queue a global task to fire an event named hashchange
503+
if (navigateEvent.hashChange) {
504+
const hashchangeEvent = createHashChangeEvent(oldUrl, this.currentEntry.url!);
505+
this.window.dispatchEvent(hashchangeEvent);
506+
}
503507
}
504508

505509
/**
@@ -1061,6 +1065,16 @@ function createPopStateEvent({state}: {state: unknown}) {
10611065
return event as PopStateEvent;
10621066
}
10631067

1068+
function createHashChangeEvent(newURL: string, oldURL: string) {
1069+
const event = new Event('hashchange', {
1070+
bubbles: false,
1071+
cancelable: false,
1072+
}) as {-readonly [P in keyof HashChangeEvent]: HashChangeEvent[P]};
1073+
event.newURL = newURL;
1074+
event.oldURL = oldURL;
1075+
return event as HashChangeEvent;
1076+
}
1077+
10641078
/**
10651079
* Fake equivalent of `NavigationDestination`.
10661080
*/

0 commit comments

Comments
 (0)