77 */
88
99import { Route } from '../../../protocol' ;
10- import type { Route as AngularRoute } from '@angular/router' ;
10+ import type { Route as AngularRoute , ActivatedRoute } from '@angular/router' ;
1111
1212export type RoutePropertyType = RouteGuard | 'providers' | 'component' | 'redirectTo' | 'title' ;
1313
@@ -18,15 +18,64 @@ const routeGuards = ['canActivate', 'canActivateChild', 'canDeactivate', 'canMat
1818type Routes = any ;
1919type Router = any ;
2020
21+ /**
22+ * Recursively traverses the ActivatedRoute tree and collects all routeConfig objects.
23+ * @param activatedRoute - The ActivatedRoute to start traversal from
24+ * @param activeRoutes - Set to collect active Route configuration objects
25+ * @returns Set of active Route configuration objects
26+ */
27+ function collectActiveRouteConfigs (
28+ activatedRoute : ActivatedRoute ,
29+ activeRoutes : Set < AngularRoute > = new Set ( ) ,
30+ ) : Set < AngularRoute > {
31+ // Get the routeConfig for this ActivatedRoute
32+ const routeConfig = activatedRoute . routeConfig ;
33+ if ( routeConfig ) {
34+ activeRoutes . add ( routeConfig ) ;
35+ }
36+
37+ // Recursively process all children
38+ const children = activatedRoute . children || [ ] ;
39+ for ( const child of children ) {
40+ collectActiveRouteConfigs ( child , activeRoutes ) ;
41+ }
42+
43+ return activeRoutes ;
44+ }
45+
46+ /**
47+ * Gets the set of currently active Route configuration objects from the router state.
48+ * This function synchronously reads the current router state without waiting for navigation events.
49+ *
50+ * @param router - The Angular Router instance
51+ * @returns A Set containing all Route configuration objects that are currently active
52+ *
53+ * @example
54+ * ```ts
55+ * const activeRoutes = getActiveRouteConfigs(router);
56+ * // activeRoutes is a Set<Route> containing all currently active route configurations
57+ * ```
58+ */
59+ export function getActiveRouteConfigs ( router : Router ) : Set < AngularRoute > {
60+ const rootActivatedRoute = router . routerState ?. root ;
61+ if ( ! rootActivatedRoute ) {
62+ return new Set ( ) ;
63+ }
64+
65+ return collectActiveRouteConfigs ( rootActivatedRoute ) ;
66+ }
67+
2168export function parseRoutes ( router : Router ) : Route {
22- const currentUrl = router . stateManager ?. routerState ?. snapshot ?. url ;
2369 const rootName = 'App Root' ;
2470 const rootChildren = router . config ;
2571
72+ // Get the set of active Route configuration objects from the router state
73+ const activeRouteConfigs = getActiveRouteConfigs ( router ) ;
74+
2675 const root : Route = {
2776 component : rootName ,
2877 path : rootName ,
29- children : rootChildren ? assignChildrenToParent ( null , rootChildren , currentUrl ) : [ ] ,
78+ children : rootChildren ? assignChildrenToParent ( null , rootChildren , activeRouteConfigs ) : [ ] ,
3079 isAux : false ,
3180 isLazy : false ,
3281 isActive : true , // Root is always active.
@@ -52,7 +101,7 @@ function getProviderName(child: any): string[] {
52101function assignChildrenToParent (
53102 parentPath : string | null ,
54103 children : Routes ,
55- currentUrl : string ,
104+ activeRouteConfigs : Set < AngularRoute > ,
56105) : Route [ ] {
57106 return children . map ( ( child : AngularRoute ) => {
58107 const childName = childRouteName ( child ) ;
@@ -66,8 +115,9 @@ function assignChildrenToParent(
66115 const isAux = Boolean ( child . outlet ) ;
67116 const isLazy = Boolean ( child . loadChildren || child . loadComponent ) ;
68117
69- const pathWithoutParams = routePath . split ( '/:' ) [ 0 ] ;
70- const isActive = currentUrl ?. startsWith ( pathWithoutParams ) ;
118+ // Check if this route configuration object is in the active routes set
119+ // This is the direct reference to the Route object from router.config
120+ const isActive = activeRouteConfigs . has ( child ) ;
71121
72122 const routeConfig : Route = {
73123 pathMatch : child . pathMatch ,
@@ -93,7 +143,11 @@ function assignChildrenToParent(
93143 }
94144
95145 if ( childDescendents ) {
96- routeConfig . children = assignChildrenToParent ( routeConfig . path , childDescendents , currentUrl ) ;
146+ routeConfig . children = assignChildrenToParent (
147+ routeConfig . path ,
148+ childDescendents ,
149+ activeRouteConfigs ,
150+ ) ;
97151 }
98152
99153 if ( child . data ) {
0 commit comments