Skip to content

Commit

Permalink
Fix same-document navigation breaking current main frame navigation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx authored Jul 1, 2024
1 parent be669f8 commit ae719ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions Sources/Navigation/DistributedNavigationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -881,21 +881,21 @@ extension DistributedNavigationDelegate: WKNavigationDelegate {
}

} else {
// don‘t mark extra Session State Pop navigations as `current` when there‘s a `current` Anchor navigation stored in `startedNavigation`
let isCurrent = if let startedNavigation {
!(startedNavigation.navigationAction.navigationType.isSameDocumentNavigation && startedNavigation.isCurrent)
} else {
true
}
let shouldBecomeCurrent = {
guard let startedNavigation else { return true } // no current navigation, make the same-doc navigation current
guard startedNavigation.navigationAction.navigationType.isSameDocumentNavigation else { return false } // don‘t intrude into current non-same-doc navigation
// don‘t mark extra Session State Pop navigations as `current` when there‘s a `current` same-doc Anchor navigation stored in `startedNavigation`
return !startedNavigation.isCurrent
}()

navigation = Navigation(identity: NavigationIdentity(wkNavigation), responders: responders, state: .expected(nil), isCurrent: isCurrent)
navigation = Navigation(identity: NavigationIdentity(wkNavigation), responders: responders, state: .expected(nil), isCurrent: shouldBecomeCurrent)
let request = wkNavigation?.request ?? URLRequest(url: webView.url ?? .empty)
let navigationAction = NavigationAction(request: request, navigationType: .sameDocumentNavigation(navigationType), currentHistoryItemIdentity: currentHistoryItemIdentity, redirectHistory: nil, isUserInitiated: wkNavigation?.isUserInitiated ?? false, sourceFrame: .mainFrame(for: webView), targetFrame: .mainFrame(for: webView), shouldDownload: false, mainFrameNavigation: navigation)
navigation.navigationActionReceived(navigationAction)
os_log("new same-doc navigation(.%d): %s (%s): %s, isCurrent: %d", log: log, type: .debug, wkNavigationType, wkNavigation.debugDescription, navigation.debugDescription, navigationAction.debugDescription, isCurrent ? 1 : 0)
os_log("new same-doc navigation(.%d): %s (%s): %s, isCurrent: %d", log: log, type: .debug, wkNavigationType, wkNavigation.debugDescription, navigation.debugDescription, navigationAction.debugDescription, shouldBecomeCurrent ? 1 : 0)

// store `current` navigations in `startedNavigation` to get `currentNavigation` published
if isCurrent {
if shouldBecomeCurrent {
self.startedNavigation = navigation
}
// mark Navigation as finished as we‘re in __did__SameDocumentNavigation
Expand Down
4 changes: 2 additions & 2 deletions Tests/NavigationTests/SameDocumentNavigationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ class SameDocumentNavigationTests: DistributedNavigationDelegateTestsBase {
.response(Nav(action: navAct(1), .responseReceived, resp: .resp(urls.local, data.sessionStatePushClientRedirectData.count, headers: .default + ["Content-Type": "text/html"]))),
.didCommit(Nav(action: navAct(1), .responseReceived, resp: resp(0), .committed)),

.didSameDocumentNavigation(Nav(action: NavAction(req(urls.localHashed1, [:]), .sameDocumentNavigation(.sessionStatePush), from: history[1], src: main(urls.localHashed1)), .finished), 1),
.didSameDocumentNavigation(Nav(action: NavAction(req(urls.localHashed1, [:]), .sameDocumentNavigation(.sessionStatePush), from: history[1], src: main(urls.localHashed1)), .finished, isCurrent: false), 1),

.didFinish(Nav(action: navAct(1), .finished, resp: resp(0), .committed, isCurrent: false)),
.didFinish(Nav(action: navAct(1), .finished, resp: resp(0), .committed, isCurrent: true)),
])
}

Expand Down

0 comments on commit ae719ad

Please sign in to comment.