-
Notifications
You must be signed in to change notification settings - Fork 16
Fix bug where Back navigation doesn't work properly with bangs #1503
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,4 +92,20 @@ extension String { | |
| return hasPrefix(other) || other.hasPrefix(self) | ||
| } | ||
|
|
||
| // MARK: - Search with bang | ||
|
|
||
| private static let searchWithBangPattern = "^.+\\?q=%21[a-zA-Z]+(?:%20[a-zA-Z]+)*$" | ||
|
|
||
| private static var compiledSearchWithBangRegex: NSRegularExpression? = { | ||
| if let newRegex = try? NSRegularExpression(pattern: searchWithBangPattern, options: .caseInsensitive) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check BSK.Common.StringExtension -> RegEx for regex instantiation helper (to be updated with compile-time checked macro with xc15) |
||
| return newRegex | ||
| } | ||
| return nil | ||
| }() | ||
|
|
||
| var isSearchWithBang: Bool { | ||
| guard let regex = Self.compiledSearchWithBangRegex else { return false } | ||
| return self.url?.isDuckDuckGoSearch ?? false && regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count)) != nil | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -670,7 +670,8 @@ protocol NewWindowPolicyDecisionMaker { | |
| return | ||
| } | ||
|
|
||
| let canGoBack = webView.canGoBack || self.error != nil | ||
| let isJustOneRidirectGoingBack = webView.backForwardList.backList.count == 1 && webView.backForwardList.backItem?.url.absoluteString.contains("%21") ?? false | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems error-prone since it‘s checking for "%21" for every url |
||
| let canGoBack = (webView.canGoBack && !isJustOneRidirectGoingBack) || self.error != nil | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the meaning of the check is non-straightforward and would be good if it had some comment on its purpose |
||
| let canGoForward = webView.canGoForward && self.error == nil | ||
| let canReload = (self.content.urlForWebView?.scheme ?? URL.NavigationalScheme.about.rawValue) != URL.NavigationalScheme.about.rawValue | ||
|
|
||
|
|
@@ -700,13 +701,29 @@ protocol NewWindowPolicyDecisionMaker { | |
| } | ||
|
|
||
| userInteractionDialog = nil | ||
|
|
||
| // If search with bang go back of 2 spaces (to avoid redirect) | ||
| if let urlString = webView.backForwardList.backItem?.url.absoluteString, urlString.isSearchWithBang { | ||
| if let item = webView.backForwardList.item(at: -2) { | ||
| return webView.navigator()?.go(to: item) | ||
| } | ||
| } | ||
|
Comment on lines
+706
to
+710
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this check doesn‘t belong here as we were struggling to reduce navigation policy decision logics and move it away to TabExtensions |
||
|
|
||
| return webView.navigator()?.goBack(withExpectedNavigationType: .backForward(distance: -1)) | ||
| } | ||
|
|
||
| @MainActor | ||
| @discardableResult | ||
| func goForward() -> ExpectedNavigation? { | ||
| guard canGoForward else { return nil } | ||
|
|
||
| // If search with bang go forward of 2 spaces (to avoid redirect) | ||
| if let urlString = webView.backForwardList.forwardItem?.url.absoluteString, urlString.isSearchWithBang { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these won‘t work for gesture (2-finger swipe) back/forw navigations |
||
| if let item = webView.backForwardList.item(at: +2) { | ||
| return webView.navigator()?.go(to: item) | ||
| } | ||
| } | ||
|
|
||
| return webView.navigator()?.goForward(withExpectedNavigationType: .backForward(distance: 1)) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // | ||
| // TabUrlExtensionTests.swift | ||
| // | ||
| // Copyright © 2023 DuckDuckGo. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import DuckDuckGo_Privacy_Browser | ||
|
|
||
| final class TabStringExtensionTests: XCTestCase { | ||
|
|
||
| func testSearchWithBangDetection() { | ||
| let searchWithBang: [String] = [ | ||
| "https://duckduckgo.com/?q=%21Hello", | ||
| "https://duckduckgo.com/?q=%21Hello%20World", | ||
| "https://duckduckgo.com/?q=%21Search%20With%20Bang" | ||
| ] | ||
|
|
||
| let nonSearchWithBang: [String] = [ | ||
| "https://duckduckgo.com/?q=%21", | ||
| "https://duckduckgo.com/?q=%21%20", | ||
| "https://duckduckgo.com/?q=%21%20test", | ||
| "https://duckduckgo.com/?q=test%21test", | ||
| ] | ||
|
|
||
| for url in searchWithBang { | ||
| XCTAssertTrue(url.isSearchWithBang, "\(url) should be detected as a search with bang") | ||
| } | ||
|
|
||
| for url in nonSearchWithBang { | ||
| XCTAssertFalse(url.isSearchWithBang, "\(url) should not be detected as search with bang") | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs some comment describing what the regex does as it‘s non-trivial