diff --git a/Sources/FoundationEssentials/URL/URL.swift b/Sources/FoundationEssentials/URL/URL.swift index 1b117efea..44c367854 100644 --- a/Sources/FoundationEssentials/URL/URL.swift +++ b/Sources/FoundationEssentials/URL/URL.swift @@ -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. diff --git a/Sources/FoundationEssentials/URL/URLComponents.swift b/Sources/FoundationEssentials/URL/URLComponents.swift index f5ce53ae7..6eb3a6680 100644 --- a/Sources/FoundationEssentials/URL/URLComponents.swift +++ b/Sources/FoundationEssentials/URL/URLComponents.swift @@ -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. diff --git a/Tests/FoundationEssentialsTests/URLTests.swift b/Tests/FoundationEssentialsTests/URLTests.swift index ed3ef5a36..1017254ac 100644 --- a/Tests/FoundationEssentialsTests/URLTests.swift +++ b/Tests/FoundationEssentialsTests/URLTests.swift @@ -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(