Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Sources/Panels/BrowserPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1532,10 +1532,10 @@ private class BrowserNavigationDelegate: NSObject, WKNavigationDelegate {
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
) {
// target=_blank or window.open() — open in a new tab instead of a new window
// target=_blank or window.open() — navigate in the current webview
if navigationAction.targetFrame == nil,
let url = navigationAction.request.url {
openInNewTab?(url)
webView.load(URLRequest(url: url))
decisionHandler(.cancel)
return
}
Expand All @@ -1558,16 +1558,20 @@ private class BrowserNavigationDelegate: NSObject, WKNavigationDelegate {
private class BrowserUIDelegate: NSObject, WKUIDelegate {
var openInNewTab: ((URL) -> Void)?

/// Handle cmd+click / target=_blank links. Returning nil tells WebKit not to open a new window;
/// instead we open the URL as a new surface in the same pane.
/// Returning nil tells WebKit not to open a new window.
/// Cmd+click opens in a new tab; regular target=_blank navigates in-place.
func webView(
_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures
) -> WKWebView? {
if let url = navigationAction.request.url {
openInNewTab?(url)
if navigationAction.modifierFlags.contains(.command) {
openInNewTab?(url)
} else {
webView.load(URLRequest(url: url))
}
}
return nil
}
Expand Down