Skip to content

(142076445) Allow URL.standardized to return an empty string URL #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ public struct URL: Equatable, Sendable, Hashable {
var components = URLComponents(parseInfo: _parseInfo)
let newPath = components.percentEncodedPath.removingDotSegments
components.percentEncodedPath = newPath
return components.url(relativeTo: baseURL)!
return components.url(relativeTo: baseURL) ?? self
}

/// Standardizes the path of a file URL by removing dot segments.
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/URL/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public struct URLComponents: Hashable, Equatable, Sendable {
return CFURLCreateWithString(kCFAllocatorDefault, string as CFString, nil) as URL?
}
#endif
return URL(string: string)
return URL(string: string, relativeTo: nil)
}

/// Returns a URL created from the URLComponents relative to a base URL.
Expand Down
6 changes: 6 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,12 @@ final class URLTests : XCTestCase {
XCTAssertEqual(comp.path, "/my\u{0}path")
}

func testURLStandardizedEmptyString() {
let url = URL(string: "../../../")!
let standardized = url.standardized
XCTAssertTrue(standardized.path().isEmpty)
}

#if FOUNDATION_FRAMEWORK
func testURLComponentsBridging() {
var nsURLComponents = NSURLComponents(
Expand Down