1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 07:07:44 +00:00

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

@ -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&);
}