Skip to content

Commit

Permalink
Remove URL path de-emphasis
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Jan 22, 2024
1 parent 56d9f65 commit 84be4ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 77 deletions.
38 changes: 4 additions & 34 deletions DuckDuckGo/AddressDisplayHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/OmniBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

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

0 comments on commit 84be4ea

Please sign in to comment.