Skip to content

Compatibility path for apps looping on URL.deletingLastPathComponent().standardized #1114

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
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
16 changes: 14 additions & 2 deletions Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,13 @@ public struct URL: Equatable, Sendable, Hashable {
/// then this function will return the URL unchanged.
public func deletingLastPathComponent() -> URL {
#if FOUNDATION_FRAMEWORK
guard foundation_swift_url_enabled() else {
/// Compatibility path for apps that loop on:
/// `url = url.deletingPathComponent().standardized` until `url.path.isEmpty`.
///
/// This used to work due to a combination of bugs where:
/// `URL("/").deletingLastPathComponent == URL("/../")`
/// `URL("/../").standardized == URL("")`
guard foundation_swift_url_enabled(), !Self.compatibility4 else {
// This is a slight behavior change from NSURL, but better than returning "http://www.example.com../".
guard !path.isEmpty, let result = _url.deletingLastPathComponent.map({ URL(reference: $0 as NSURL) }) else { return self }
return result
Expand Down Expand Up @@ -1837,7 +1843,13 @@ public struct URL: Equatable, Sendable, Hashable {
/// - note: This method does not consult the file system.
public var standardized: URL {
#if FOUNDATION_FRAMEWORK
guard foundation_swift_url_enabled() else {
/// Compatibility path for apps that loop on:
/// `url = url.deletingPathComponent().standardized` until `url.path.isEmpty`.
///
/// This used to work due to a combination of bugs where:
/// `URL("/").deletingLastPathComponent == URL("/../")`
/// `URL("/../").standardized == URL("")`
guard foundation_swift_url_enabled(), !Self.compatibility4 else {
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
guard let result = _url.standardized.map({ URL(reference: $0 as NSURL) }) else { return self }
return result
Expand Down