Allow tg:// links in text url entities

This commit is contained in:
Ilya Laktyushin 2019-09-20 23:50:21 +03:00
parent e26eedc664
commit 4408dc8f5a
2 changed files with 9 additions and 7 deletions

View File

@ -406,7 +406,7 @@ func chatTextLinkEditController(sharedContext: SharedAccountContext, account: Ac
return return
} }
let updatedLink = explicitUrl(contentNode.link) let updatedLink = explicitUrl(contentNode.link)
if !updatedLink.isEmpty && isValidUrl(updatedLink) { if !updatedLink.isEmpty && isValidUrl(updatedLink, validSchemes: ["http": true, "https": true, "tg": false, "ton": false]) {
dismissImpl?(true) dismissImpl?(true)
apply(updatedLink) apply(updatedLink)
} else { } else {

View File

@ -34,12 +34,14 @@ public extension CharacterSet {
}() }()
} }
public func isValidUrl(_ url: String) -> Bool { public func isValidUrl(_ url: String, validSchemes: [String: Bool] = ["http": true, "https": true]) -> Bool {
if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: escapedUrl), ["http", "https"].contains(url.scheme), let host = url.host, host.contains(".") && url.user == nil { if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: escapedUrl), let scheme = url.scheme, let requiresTopLevelDomain = validSchemes[scheme], let host = url.host, (!requiresTopLevelDomain || host.contains(".")) && url.user == nil {
let components = host.components(separatedBy: ".") if requiresTopLevelDomain {
let domain = (components.first ?? "") let components = host.components(separatedBy: ".")
if domain.isEmpty { let domain = (components.first ?? "")
return false if domain.isEmpty {
return false
}
} }
return true return true
} else { } else {