From ea5da07a2d0ba4bdc9f68668c08cb68dfb44a0d0 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 29 Sep 2025 14:08:10 -0700 Subject: [PATCH] Rename URL.reparingIfRequired to URL.encodeSpacesIfNeeded, which is more explicit about what it does. Slightly edit the code and comment. --- Shared/Extensions/ArticleUtilities.swift | 6 +++--- Shared/Extensions/URL-Extensions.swift | 20 +++++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Shared/Extensions/ArticleUtilities.swift b/Shared/Extensions/ArticleUtilities.swift index 0b52c15d8..1a526f5e0 100644 --- a/Shared/Extensions/ArticleUtilities.swift +++ b/Shared/Extensions/ArticleUtilities.swift @@ -47,15 +47,15 @@ extension Article { } var url: URL? { - return URL.reparingIfRequired(rawLink) + return URL.encodeSpacesIfNeeded(rawLink) } var externalURL: URL? { - return URL.reparingIfRequired(rawExternalLink) + return URL.encodeSpacesIfNeeded(rawExternalLink) } var imageURL: URL? { - return URL.reparingIfRequired(rawImageLink) + return URL.encodeSpacesIfNeeded(rawImageLink) } var link: String? { diff --git a/Shared/Extensions/URL-Extensions.swift b/Shared/Extensions/URL-Extensions.swift index ded475910..8676a4b29 100644 --- a/Shared/Extensions/URL-Extensions.swift +++ b/Shared/Extensions/URL-Extensions.swift @@ -35,16 +35,14 @@ extension URL { return value } - - static func reparingIfRequired(_ link: String?) -> URL? { - // If required, we replace any space characters to handle malformed links that are otherwise percent - // encoded but contain spaces. For performance reasons, only try this if initial URL init fails. - guard let link = link, !link.isEmpty else { return nil } - if let url = URL(string: link) { - return url - } else { - return URL(string: link.replacingOccurrences(of: " ", with: "%20")) - } - } + /// Percent-encode spaces in links that may contain spaces but are otherwise already percent-encoded. + /// + /// For performance reasons, try this only if initial URL init fails. + static func encodeSpacesIfNeeded(_ link: String?) -> URL? { + guard let link, !link.isEmpty else { + return nil + } + return URL(string: link.replacingOccurrences(of: " ", with: "%20")) + } }