diff --git a/Userland/Libraries/LibWebView/CMakeLists.txt b/Userland/Libraries/LibWebView/CMakeLists.txt index 49d847cee64..2bea093f896 100644 --- a/Userland/Libraries/LibWebView/CMakeLists.txt +++ b/Userland/Libraries/LibWebView/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES DOMTreeModel.cpp History.cpp RequestServerAdapter.cpp + SearchEngine.cpp SourceHighlighter.cpp StylePropertiesModel.cpp URL.cpp diff --git a/Userland/Libraries/LibWebView/Forward.h b/Userland/Libraries/LibWebView/Forward.h index 44e88632c8c..062237509c4 100644 --- a/Userland/Libraries/LibWebView/Forward.h +++ b/Userland/Libraries/LibWebView/Forward.h @@ -19,6 +19,7 @@ class ViewImplementation; class WebContentClient; struct CookieStorageKey; +struct SearchEngine; } diff --git a/Userland/Libraries/LibWebView/SearchEngine.cpp b/Userland/Libraries/LibWebView/SearchEngine.cpp new file mode 100644 index 00000000000..34ebb0ede49 --- /dev/null +++ b/Userland/Libraries/LibWebView/SearchEngine.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace WebView { + +static constexpr auto builtin_search_engines = Array { + SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv }, + SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv }, + SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv }, + SearchEngine { "GitHub"sv, "https://github.com/search?q={}"sv }, + SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv }, + SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv }, + SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv }, + SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv }, +}; + +ReadonlySpan search_engines() +{ + return builtin_search_engines; +} + +SearchEngine const& default_search_engine() +{ + static auto default_engine = find_search_engine("Google"sv); + VERIFY(default_engine.has_value()); + + return *default_engine; +} + +Optional find_search_engine(StringView name) +{ + auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(), + [&](auto const& engine) { + return engine.name == name; + }); + + if (it == builtin_search_engines.end()) + return {}; + + return *it; +} + +} diff --git a/Userland/Libraries/LibWebView/SearchEngine.h b/Userland/Libraries/LibWebView/SearchEngine.h new file mode 100644 index 00000000000..0a6ab6a5f0c --- /dev/null +++ b/Userland/Libraries/LibWebView/SearchEngine.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace WebView { + +struct SearchEngine { + StringView name; + StringView query_url; +}; + +ReadonlySpan search_engines(); +SearchEngine const& default_search_engine(); +Optional find_search_engine(StringView name); + +}