Skip to content

Commit ab53b92

Browse files
authored
Merge pull request #5080 from danielpunkass/support-internal-links
Support for internal page link navigation
2 parents fb895d4 + 1aa6663 commit ab53b92

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Mac/MainWindow/Detail/DetailWebViewController.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,24 @@ extension DetailWebViewController: WKNavigationDelegate, WKUIDelegate {
200200

201201
// WKNavigationDelegate
202202

203+
// Prevent user navigations within a page (for example, to elements with ids) from being dispatched to the browser
204+
func isInternalPageNavigationAction(_ navigationAction: WKNavigationAction) -> Bool {
205+
// If the origin and destination of the navigation have the same host and path, then
206+
// the presence of a fragment should be considered "internal" to an article.
207+
if let targetURL = navigationAction.request.url,
208+
let fragment = targetURL.fragment, fragment.isEmpty == false,
209+
let sourceURL = self.webView.url,
210+
sourceURL.host == targetURL.host,
211+
sourceURL.path == targetURL.path {
212+
return true
213+
}
214+
else {
215+
return false
216+
}
217+
}
218+
203219
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping @MainActor @Sendable (WKNavigationActionPolicy) -> Void) {
204-
if navigationAction.navigationType == .linkActivated {
220+
if navigationAction.navigationType == .linkActivated, !isInternalPageNavigationAction(navigationAction) {
205221
if let url = navigationAction.request.url {
206222
self.openInBrowser(url, flags: navigationAction.modifierFlags)
207223
}

0 commit comments

Comments
 (0)