-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Url variants for bookmark icon (#3341)
Task/Issue URL: https://app.asana.com/0/1207340338530322/1205619096931465/f **Description**: The address bar bookmark icon now indicates that a URL is bookmarked, even if its variants such as different HTTP/HTTPS schemes or trailing slashes are bookmarked.
- Loading branch information
1 parent
90f521d
commit aaea291
Showing
8 changed files
with
290 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// BookmarkUrlExtension.swift | ||
// | ||
// Copyright © 2024 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. | ||
// | ||
|
||
extension URL { | ||
|
||
// Generates all possible URL variants for bookmark comparison, including http/https and optional trailing slashes. | ||
// Variants are only created for URLs with http/https schemes, and trailing slashes are added only if there are no query or fragment components. | ||
func bookmarkButtonUrlVariants() -> [URL] { | ||
var baseUrlString = self.absoluteString | ||
|
||
guard let scheme = self.scheme.map(NavigationalScheme.init), | ||
scheme.isHypertextScheme else { | ||
return [self] | ||
} | ||
|
||
baseUrlString = baseUrlString.replacingOccurrences(of: "\(scheme.separated())", with: "") | ||
|
||
// Generate variants without trailing slash | ||
let withoutTrailingSlash = baseUrlString.trimmingCharacters(in: CharacterSet(charactersIn: "/")) | ||
|
||
// Only append trailing slash if there are no query or fragment components | ||
let shouldAddTrailingSlash = self.query == nil && self.fragment == nil | ||
let withTrailingSlash = shouldAddTrailingSlash ? withoutTrailingSlash + "/" : withoutTrailingSlash | ||
|
||
// Create variants | ||
let httpVariant = NavigationalScheme.http.separated() + withoutTrailingSlash | ||
let httpsVariant = NavigationalScheme.https.separated() + withoutTrailingSlash | ||
let httpVariantWithSlash = NavigationalScheme.http.separated() + withTrailingSlash | ||
let httpsVariantWithSlash = NavigationalScheme.https.separated() + withTrailingSlash | ||
let variants: [URL?] = [ | ||
self, | ||
URL(string: httpVariant), | ||
URL(string: httpsVariant), | ||
shouldAddTrailingSlash ? URL(string: httpVariantWithSlash) : nil, | ||
shouldAddTrailingSlash ? URL(string: httpsVariantWithSlash) : nil | ||
] | ||
|
||
var seen = Set<String>() | ||
return variants.compactMap { variant in | ||
guard let url = variant else { return nil } | ||
let normalizedUrl = url.absoluteString.lowercased() | ||
if seen.contains(normalizedUrl) { | ||
return nil // Skip if already added | ||
} | ||
seen.insert(normalizedUrl) | ||
return url | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
UnitTests/Bookmarks/Model/BookmarkUrlExtensionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// BookmarkUrlExtensionTests.swift | ||
// | ||
// Copyright © 2021 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 Combine | ||
import Foundation | ||
|
||
import XCTest | ||
@testable import DuckDuckGo_Privacy_Browser | ||
final class BookmarkUrlExtensionTests: XCTestCase { | ||
|
||
func testWhenUrlIsHttpWithTrailingSlash_ThenCorrectVariantsAreGenerated() { | ||
let originalURL = URL(string: "http://example.com/")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 4) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com/")!)) | ||
} | ||
|
||
func testWhenUrlIsHttpsWithoutTrailingSlash_ThenCorrectVariantsAreGenerated() { | ||
let originalURL = URL(string: "https://example.com")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 4) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com/")!)) | ||
} | ||
|
||
func testWhenUrlIsHttpWithoutTrailingSlash_ThenCorrectVariantsAreGenerated() { | ||
let originalURL = URL(string: "http://example.com")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 4) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com/")!)) | ||
} | ||
|
||
func testWhenUrlIsHttpsWithTrailingSlash_ThenCorrectVariantsAreGenerated() { | ||
let originalURL = URL(string: "https://example.com/")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 4) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/")!)) | ||
} | ||
|
||
func testWhenUrlHasNoScheme_ThenNoVariantsGenerated() { | ||
let originalURL = URL(string: "example.com")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 1) | ||
XCTAssertEqual(variants[0], originalURL) | ||
} | ||
|
||
func testWhenUrlHasNonHttpOrHttpsScheme_ThenOnlyOriginalUrlIsReturned() { | ||
let originalURL = URL(string: "file:///Users/example/file.txt")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 1) | ||
XCTAssertEqual(variants[0], originalURL) | ||
} | ||
|
||
func testWhenUrlHasQueryParameters_ThenVariantsAreGeneratedWithoutTrailingSlashes() { | ||
let originalURL = URL(string: "https://example.com/path/to/resource?query=value")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 2) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/path/to/resource?query=value")!)) | ||
} | ||
|
||
func testWhenUrlHasNoQueryParameters_ThenVariantsIncludeTrailingSlashes() { | ||
let originalURL = URL(string: "https://example.com/path/to/resource")! | ||
|
||
let variants = originalURL.bookmarkButtonUrlVariants() | ||
|
||
XCTAssertEqual(variants.count, 4) | ||
XCTAssertEqual(variants[0], originalURL) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/path/to/resource")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "http://example.com/path/to/resource/")!)) | ||
XCTAssertTrue(variants.contains(URL(string: "https://example.com/path/to/resource/")!)) | ||
} | ||
} |
Oops, something went wrong.