1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:57:35 +00:00

Browser: Be a little bit smarter about interpretering command line URLs

If the (optional) URL specified on the command line is an existing file
we now resolve its real path before turning into a URL. This makes
relative URLs inside the loaded document work correctly.

Also interpret all other specified URLs the same way we would if you
had typed them into the location bar.
This commit is contained in:
Andreas Kling 2020-06-12 21:29:27 +02:00
parent d54ace5f04
commit 68ae1de463
2 changed files with 33 additions and 17 deletions

View file

@ -49,19 +49,31 @@
#include <LibWeb/DOMTreeModel.h> #include <LibWeb/DOMTreeModel.h>
#include <LibWeb/Dump.h> #include <LibWeb/Dump.h>
#include <LibWeb/Frame/Frame.h> #include <LibWeb/Frame/Frame.h>
#include <LibWeb/PageView.h>
#include <LibWeb/Layout/LayoutBlock.h> #include <LibWeb/Layout/LayoutBlock.h>
#include <LibWeb/Layout/LayoutDocument.h> #include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/Layout/LayoutInline.h> #include <LibWeb/Layout/LayoutInline.h>
#include <LibWeb/Layout/LayoutNode.h> #include <LibWeb/Layout/LayoutNode.h>
#include <LibWeb/Parser/CSSParser.h>
#include <LibWeb/Loader/ResourceLoader.h> #include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/PageView.h>
#include <LibWeb/Parser/CSSParser.h>
namespace Browser { namespace Browser {
extern bool g_use_old_html_parser; extern bool g_use_old_html_parser;
extern String g_home_url; extern String g_home_url;
URL url_from_user_input(const String& input)
{
auto url = URL(input);
if (url.is_valid())
return url;
StringBuilder builder;
builder.append("http://");
builder.append(input);
return URL(builder.build());
}
Tab::Tab() Tab::Tab()
{ {
auto& widget = *this; auto& widget = *this;
@ -78,26 +90,30 @@ Tab::Tab()
update_actions(); update_actions();
TemporaryChange<bool> change(m_should_push_loads_to_history, false); TemporaryChange<bool> change(m_should_push_loads_to_history, false);
m_page_view->load(m_history.current()); m_page_view->load(m_history.current());
}, this); },
this);
m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
m_history.go_forward(); m_history.go_forward();
update_actions(); update_actions();
TemporaryChange<bool> change(m_should_push_loads_to_history, false); TemporaryChange<bool> change(m_should_push_loads_to_history, false);
m_page_view->load(m_history.current()); m_page_view->load(m_history.current());
}, this); },
this);
toolbar.add_action(*m_go_back_action); toolbar.add_action(*m_go_back_action);
toolbar.add_action(*m_go_forward_action); toolbar.add_action(*m_go_forward_action);
toolbar.add_action(GUI::CommonActions::make_go_home_action([this](auto&) { toolbar.add_action(GUI::CommonActions::make_go_home_action([this](auto&) {
m_page_view->load(g_home_url); m_page_view->load(g_home_url);
}, this)); },
this));
m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) {
TemporaryChange<bool> change(m_should_push_loads_to_history, false); TemporaryChange<bool> change(m_should_push_loads_to_history, false);
m_page_view->reload(); m_page_view->reload();
}, this); },
this);
toolbar.add_action(*m_reload_action); toolbar.add_action(*m_reload_action);
@ -106,15 +122,8 @@ Tab::Tab()
m_location_box->set_preferred_size(0, 22); m_location_box->set_preferred_size(0, 22);
m_location_box->on_return_pressed = [this] { m_location_box->on_return_pressed = [this] {
String location = m_location_box->text(); auto url = url_from_user_input(m_location_box->text());
if (!URL(location).is_valid()) { m_page_view->load(url);
StringBuilder builder;
builder.append("http://");
builder.append(location);
location = builder.build();
}
m_page_view->load(location);
m_page_view->set_focus(true); m_page_view->set_focus(true);
}; };

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/StringBuilder.h>
#include "BookmarksBarWidget.h" #include "BookmarksBarWidget.h"
#include "InspectorWidget.h" #include "InspectorWidget.h"
#include "Tab.h" #include "Tab.h"
@ -46,6 +47,7 @@ namespace Browser {
static const char* bookmarks_filename = "/home/anon/bookmarks.json"; static const char* bookmarks_filename = "/home/anon/bookmarks.json";
String g_home_url; String g_home_url;
bool g_use_old_html_parser = false; bool g_use_old_html_parser = false;
URL url_from_user_input(const String& input);
} }
@ -183,8 +185,13 @@ int main(int argc, char** argv)
}; };
URL first_url = Browser::g_home_url; URL first_url = Browser::g_home_url;
if (specified_url) if (specified_url) {
first_url = URL::create_with_url_or_path(specified_url); if (Core::File::exists(specified_url)) {
first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url));
} else {
first_url = Browser::url_from_user_input(specified_url);
}
}
window_actions.on_create_new_tab = [&] { window_actions.on_create_new_tab = [&] {
create_new_tab(Browser::g_home_url, true); create_new_tab(Browser::g_home_url, true);