From f62efe1ed68179d45397a4188d5ec9f8d2ee7239 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Fri, 2 Aug 2024 01:44:08 +0200 Subject: [PATCH] Various fixes --- .../Sources/BrowserAddressBarComponent.swift | 2 +- submodules/BrowserUI/Sources/BrowserScreen.swift | 10 ++++++++++ submodules/BrowserUI/Sources/Utils.swift | 12 +++++++----- .../Sources/ChatTextLinkEditController.swift | 2 +- submodules/TelegramUI/Sources/OpenChatMessage.swift | 6 +++--- .../TelegramUI/Sources/SharedAccountContext.swift | 8 ++++---- submodules/UrlEscaping/Sources/UrlEscaping.swift | 8 ++++++-- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/submodules/BrowserUI/Sources/BrowserAddressBarComponent.swift b/submodules/BrowserUI/Sources/BrowserAddressBarComponent.swift index 1c51f026da..da83dddb91 100644 --- a/submodules/BrowserUI/Sources/BrowserAddressBarComponent.swift +++ b/submodules/BrowserUI/Sources/BrowserAddressBarComponent.swift @@ -441,7 +441,7 @@ final class AddressBarContentComponent: Component { textField.addTarget(self, action: #selector(self.textFieldChanged(_:)), for: .editingChanged) } - let address = getDisplayUrl(self.component?.url ?? "") + let address = getDisplayUrl(self.component?.url ?? "", trim: false) if textField.text != address { textField.text = address self.clearIconView.isHidden = address.isEmpty diff --git a/submodules/BrowserUI/Sources/BrowserScreen.swift b/submodules/BrowserUI/Sources/BrowserScreen.swift index d5c664de3f..afd4755c0b 100644 --- a/submodules/BrowserUI/Sources/BrowserScreen.swift +++ b/submodules/BrowserUI/Sources/BrowserScreen.swift @@ -1390,6 +1390,16 @@ public class BrowserScreen: ViewController, MinimizableController { ] public init(context: AccountContext, subject: Subject, openPreviousOnClose: Bool = false) { + var subject = subject + if case let .webPage(url) = subject, let parsedUrl = URL(string: url) { + if parsedUrl.host?.hasSuffix(".ton") == true { + var urlComponents = URLComponents(string: url) + urlComponents?.scheme = "tonsite" + if let updatedUrl = urlComponents?.url?.absoluteString { + subject = .webPage(url: updatedUrl) + } + } + } self.context = context self.subject = subject self.openPreviousOnClose = openPreviousOnClose diff --git a/submodules/BrowserUI/Sources/Utils.swift b/submodules/BrowserUI/Sources/Utils.swift index 49e3280e92..71489f05a3 100644 --- a/submodules/BrowserUI/Sources/Utils.swift +++ b/submodules/BrowserUI/Sources/Utils.swift @@ -111,7 +111,7 @@ func getPrimaryUrl(message: Message) -> String? { private let asciiChars = CharacterSet(charactersIn: "a".unicodeScalars.first! ... "z".unicodeScalars.first!) -func getDisplayUrl(_ url: String, hostOnly: Bool = false) -> String { +func getDisplayUrl(_ url: String, hostOnly: Bool = false, trim: Bool = true) -> String { if hostOnly { var title = url if let parsedUrl = URL(string: url) { @@ -143,10 +143,12 @@ func getDisplayUrl(_ url: String, hostOnly: Bool = false) -> String { } } } - address = address.replacingOccurrences(of: "https://www.", with: "") - address = address.replacingOccurrences(of: "https://", with: "") - address = address.replacingOccurrences(of: "tonsite://", with: "") - address = address.trimmingCharacters(in: CharacterSet(charactersIn: "/")) + if trim { + address = address.replacingOccurrences(of: "https://www.", with: "") + address = address.replacingOccurrences(of: "https://", with: "") + address = address.replacingOccurrences(of: "tonsite://", with: "") + address = address.trimmingCharacters(in: CharacterSet(charactersIn: "/")) + } return address } } diff --git a/submodules/ChatTextLinkEditUI/Sources/ChatTextLinkEditController.swift b/submodules/ChatTextLinkEditUI/Sources/ChatTextLinkEditController.swift index 8ed99a1fb8..c802ca3928 100644 --- a/submodules/ChatTextLinkEditUI/Sources/ChatTextLinkEditController.swift +++ b/submodules/ChatTextLinkEditUI/Sources/ChatTextLinkEditController.swift @@ -433,7 +433,7 @@ public func chatTextLinkEditController(sharedContext: SharedAccountContext, upda return } let updatedLink = explicitUrl(contentNode.link) - if !updatedLink.isEmpty && isValidUrl(updatedLink, validSchemes: ["http": true, "https": true, "tg": false, "ton": false]) { + if !updatedLink.isEmpty && isValidUrl(updatedLink, validSchemes: ["http": true, "https": true, "tg": false, "ton": false, "tonsite": true]) { dismissImpl?(true) apply(updatedLink) } else if allowEmpty && contentNode.link.isEmpty { diff --git a/submodules/TelegramUI/Sources/OpenChatMessage.swift b/submodules/TelegramUI/Sources/OpenChatMessage.swift index dbab32a943..cade6b3377 100644 --- a/submodules/TelegramUI/Sources/OpenChatMessage.swift +++ b/submodules/TelegramUI/Sources/OpenChatMessage.swift @@ -381,15 +381,15 @@ func openChatMessageImpl(_ params: OpenChatMessageParams) -> Bool { return false } -func makeInstantPageControllerImpl(context: AccountContext, message: Message, sourcePeerType: MediaAutoDownloadPeerType?, navigationController: NavigationController) -> ViewController? { +func makeInstantPageControllerImpl(context: AccountContext, message: Message, sourcePeerType: MediaAutoDownloadPeerType?) -> ViewController? { guard let (webpage, anchor) = instantPageAndAnchor(message: message) else { return nil } let sourceLocation = InstantPageSourceLocation(userLocation: .peer(message.id.peerId), peerType: sourcePeerType ?? .channel) - return makeInstantPageControllerImpl(context: context, webPage: webpage, anchor: anchor, sourceLocation: sourceLocation, navigationController: navigationController) + return makeInstantPageControllerImpl(context: context, webPage: webpage, anchor: anchor, sourceLocation: sourceLocation) } -func makeInstantPageControllerImpl(context: AccountContext, webPage: TelegramMediaWebpage, anchor: String?, sourceLocation: InstantPageSourceLocation, navigationController: NavigationController) -> ViewController { +func makeInstantPageControllerImpl(context: AccountContext, webPage: TelegramMediaWebpage, anchor: String?, sourceLocation: InstantPageSourceLocation) -> ViewController { return BrowserScreen(context: context, subject: .instantPage(webPage: webPage, anchor: anchor, sourceLocation: sourceLocation)) } diff --git a/submodules/TelegramUI/Sources/SharedAccountContext.swift b/submodules/TelegramUI/Sources/SharedAccountContext.swift index a6731e4576..d040458dc2 100644 --- a/submodules/TelegramUI/Sources/SharedAccountContext.swift +++ b/submodules/TelegramUI/Sources/SharedAccountContext.swift @@ -1846,12 +1846,12 @@ public final class SharedAccountContextImpl: SharedAccountContext { }) } - public func makeInstantPageController(context: AccountContext, message: Message, sourcePeerType: MediaAutoDownloadPeerType?, navigationController: NavigationController) -> ViewController? { - return makeInstantPageControllerImpl(context: context, message: message, sourcePeerType: sourcePeerType, navigationController: navigationController) + public func makeInstantPageController(context: AccountContext, message: Message, sourcePeerType: MediaAutoDownloadPeerType?) -> ViewController? { + return makeInstantPageControllerImpl(context: context, message: message, sourcePeerType: sourcePeerType) } - public func makeInstantPageController(context: AccountContext, webPage: TelegramMediaWebpage, anchor: String?, sourceLocation: InstantPageSourceLocation, navigationController: NavigationController) -> ViewController { - return makeInstantPageControllerImpl(context: context, webPage: webPage, anchor: anchor, sourceLocation: sourceLocation, navigationController: navigationController) + public func makeInstantPageController(context: AccountContext, webPage: TelegramMediaWebpage, anchor: String?, sourceLocation: InstantPageSourceLocation) -> ViewController { + return makeInstantPageControllerImpl(context: context, webPage: webPage, anchor: anchor, sourceLocation: sourceLocation) } public func openChatWallpaper(context: AccountContext, message: Message, present: @escaping (ViewController, Any?) -> Void) { diff --git a/submodules/UrlEscaping/Sources/UrlEscaping.swift b/submodules/UrlEscaping/Sources/UrlEscaping.swift index 3bc674f9e4..bbf09a6488 100644 --- a/submodules/UrlEscaping/Sources/UrlEscaping.swift +++ b/submodules/UrlEscaping/Sources/UrlEscaping.swift @@ -39,8 +39,12 @@ public func isValidUrl(_ url: String, validSchemes: [String: Bool] = ["http": tr public func explicitUrl(_ url: String) -> String { var url = url - if !url.hasPrefix("http") && !url.hasPrefix("https") && url.range(of: "://") == nil { - url = "https://\(url)" + if !url.lowercased().hasPrefix("http:") && !url.lowercased().hasPrefix("https:") && !url.lowercased().hasPrefix("tonsite:") && url.range(of: "://") == nil { + if let parsedUrl = URL(string: "http://\(url)"), parsedUrl.host?.hasSuffix(".ton") == true { + url = "tonsite://\(url)" + } else { + url = "https://\(url)" + } } return url }