From 62c73d2c959e1bccbfa36638db3d65aeff9c9348 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 27 Apr 2026 20:31:49 -0700 Subject: [PATCH] Match example.com and www.example.com in urlStringMatchesDomain. --- Modules/RSWeb/Sources/RSWeb/SpecialCases.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Modules/RSWeb/Sources/RSWeb/SpecialCases.swift b/Modules/RSWeb/Sources/RSWeb/SpecialCases.swift index ad7e1662b..a8a3181fa 100644 --- a/Modules/RSWeb/Sources/RSWeb/SpecialCases.swift +++ b/Modules/RSWeb/Sources/RSWeb/SpecialCases.swift @@ -25,20 +25,32 @@ nonisolated public struct SpecialCase { return false } - /// Returns true if the URL’s host matches one of the supplied domain names exactly. + /// Returns true if the URL’s host matches one of the supplied domain names. + /// /// Unlike `urlStringContainSpecialCase`, this checks only the host component — not the path or query. - /// The supplied `domains` are assumed to already be lowercased. + /// A leading `www.` on the URL’s host is treated as optional, so `example.com` in `domains` matches a host of either `example.com` or `www.example.com`. + /// The supplied `domains` are assumed to already be lowercased and to have any leading `www.` stripped. public static func urlStringMatchesDomain(_ urlString: String, _ domains: [String]) -> Bool { guard let url = URL(string: urlString), let host = url.host()?.lowercased(with: localeForLowercasing) else { return false } + let normalizedHost = stringByStrippingWWWPrefix(host) for domain in domains { - if host == domain { + if normalizedHost == domain { return true } } return false } + + private static let wwwPrefix = "www." + + private static func stringByStrippingWWWPrefix(_ host: String) -> String { + if host.hasPrefix(wwwPrefix) { + return String(host.dropFirst(wwwPrefix.count)) + } + return host + } } nonisolated extension URL {