diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index e12b88e3c43..f76ba77b46d 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -470,6 +470,8 @@ public: void make_active(); + void set_salvageable(bool value) { m_salvageable = value; }; + protected: virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; diff --git a/Userland/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Userland/Libraries/LibWeb/DOM/DocumentLoading.cpp index d3f9a95689c..d4f18928e44 100644 --- a/Userland/Libraries/LibWeb/DOM/DocumentLoading.cpp +++ b/Userland/Libraries/LibWeb/DOM/DocumentLoading.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -201,4 +202,100 @@ bool parse_document(DOM::Document& document, ByteBuffer const& data) return false; } +// https://html.spec.whatwg.org/multipage/browsing-the-web.html#loading-a-document +JS::GCPtr load_document(Optional navigation_params) +{ + VERIFY(navigation_params.has_value()); + + auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", *navigation_params).release_value_but_fixme_should_propagate_errors(); + + auto& realm = document->realm(); + + if (navigation_params->response->body().has_value()) { + auto process_body = [navigation_params, document](ByteBuffer bytes) { + if (!parse_document(*document, bytes)) { + // FIXME: Load html page with an error if parsing failed. + TODO(); + } + }; + + auto process_body_error = [](auto&) { + // FIXME: Load html page with an error if read of body failed. + TODO(); + }; + + navigation_params->response->body()->fully_read( + realm, + move(process_body), + move(process_body_error), + JS::NonnullGCPtr { realm.global_object() }) + .release_value_but_fixme_should_propagate_errors(); + } + + return document; +} + +// https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-ua-inline +JS::GCPtr create_document_for_inline_content(JS::GCPtr navigable, Optional navigation_id, StringView content_html) +{ + auto& vm = navigable->vm(); + + // 1. Let origin be a new opaque origin. + HTML::Origin origin {}; + + // 2. Let coop be a new cross-origin opener policy. + auto coop = HTML::CrossOriginOpenerPolicy {}; + + // 3. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with + // url: response's URL + // origin: origin + // cross-origin opener policy: coop + HTML::CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result { + .url = AK::URL("about:error"), // AD-HOC + .origin = origin, + .cross_origin_opener_policy = coop + }; + + // 4. Let navigationParams be a new navigation params with + // id: navigationId + // request: null + // response: a new response + // origin: origin + // policy container: a new policy container + // final sandboxing flag set: an empty set + // cross-origin opener policy: coop + // COOP enforcement result: coopEnforcementResult + // reserved environment: null + // navigable: navigable + // FIXME: navigation timing type: navTimingType + // FIXME: fetch controller: fetch controller + // FIXME: commit early hints: null + auto response = Fetch::Infrastructure::Response::create(vm); + response->url_list().append(AK::URL("about:error")); // AD-HOC: https://github.com/whatwg/html/issues/9122 + HTML::NavigationParams navigation_params { + .id = navigation_id, + .request = {}, + .response = *response, + .origin = move(origin), + .policy_container = HTML::PolicyContainer {}, + .final_sandboxing_flag_set = HTML::SandboxingFlagSet {}, + .cross_origin_opener_policy = move(coop), + .coop_enforcement_result = move(coop_enforcement_result), + .reserved_environment = {}, + .browsing_context = navigable->active_browsing_context(), + .navigable = navigable, + }; + + // 5. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams. + auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", navigation_params).release_value_but_fixme_should_propagate_errors(); + + // 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")); + + // 7. Return document. + return document; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/DocumentLoading.h b/Userland/Libraries/LibWeb/DOM/DocumentLoading.h index 0b330e1458a..aa87db2a21b 100644 --- a/Userland/Libraries/LibWeb/DOM/DocumentLoading.h +++ b/Userland/Libraries/LibWeb/DOM/DocumentLoading.h @@ -12,5 +12,7 @@ namespace Web { bool parse_document(DOM::Document& document, ByteBuffer const& data); +JS::GCPtr load_document(Optional navigation_params); +JS::GCPtr create_document_for_inline_content(JS::GCPtr navigable, Optional navigation_id, StringView content_html); } diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index 18a7a0286fb..789223cb349 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -7,9 +7,15 @@ #include #include +#include +#include +#include +#include +#include #include #include #include +#include #include #include #include @@ -218,6 +224,134 @@ Vector>& Navigable::get_session_history_en VERIFY_NOT_REACHED(); } +// https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document +WebIDL::ExceptionOr Navigable::populate_session_history_entry_document(JS::GCPtr entry, Optional navigation_params, Optional navigation_id, SourceSnapshotParams const& source_snapshot_params, Function completion_steps) +{ + // FIXME: 1. Assert: this is running in parallel. + + // 2. Assert: if navigationParams is non-null, then navigationParams's response is non-null. + if (navigation_params.has_value()) + VERIFY(navigation_params->response); + + // 3. Let currentBrowsingContext be navigable's active browsing context. + [[maybe_unused]] auto current_browsing_context = active_browsing_context(); + + // 4. Let documentResource be entry's document state's resource. + auto document_resource = entry->document_state->resource(); + + // 5. If navigationParams is null, then: + if (!navigation_params.has_value()) { + // 1. If documentResource is a string, then set navigationParams to the result + // of creating navigation params from a srcdoc resource given entry, navigable, + // targetSnapshotParams, navigationId, and navTimingType. + if (document_resource.has()) { + TODO(); + } + // 2. Otherwise, if both of the following are true: + // - entry's URL's scheme is a fetch scheme; and + // - documentResource is null, FIXME: or allowPOST is true and documentResource's request body is not failure + else if (Fetch::Infrastructure::is_fetch_scheme(entry->url.scheme()) && document_resource.has()) { + TODO(); + } + // FIXME: 3. Otherwise, if entry's URL's scheme is not a fetch scheme, then set navigationParams to a new non-fetch scheme navigation params, with + // initiator origin: entry's document state's initiator origin + else { + TODO(); + } + } + + // 6. Queue a global task on the navigation and traversal task source, given navigable's active window, to run these steps: + queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, entry, navigation_params, navigation_id, completion_steps = move(completion_steps)] { + // 1. If navigable's ongoing navigation no longer equals navigationId, then run completionSteps and return. + if (navigation_id.has_value() && (!ongoing_navigation().has() || ongoing_navigation().get() != *navigation_id)) { + completion_steps(); + return; + } + + // 2. Let failure be false. + auto failure = false; + + // FIXME: 3. If navigationParams is a non-fetch scheme navigation params, then set entry's document state's document to the result of running attempt to create a non-fetch + // scheme document given entry's URL, navigable, targetSnapshotParams's sandboxing flags, navigationId, navTimingType, sourceSnapshotParams's has transient + // activation, and navigationParams's initiator origin. + + // 4. Otherwise, if navigationParams is null, then set failure to true. + if (!navigation_params.has_value()) { + failure = true; + } + + // FIXME: 5. Otherwise, if the result of should navigation response to navigation request of type in target be blocked by Content Security Policy? given navigationParams's request, + // navigationParams's response, navigationParams's policy container's CSP list, cspNavigationType, and navigable is "Blocked", then set failure to true. + + // FIXME: 6. Otherwise, if navigationParams's reserved environment is non-null and the result of checking a navigation response's adherence to its embedder policy given + // navigationParams's response, navigable, and navigationParams's policy container's embedder policy is false, then set failure to true. + + // 8. If failure is true, then: + 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("

Failed to load {}

"sv, 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. + entry->document_state->document()->set_salvageable(false); + + // FIXME: 3. If navigationParams is not null, then: + if (navigation_params.has_value()) { + TODO(); + } + } + // FIXME: 9. Otherwise, if navigationParams's response's status is 204 or 205, then: + else if (navigation_params->response->status() == 204 || navigation_params->response->status() == 205) { + // 1. Run completionSteps. + completion_steps(); + + // 2. Return. + return; + } + // FIXME: 10. Otherwise, if navigationParams's response has a `Content-Disposition` + // header specifying the attachment disposition type, then: + // 11. Otherwise: + else { + // 1. Let document be the result of loading a document given navigationParams, sourceSnapshotParams, + // and entry's document state's initiator origin. + auto document = load_document(navigation_params); + + // 2. If document is null, then run completionSteps and return. + if (!document) { + VERIFY_NOT_REACHED(); + + completion_steps(); + return; + } + + // 3. Set entry's document state's document to document. + entry->document_state->set_document(document.ptr()); + + // 4. Set entry's document state's origin to document's origin. + entry->document_state->set_origin(document->origin()); + } + + // FIXME: 12. If entry's document state's request referrer is "client", then set it to request's referrer. + + // 13. If entry's document state's document is not null, then set entry's document state's ever populated to true. + if (entry->document_state->document()) { + entry->document_state->set_ever_populated(true); + } + + // 14. Run completionSteps. + completion_steps(); + + (void)this; + }); + + (void)source_snapshot_params; + + return {}; +} + // To navigate a navigable navigable to a URL url using a Document sourceDocument, // with an optional POST resource, string, or null documentResource (default null), // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false), diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.h b/Userland/Libraries/LibWeb/HTML/Navigable.h index 6340f18acfa..f1b041130c6 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.h +++ b/Userland/Libraries/LibWeb/HTML/Navigable.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace Web::HTML { @@ -60,6 +61,8 @@ public: Variant ongoing_navigation() const { return m_ongoing_navigation; } + WebIDL::ExceptionOr populate_session_history_entry_document(JS::GCPtr, Optional, Optional navigation_id, SourceSnapshotParams const&, Function); + WebIDL::ExceptionOr navigate( AK::URL const&, JS::NonnullGCPtr source_document,