@@ -947,13 +947,52 @@ int handleSourceChanged(long pView, long pArgs) {
947947 // to an empty string. Navigations with NavigateToString set the Source
948948 // to about:blank. Initial Source is about:blank. If Source value
949949 // is the same between navigations, SourceChanged isn't fired.
950- // Calling LocationListener#changed() was moved to
951- // handleNavigationCompleted() to have consistent/symmetric
950+ // Since we do not use NavigateToString (but always use a dummy file)
951+ // we always see a proper (file:// URI).
952+ // The main location events are fired from
953+ // handleNavigationStarting() / handleNavigationCompleted()
954+ // to get consistent/symmetric
952955 // LocationListener.changing() -> LocationListener.changed() behavior.
953- // TODO: #fragment navigation inside a page does not result in
954- // NavigationStarted / NavigationCompleted events from WebView2.
955- // so for now we cannot send consistent changing() + changed() events
956- // for those scenarios.
956+ // For #fragment navigation within a page, no NavigationStarted/NavigationCompleted
957+ // events are send from WebView2.
958+ // Instead, SourceChanged is fired.
959+ // We therefore also handle this event specifically for in-same-document scenario.
960+ // Since SourceChanged cannot be blocked, we only send out
961+ // LocationListener#changed() events, no changing() events.
962+ int [] isNewDocument = new int [1 ];
963+ ICoreWebView2SourceChangedEventArgs args = new ICoreWebView2SourceChangedEventArgs (pArgs );
964+ args .get_IsNewDocument (isNewDocument );
965+ if (isNewDocument [0 ] == 0 ) {
966+ // #fragment navigation inside the same document
967+ long [] ppsz = new long [1 ];
968+ int hr = webViewProvider .getWebView (true ).get_Source (ppsz );
969+ if (hr != COM .S_OK ) return hr ;
970+ String url = wstrToString (ppsz [0 ], true );
971+ int fragmentIndex = url .indexOf ('#' );
972+ String urlWithoutFragment = fragmentIndex == -1 ? url : url .substring (0 ,fragmentIndex );
973+ String location ;
974+ if (isLocationForCustomText (urlWithoutFragment )) {
975+ if (fragmentIndex != -1 ) {
976+ location = ABOUT_BLANK .toString () + url .substring (fragmentIndex );
977+ } else {
978+ location = ABOUT_BLANK .toString ();
979+ }
980+ } else {
981+ location = url ;
982+ }
983+ browser .getDisplay ().asyncExec (() -> {
984+ if (browser .isDisposed ()) return ;
985+ LocationEvent event = new LocationEvent (browser );
986+ event .display = browser .getDisplay ();
987+ event .widget = browser ;
988+ event .location = location ;
989+ event .top = true ;
990+ for (LocationListener listener : locationListeners ) {
991+ listener .changed (event );
992+ if (browser .isDisposed ()) return ;
993+ }
994+ });
995+ }
957996 return COM .S_OK ;
958997}
959998
0 commit comments