Match subdomains.

This commit is contained in:
Brent Simmons committed 2026-05-31 11:33:27 -07:00
1 parent 4bb6afac1f
commit dfcf79ebad
1 file changed
+6 -2
@@ -25,10 +25,11 @@ nonisolated public struct SpecialCase {
return false
}
/// Returns true if the URL’s host matches one of the supplied domain names.
/// Returns true if the URL’s host belongs to one of the supplied domains.
///
/// Unlike `urlStringContainSpecialCase`, this checks only the host component — not the path or query.
/// 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`.
/// Subdomains also match: `micro.blog` in `domains` matches a host of `gruber.micro.blog`.
/// 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 {
@@ -36,7 +37,10 @@ nonisolated public struct SpecialCase {
}
let normalizedHost = stringByStrippingWWWPrefix(host)
for domain in domains {
if normalizedHost == domain {
if domain.isEmpty {
continue
}
if normalizedHost == domain || normalizedHost.hasSuffix("." + domain) {
return true
}
}