Skip to content

Commit

Permalink
Revert "Remove URL path de-emphasis"
Browse files Browse the repository at this point in the history
This reverts commit 84be4ea.

See
#2357
  • Loading branch information
dus7 committed Mar 15, 2024
1 parent ae00631 commit da2105e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
38 changes: 34 additions & 4 deletions DuckDuckGo/AddressDisplayHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/OmniBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down
44 changes: 41 additions & 3 deletions DuckDuckGoTests/AddressDisplayHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/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:[email protected]: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")
Expand All @@ -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")
}
}

0 comments on commit da2105e

Please sign in to comment.