From 5bb5967259545a6df8d23cd71067a747d4f33fe8 Mon Sep 17 00:00:00 2001 From: Rok Povsic Date: Sat, 19 Mar 2022 12:10:38 +0100 Subject: [PATCH] Browser: Append .com when pressing CTRL+Enter in the URL text editor This is the behavior in Firefox / Chrome. --- Userland/Applications/Browser/Tab.cpp | 37 ++++++++++++++++++++++----- Userland/Applications/Browser/Tab.h | 7 +++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index f4ea417e8f..e8445e7343 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -151,13 +151,15 @@ Tab::Tab(BrowserWindow& window) m_location_box->set_placeholder("Address"); m_location_box->on_return_pressed = [this] { - if (m_location_box->text().starts_with('?') && g_search_engine.is_empty()) { - GUI::MessageBox::show(&this->window(), "Select a search engine in the Settings menu before searching.", "No search engine selected", GUI::MessageBox::Type::Information); - return; - } + auto url = url_from_location_bar(); + if (url.has_value()) + load(url.release_value()); + }; - auto url = url_from_user_input(m_location_box->text()); - load(url); + m_location_box->on_ctrl_return_pressed = [this] { + auto url = url_from_location_bar(MayAppendTLD::Yes); + if (url.has_value()) + load(url.release_value()); }; m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) { @@ -393,6 +395,29 @@ Tab::Tab(BrowserWindow& window) }; } +Optional Tab::url_from_location_bar(MayAppendTLD may_append_tld) +{ + if (m_location_box->text().starts_with('?') && g_search_engine.is_empty()) { + GUI::MessageBox::show(&this->window(), "Select a search engine in the Settings menu before searching.", "No search engine selected", GUI::MessageBox::Type::Information); + return {}; + } + + String text = m_location_box->text(); + + StringBuilder builder; + builder.append(text); + if (may_append_tld == MayAppendTLD::Yes) { + // FIXME: Expand the list of top level domains. + if (!(text.ends_with(".com") || text.ends_with(".net") || text.ends_with(".org"))) { + builder.append(".com"); + } + } + String final_text = builder.to_string(); + + auto url = url_from_user_input(final_text); + return url; +} + void Tab::load(const URL& url, LoadType load_type) { m_is_history_navigation = (load_type == LoadType::HistoryNavigation); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 265266a730..7e01c61765 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -94,6 +94,13 @@ private: void view_source(const URL& url, const String& source); void update_status(Optional text_override = {}, i32 count_waiting = 0); + enum class MayAppendTLD { + No, + Yes + }; + + Optional url_from_location_bar(MayAppendTLD = MayAppendTLD::No); + History m_history; RefPtr m_web_content_view;