diff --git a/DuckDuckGo/AddressDisplayHelper.swift b/DuckDuckGo/AddressDisplayHelper.swift index 0016af4702..16b508bdeb 100644 --- a/DuckDuckGo/AddressDisplayHelper.swift +++ b/DuckDuckGo/AddressDisplayHelper.swift @@ -23,42 +23,12 @@ extension OmniBar { struct AddressDisplayHelper { - static func addressForDisplay(url: URL, showsFullURL: Bool) -> NSAttributedString { - - if !showsFullURL, let shortAddress = shortURLString(url) { - return NSAttributedString( - string: shortAddress, - attributes: [.foregroundColor: ThemeManager.shared.currentTheme.searchBarTextColor]) - } else { - return deemphasisePath(forUrl: url) - } - } - - static func deemphasisePath(forUrl url: URL) -> NSAttributedString { - - let s = url.absoluteString - let attributedString = NSMutableAttributedString(string: s) - guard let c = URLComponents(url: url, resolvingAgainstBaseURL: true) else { - return attributedString - } - - let theme = ThemeManager.shared.currentTheme - - if let pathStart = c.rangeOfPath?.lowerBound { - let urlEnd = s.endIndex - - let pathRange = NSRange(pathStart ..< urlEnd, in: s) - attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextDeemphasisColor, range: pathRange) - - let domainRange = NSRange(s.startIndex ..< pathStart, in: s) - attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextColor, range: domainRange) - - } else { - let range = NSRange(s.startIndex ..< s.endIndex, in: s) - attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextColor, range: range) + static func addressForDisplay(url: URL, showsFullURL: Bool) -> String { + guard !showsFullURL, let shortAddress = shortURLString(url) else { + return url.absoluteString } - return attributedString + return shortAddress } /// Creates a string containing a short version the http(s) URL. diff --git a/DuckDuckGo/OmniBar.swift b/DuckDuckGo/OmniBar.swift index ec5969f657..4a3bc54fd0 100644 --- a/DuckDuckGo/OmniBar.swift +++ b/DuckDuckGo/OmniBar.swift @@ -394,7 +394,7 @@ class OmniBar: UIView { if let query = url.searchQuery { textField.text = query } else { - textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing || forceFullURL) + textField.text = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing || forceFullURL) } } @@ -540,7 +540,7 @@ extension OmniBar: Themable { searchStackContainer?.tintColor = theme.barTintColor if let url = textField.text.flatMap({ URL(trimmedAddressBarString: $0.trimmingWhitespace()) }) { - textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing) + textField.text = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing) } textField.textColor = theme.searchBarTextColor textField.tintColor = UIColor(designSystemColor: .accent) diff --git a/DuckDuckGoTests/AddressDisplayHelperTests.swift b/DuckDuckGoTests/AddressDisplayHelperTests.swift index 7894c758f0..c4122c4f20 100644 --- a/DuckDuckGoTests/AddressDisplayHelperTests.swift +++ b/DuckDuckGoTests/AddressDisplayHelperTests.swift @@ -23,44 +23,6 @@ class AddressDisplayHelperTests: XCTestCase { private typealias AddressHelper = OmniBar.AddressDisplayHelper - func testDeemphasisePathDoesNotCrash() { - - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "example.com")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "example.com")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "a/b")!) - - testWith(prefix: "http:///") // crashes but we don't allow it anyway - testWith(prefix: "http://localhost") - testWith(prefix: "http://localhost/") - testWith(prefix: "http://example.com") - testWith(prefix: "http://example.com/") - testWith(prefix: "http://example.com/path") - testWith(prefix: "http://example.com/path/") - testWith(prefix: "http://user:password@example.com/path/") - - testWith(prefix: "http://localhost:8080") - testWith(prefix: "http://localhost:8080/") - testWith(prefix: "http://example.com:8080") - testWith(prefix: "http://example.com:8080/") - testWith(prefix: "http://example.com:8080/path") - testWith(prefix: "http://example.com:8080/path/") - testWith(prefix: "http://user:password@example.com:8080/path/") - - } - - private func testWith(prefix: String) { - - _ = AddressHelper.deemphasisePath(forUrl: URL(string: prefix)!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)#")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)#/fragment")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)?")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)?x=1")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)?x=1&")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)?x=1&y=1")!) - _ = AddressHelper.deemphasisePath(forUrl: URL(string: "\(prefix)?x=1&y=1,2")!) - - } - func testShortURL() { XCTAssertEqual(AddressHelper.shortURLString(URL(string: "https://www.duckduckgo.com")!), "duckduckgo.com") @@ -82,18 +44,18 @@ class AddressDisplayHelperTests: XCTestCase { func testShortensURLWhenShortVersionExpected() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: false) - XCTAssertEqual(addressForDisplay.string, "some.domain.eu") + XCTAssertEqual(addressForDisplay, "some.domain.eu") } func testDoesNotShortenURLWhenFullVersionExpected() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: true) - XCTAssertEqual(addressForDisplay.string, "http://some.domain.eu/with/path") + XCTAssertEqual(addressForDisplay, "http://some.domain.eu/with/path") } func testFallsBackToLongURLWhenCannotProduceShortURL() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "file:///some/path")!, showsFullURL: false) - XCTAssertEqual(addressForDisplay.string, "file:///some/path") + XCTAssertEqual(addressForDisplay, "file:///some/path") } }