From d482ffbe4c638734a49b5b25f67711cea26bf718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Buczek?= Date: Fri, 18 Oct 2019 15:17:28 +0200 Subject: [PATCH] Ref #1533: Don't hide rest of url, hide `www.` subdomain only. (#1731) --- Shared/Extensions/URLExtensions.swift | 8 ++++---- SharedTests/NSURLExtensionsTests.swift | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Shared/Extensions/URLExtensions.swift b/Shared/Extensions/URLExtensions.swift index dde81dfe4f9..23a626d8461 100644 --- a/Shared/Extensions/URLExtensions.swift +++ b/Shared/Extensions/URLExtensions.swift @@ -248,10 +248,10 @@ extension URL { * E.g., https://mobile.foo.com/bar/baz?noo=abc#123 => https://mobile.foo.com/ */ public func domainURL(stripWWWSubdomainOnly: Bool = false) -> URL { - if let normalized = self.normalizedHost(stripWWWSubdomainOnly) { - // Use URLComponents instead of URL since the former correctly preserves - // brackets for IPv6 hosts, whereas the latter escapes them. - var components = URLComponents() + // Use URLComponents instead of URL since the former correctly preserves + // brackets for IPv6 hosts, whereas the latter escapes them. + if let normalized = self.normalizedHost(stripWWWSubdomainOnly), + var components = URLComponents(url: self, resolvingAgainstBaseURL: false) { components.scheme = self.scheme components.port = self.port components.host = normalized diff --git a/SharedTests/NSURLExtensionsTests.swift b/SharedTests/NSURLExtensionsTests.swift index d3dd1bda1e4..85ed9d5456d 100644 --- a/SharedTests/NSURLExtensionsTests.swift +++ b/SharedTests/NSURLExtensionsTests.swift @@ -359,18 +359,18 @@ class NSURLExtensionsTests: XCTestCase { func testdomainURL() { let urls = [ - ("https://www.example.com/index.html", "https://example.com"), - ("https://mail.example.com/index.html", "https://mail.example.com"), - ("https://mail.example.co.uk/index.html", "https://mail.example.co.uk"), + ("https://www.example.com/index.html", "https://example.com/index.html"), + ("https://mail.example.com/index.html", "https://mail.example.com/index.html"), + ("https://mail.example.co.uk/index.html", "https://mail.example.co.uk/index.html"), ] urls.forEach { XCTAssertEqual(URL(string:$0.0)!.domainURL().absoluteString, $0.1) } } func testdomainURLStrippingOnlyWWW() { let urls = [ - ("https://www.example.com/index.html", "https://example.com"), - ("https://m.example.com/index.html", "https://m.example.com"), - ("https://mobile.example.co.uk/index.html", "https://mobile.example.co.uk"), + ("https://www.example.com/index.html", "https://example.com/index.html"), + ("https://m.example.com/index.html", "https://m.example.com/index.html"), + ("https://mobile.example.co.uk/index.html", "https://mobile.example.co.uk/index.html"), ] urls.forEach { XCTAssertEqual(URL(string:$0.0)!.domainURL(stripWWWSubdomainOnly: true).absoluteString, $0.1) } }