From da2105eb52b7ab94c226d7a288ce69b9caaa7461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20=C5=9Apiewak?= Date: Tue, 12 Mar 2024 09:03:33 +0100 Subject: [PATCH] Revert "Remove URL path de-emphasis" This reverts commit 84be4eae2ecc6b2c67b8d93acf621f47194e4607. See https://github.com/duckduckgo/iOS/pull/2357 --- DuckDuckGo/AddressDisplayHelper.swift | 38 ++++++++++++++-- DuckDuckGo/OmniBar.swift | 4 +- .../AddressDisplayHelperTests.swift | 44 +++++++++++++++++-- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/DuckDuckGo/AddressDisplayHelper.swift b/DuckDuckGo/AddressDisplayHelper.swift index 16b508bdeb..0016af4702 100644 --- a/DuckDuckGo/AddressDisplayHelper.swift +++ b/DuckDuckGo/AddressDisplayHelper.swift @@ -23,12 +23,42 @@ extension OmniBar { struct AddressDisplayHelper { - static func addressForDisplay(url: URL, showsFullURL: Bool) -> String { - guard !showsFullURL, let shortAddress = shortURLString(url) else { - return url.absoluteString + 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) } - return shortAddress + return attributedString } /// Creates a string containing a short version the http(s) URL. diff --git a/DuckDuckGo/OmniBar.swift b/DuckDuckGo/OmniBar.swift index 282bd81f81..7feb722c3e 100644 --- a/DuckDuckGo/OmniBar.swift +++ b/DuckDuckGo/OmniBar.swift @@ -384,7 +384,7 @@ class OmniBar: UIView { if let query = url.searchQuery { textField.text = query } else { - textField.text = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing || forceFullURL) + textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing || forceFullURL) } } @@ -530,7 +530,7 @@ extension OmniBar: Themable { searchStackContainer?.tintColor = theme.barTintColor if let url = textField.text.flatMap({ URL(trimmedAddressBarString: $0.trimmingWhitespace()) }) { - textField.text = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing) + textField.attributedText = 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 5aa4108aa2..1de073f856 100644 --- a/DuckDuckGoTests/AddressDisplayHelperTests.swift +++ b/DuckDuckGoTests/AddressDisplayHelperTests.swift @@ -25,6 +25,44 @@ 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") @@ -46,18 +84,18 @@ class AddressDisplayHelperTests: XCTestCase { func testShortensURLWhenShortVersionExpected() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: false) - XCTAssertEqual(addressForDisplay, "some.domain.eu") + XCTAssertEqual(addressForDisplay.string, "some.domain.eu") } func testDoesNotShortenURLWhenFullVersionExpected() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: true) - XCTAssertEqual(addressForDisplay, "http://some.domain.eu/with/path") + XCTAssertEqual(addressForDisplay.string, "http://some.domain.eu/with/path") } func testFallsBackToLongURLWhenCannotProduceShortURL() { let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "file:///some/path")!, showsFullURL: false) - XCTAssertEqual(addressForDisplay, "file:///some/path") + XCTAssertEqual(addressForDisplay.string, "file:///some/path") } }