Repository Public
1import Foundation23/// A search provider. `template` contains `%s` where the percent-encoded query goes.4struct SearchEngine: Codable, Equatable, Identifiable, Sendable {5 var id: String6 var name: String7 var template: String8 /// Typed prefix that routes a query to this engine, e.g. `w swift` → Wikipedia.9 var shortcut: String1011 func searchURL(for query: String) -> URL? {12 guard let encoded = query.addingPercentEncoding(withAllowedCharacters: Self.queryAllowed) else { return nil }13 return URL(string: template.replacingOccurrences(of: "%s", with: encoded))14 }1516 /// A template is usable only if it is an http(s) URL containing the `%s` placeholder.17 var isValid: Bool {18 guard template.contains("%s"),19 let url = URL(string: template.replacingOccurrences(of: "%s", with: "x")),20 let scheme = url.scheme?.lowercased(), scheme == "https" || scheme == "http",21 url.host != nil else { return false }22 return !name.trimmingCharacters(in: .whitespaces).isEmpty23 }2425 private static let queryAllowed: CharacterSet = {26 var set = CharacterSet.alphanumerics27 set.insert(charactersIn: "-._~")28 return set29 }()3031 /// SP Search: results are fetched by the SP Browser backend and drawn natively in the tab.32 static let spSearch = SearchEngine(id: "spsearch", name: "SP Search", template: SPSearchURL.engineTemplate, shortcut: "s")3334 var isNative: Bool { id == SearchEngine.spSearch.id }3536 static let builtIn: [SearchEngine] = [37 spSearch,38 SearchEngine(id: "duckduckgo", name: "DuckDuckGo", template: "https://duckduckgo.com/?q=%s", shortcut: "d"),39 SearchEngine(id: "google", name: "Google", template: "https://www.google.com/search?q=%s", shortcut: "g"),40 SearchEngine(id: "bing", name: "Bing", template: "https://www.bing.com/search?q=%s", shortcut: "b"),41 SearchEngine(id: "brave", name: "Brave Search", template: "https://search.brave.com/search?q=%s", shortcut: "br"),42 SearchEngine(id: "startpage", name: "Startpage", template: "https://www.startpage.com/sp/search?query=%s", shortcut: "sp"),43 SearchEngine(id: "ecosia", name: "Ecosia", template: "https://www.ecosia.org/search?q=%s", shortcut: "e"),44 SearchEngine(id: "wikipedia", name: "Wikipedia", template: "https://www.wikipedia.org/w/index.php?search=%s", shortcut: "w"),45 SearchEngine(id: "github", name: "GitHub", template: "https://github.com/search?q=%s", shortcut: "gh"),46 SearchEngine(id: "mdn", name: "MDN Web Docs", template: "https://developer.mozilla.org/search?q=%s", shortcut: "mdn"),47 ]4849 static let fallback = builtIn[0]50}51