Don't re-encode already-encoded mailto links — the subject line showed literal %20. Fixes #3719. Fixes #4380.

This commit is contained in:
Brent Simmons committed 2026-07-27 09:59:33 -07:00
1 parent a080925cb9
commit dbea20974c
2 files changed
+51 -1

No files matched your search

@@ -14,7 +14,7 @@ public extension URL {
guard scheme == "mailto" else {
return nil
}
guard let urlString = absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
guard let urlString = absoluteString.addingPercentEncoding(withAllowedCharacters: .mailtoAllowed) else {
return nil
}
return URL(string: urlString)
@@ -30,3 +30,9 @@ public extension URL {
return URL(string: link.replacingOccurrences(of: " ", with: "%20"))
}
}
private extension CharacterSet {
// Includes % so existing percent escapes don't get encoded a second time.
static let mailtoAllowed = CharacterSet.urlQueryAllowed.union(CharacterSet(charactersIn: "%"))
}
@@ -0,0 +1,44 @@
//
// URL+RSCoreTests.swift
// RSCore
//
// Created by Brent Simmons on 7/27/26.
//
import Foundation
import Testing
import RSCore
struct URLRSCoreTests {
// An already-encoded mailto URL must pass through unchanged — re-encoding
// turned %20 into %2520, showing literal "%20" in Mail.
// <https://github.com/Ranchero-Software/NetNewsWire/issues/3719>
// <https://github.com/Ranchero-Software/NetNewsWire/issues/4380>
@Test func percentEncodedEmailAddressDoesNotDoubleEncode() throws {
let url = try #require(URL(string: "mailto:test@example.com?subject=Hello%20World"))
#expect(url.percentEncodedEmailAddress?.absoluteString == "mailto:test@example.com?subject=Hello%20World")
}
@Test func percentEncodedEmailAddressPreservesMultipleEscapes() throws {
let url = try #require(URL(string: "mailto:test@example.com?subject=One%20Two%20Three&body=Hi%2C%20there"))
#expect(url.percentEncodedEmailAddress?.absoluteString == "mailto:test@example.com?subject=One%20Two%20Three&body=Hi%2C%20there")
}
@Test func percentEncodedEmailAddressIsIdempotent() throws {
let url = try #require(URL(string: "mailto:test@example.com?subject=Hello%20World"))
let once = url.percentEncodedEmailAddress
let twice = once?.percentEncodedEmailAddress
#expect(once == twice)
}
@Test func percentEncodedEmailAddressRequiresMailto() throws {
let url = try #require(URL(string: "https://example.com/"))
#expect(url.percentEncodedEmailAddress == nil)
}
@Test func percentEncodedEmailAddressPlainAddress() throws {
let url = try #require(URL(string: "mailto:test@example.com"))
#expect(url.percentEncodedEmailAddress?.absoluteString == "mailto:test@example.com")
}
}