mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 16:26:04 +00:00
Match example.com and www.example.com in urlStringMatchesDomain.
This commit is contained in:
1 parent
c509f2324a
commit
62c73d2c95
1 file changed
+15
-3
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user