dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / webview / url.lucb
#==============================================================================================
#
# url - what the address field's text loads: search engines and sanitize_url
#
# DESCRIPTION:
# Port of Libraries/LibWebView/SearchEngine.cpp (lines 1-46) and the parts of
# Libraries/LibWebView/URL.cpp the address field uses (lines 19-80: sanitize_url and
# location_looks_like_url). Typed text that names an existing file loads it; text that
# parses as a URL of a supported scheme loads that URL; a bare host gets https://; and
# anything else (or a guessed host without a public suffix) becomes a search with the
# chosen engine, when there is one.
#
# Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
# Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
# Copyright (c) 2025, Manuel Zahariev <manuel@duck.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: SearchEngine ============================================================================
## Ported from Libraries/LibWebView/SearchEngine.h:17 WebView::SearchEngine: a name and a query
## URL whose `%s` the search text replaces.
pub struct WebViewSearchEngine:
pub let name: str
pub let query_url: str
## Ported from Libraries/LibWebView/SearchEngine.cpp:12 s_builtin_search_engines.
pub let webview_builtin_search_engines: WebViewSearchEngine[10] = [
WebViewSearchEngine(name = "Bing", query_url = "https://www.bing.com/search?q=%s"),
WebViewSearchEngine(name = "Brave", query_url = "https://search.brave.com/search?q=%s"),
WebViewSearchEngine(name = "DuckDuckGo", query_url = "https://duckduckgo.com/?q=%s"),
WebViewSearchEngine(name = "Ecosia", query_url = "https://ecosia.org/search?q=%s"),
WebViewSearchEngine(name = "Google", query_url = "https://www.google.com/search?q=%s"),
WebViewSearchEngine(name = "Kagi", query_url = "https://kagi.com/search?q=%s"),
WebViewSearchEngine(name = "Mojeek", query_url = "https://www.mojeek.com/search?q=%s"),
WebViewSearchEngine(name = "Startpage", query_url = "https://startpage.com/search?q=%s"),
WebViewSearchEngine(name = "Yahoo", query_url = "https://search.yahoo.com/search?p=%s"),
WebViewSearchEngine(name = "Yandex", query_url = "https://yandex.com/search/?text=%s"),
]
## The builtin engine called `name`, if there is one.
pub func webview_find_search_engine(name: str) -> WebViewSearchEngine?:
for engine in webview_builtin_search_engines:
if engine.name == name:
return engine
return none
## Ported from Libraries/LibWebView/SearchEngine.cpp:42 WebView::SearchEngine::format_search_query_for_navigation.
pub func webview_search_engine_format_search_query_for_navigation(this: const WebViewSearchEngine*, query: ak.StringView) -> ak.String:
let query_url = ak.string_from_str(this.query_url)
let encoded = web_url.percent_encode(query)
return ak.string_replace(&query_url, ak.sv("%s"), ak.string_bytes_as_string_view(&encoded), ak.ReplaceMode.all) else trap("MUST: String::replace")
# mark: sanitize_url ============================================================================
## Ported from Libraries/LibWebView/URL.h:15 WebView::AppendTLD.
pub enum WebViewAppendTld as u8:
no = 0
yes = 1
## The schemes the address field loads (sanitize_url's SUPPORTED_SCHEMES).
let webview_supported_schemes: str[6] = ["about", "data", "file", "http", "https", "resource"]
## https://datatracker.ietf.org/doc/html/rfc2606 (sanitize_url's RESERVED_TLDS).
let webview_reserved_tlds: str[4] = [".test", ".example", ".invalid", ".localhost"]
## sanitize_url's search_url_or_error lambda.
func webview_search_url_or_error(location: ak.StringView, search_engine: const WebViewSearchEngine*?) -> web_url.Url?:
let engine = search_engine else return none
let query = webview_search_engine_format_search_query_for_navigation(engine, location)
return web_url.parser_basic_parse(ak.string_bytes_as_string_view(&query))
## FileSystem::exists(location).
func webview_file_exists(location: str) -> bool:
let native = strings.copy(location) catch failure: return false
defer strings.release(native)
return files.exists((c.str)native)
## FileSystem::real_path(location), none when it fails.
func webview_real_path(location: str) -> ak.ByteString?:
let native = strings.copy(location) catch failure: return none
defer strings.release(native)
let real = files.canonical((c.str)native) catch failure: return none
defer real.release()
return ak.byte_string_from_str(real.value)
## Ported from Libraries/LibWebView/URL.cpp:19 WebView::sanitize_url.
pub func webview_sanitize_url(raw_location: ak.StringView, search_engine: const WebViewSearchEngine*? = none, append_tld: WebViewAppendTld = WebViewAppendTld.no) -> web_url.Url?:
let location = ak.string_view_trim_whitespace(&raw_location)
if webview_file_exists(ak.string_view_text(location)):
if let path = webview_real_path(ak.string_view_text(location)):
return web_url.create_with_file_scheme(path, ak.byte_string_empty(), ak.byte_string_empty())
return webview_search_url_or_error(location, search_engine)
var https_scheme_was_guessed = false
var url = web_url.create_with_url_or_path(ak.byte_string_from_string_view(location))
var needs_scheme = true
if let parsed = url:
needs_scheme = webview_url_scheme_is(&parsed, "localhost")
if needs_scheme:
var builder = ak.string_builder_new()
ak.string_builder_append(&builder, "https://")
ak.string_builder_append_string_view(&builder, location)
url = web_url.create_with_url_or_path(ak.byte_string_from_string_view(ak.string_builder_string_view(&builder)))
if url == none:
return webview_search_url_or_error(location, search_engine)
https_scheme_was_guessed = true
var result = url else trap("unreachable")
# FIXME: Add support for other schemes, e.g. "mailto:". Firefox and Chrome open mailto: locations.
var supported = false
for scheme in webview_supported_schemes:
if webview_url_scheme_is(&result, scheme):
supported = true
if not supported:
return webview_search_url_or_error(location, search_engine)
if let host = web_url.url_host(&result):
if web_url.host_is_domain(&host):
let domain = web_url.host_get_string(&host)
if ak.string_contains_u32(&domain, (u32)'"'):
return webview_search_url_or_error(location, search_engine)
# https://datatracker.ietf.org/doc/html/rfc2606
for tld in webview_reserved_tlds:
if ak.string_view_text(ak.string_bytes_as_string_view(&domain)).length > tld.length and ak.string_ends_with_bytes(&domain, ak.sv(tld)):
return result
let public_suffix = web_url.public_suffix_data_get_public_suffix(web_url.public_suffix_data_the(), ak.string_bytes_as_string_view(&domain))
var suffix_is_domain = true
if let suffix = public_suffix:
suffix_is_domain = ak.string_equals(&suffix, &domain)
if suffix_is_domain:
if append_tld == WebViewAppendTld.yes:
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&domain))
ak.string_builder_append(&builder, ".com")
var with_tld: web_url.Host = ---
web_url.host_construct_string(&with_tld, ak.string_builder_to_string(&builder) else trap("MUST: String::formatted"))
web_url.url_set_host(&result, with_tld)
elif https_scheme_was_guessed and not ak.string_eq_string_view(&domain, ak.sv("localhost")):
return webview_search_url_or_error(location, search_engine)
return result
## Ported from Libraries/LibWebView/URL.cpp:76 WebView::location_looks_like_url.
pub func webview_location_looks_like_url(location: ak.StringView, append_tld: WebViewAppendTld = WebViewAppendTld.no) -> bool:
return webview_sanitize_url(location, none, append_tld) != none
## `url.scheme() == scheme`.
func webview_url_scheme_is(url: const web_url.Url*, scheme: str) -> bool:
let found = web_url.url_scheme(url)
return ak.string_eq_string_view(&found, ak.sv(scheme))