Ladybird+LibWeb: Use old error.html template for navigation errors again

This commit is contained in:
Bastiaan van der Plaat 2023-09-21 17:55:14 +02:00 committed by Andrew Kaster
parent 8f2319e966
commit 04ee15a5ad
7 changed files with 38 additions and 6 deletions

View file

@ -19,6 +19,5 @@
<img src="@resource_directory_url@/icons/32x32/msgbox-warning.png" alt="Warning" width="24" height="24">
<h1>Failed to load @failed_url@</h1>
</header>
<p>Error: @error@</p>
</body>
</html>

View file

@ -71,6 +71,7 @@ ErrorOr<int> service_main(int ipc_socket, int fd_passing_socket)
Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
Web::set_resource_directory_url(TRY(String::formatted("file://{}/res", s_serenity_resource_root)));
Web::set_error_page_url(TRY(String::formatted("file://{}/res/html/error.html", s_serenity_resource_root)));
Web::set_directory_page_url(TRY(String::formatted("file://{}/res/html/directory.html", s_serenity_resource_root)));
TRY(Web::Bindings::initialize_main_thread_vm());

View file

@ -97,6 +97,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
Web::set_resource_directory_url(TRY(String::formatted("file://{}/res", s_serenity_resource_root)));
Web::set_error_page_url(TRY(String::formatted("file://{}/res/html/error.html", s_serenity_resource_root)));
Web::set_directory_page_url(TRY(String::formatted("file://{}/res/html/directory.html", s_serenity_resource_root)));
TRY(Web::Bindings::initialize_main_thread_vm());

View file

@ -344,7 +344,8 @@ JS::GCPtr<DOM::Document> create_document_for_inline_content(JS::GCPtr<HTML::Navi
// 6. Either associate document with a custom rendering that is not rendered using the normal Document rendering rules, or mutate document until it represents the content the
// user agent wants to render.
auto parser = HTML::HTMLParser::create(document, content_html, "utf-8");
parser->run(AK::URL("about:error"));
document->set_url(AK::URL("about:error"));
parser->run();
// 7. Return document.
return document;

View file

@ -27,6 +27,7 @@
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Loader/GeneratedPagesLoader.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/XHR/FormData.h>
@ -998,9 +999,8 @@ WebIDL::ExceptionOr<void> Navigable::populate_session_history_entry_document(
if (failure) {
// 1. Set entry's document state's document to the result of creating a document for inline content that doesn't have a DOM, given navigable, null, and navTimingType.
// The inline content should indicate to the user the sort of error that occurred.
// FIXME: Use SourceGenerator to produce error page from file:///res/html/error.html
// and display actual error from fetch response.
auto error_html = String::formatted("<h1>Failed to load {}</h1>"sv, entry->url).release_value_but_fixme_should_propagate_errors();
// FIXME: Add error message to generated error page
auto error_html = load_error_page(entry->url).release_value_but_fixme_should_propagate_errors();
entry->document_state->set_document(create_document_for_inline_content(this, navigation_id, error_html));
// 2. Set entry's document state's document's salvageable to false.

View file

@ -26,6 +26,18 @@ void set_resource_directory_url(String resource_directory_url)
s_resource_directory_url = resource_directory_url;
}
static String s_error_page_url = "file:///res/html/error.html"_string;
String error_page_url()
{
return s_error_page_url;
}
void set_error_page_url(String error_page_url)
{
s_error_page_url = error_page_url;
}
static String s_directory_page_url = "file:///res/html/directory.html"_string;
String directory_page_url()
@ -38,6 +50,20 @@ void set_directory_page_url(String directory_page_url)
s_directory_page_url = directory_page_url;
}
ErrorOr<String> load_error_page(AK::URL const& url)
{
// Generate HTML error page from error template file
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
auto template_path = AK::URL::create_with_url_or_path(error_page_url().to_deprecated_string()).serialize_path();
auto template_file = TRY(Core::File::open(template_path, Core::File::OpenMode::Read));
auto template_contents = TRY(template_file->read_until_eof());
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("resource_directory_url", resource_directory_url());
generator.set("failed_url", url.to_deprecated_string());
generator.append(template_contents);
return TRY(String::from_utf8(generator.as_string_view()));
}
ErrorOr<String> load_file_directory_page(LoadRequest const& request)
{

View file

@ -6,16 +6,20 @@
#pragma once
#include <LibWeb/Loader/Resource.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Loader/Resource.h>
namespace Web {
String resource_directory_url();
void set_resource_directory_url(String);
String error_page_url();
void set_error_page_url(String);
String directory_page_url();
void set_directory_page_url(String);
ErrorOr<String> load_error_page(AK::URL const&);
ErrorOr<String> load_file_directory_page(LoadRequest const&);
}