dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / navigation.lucb
#==============================================================================================
#
# navigation - HTML::Navigation as the session history keeps it: the entry list of a
# window's navigation API and its current entry
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/Navigation.cpp lines 68-96 (create, the constructor,
# initialize, visit_edges), 113-126 (current_entry), 451-499
# (has_entries_and_events_disabled, get_the_navigation_api_entry_index), 1428-1458
# (initialize_the_navigation_api_entries_for_a_new_document) and 1461-1558
# (update_the_navigation_api_entries_for_a_same_document_navigation), with the inline
# accessors of Navigation.h (ongoing_navigate_event, focus_changed_during_ongoing_navigation
# and its setter, set_was_initial_about_blank_opened): what loading a document, an
# iframe's among them, and navigating within it (a fragment) need of the Window's
# navigation API with scripting off.
#
# The JS-facing API (entries(), navigate(), reload(), traverseTo(), back(), forward(),
# updateCurrentEntry(), the event handlers), the navigate event and its firing
# algorithms, the API method trackers and aborting an ongoing navigation are phase 3.
#
# Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Navigation.cpp:68 Web::HTML::Navigation::create.
pub func html_navigation_create(realm: Realm*) -> HtmlNavigation*:
return realm_create_html_navigation(realm, realm)
## Ported from Navigation.cpp:73 Web::HTML::Navigation::Navigation.
pub func html_navigation_construct(this: HtmlNavigation*, realm: Realm*):
html_navigation_init_fields(this)
dom_event_target_construct(this.dom_event_target(), realm)
## Ported from Navigation.cpp:80 Web::HTML::Navigation::initialize (override).
pub func html_navigation_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.navigation)
dom_event_target_initialize_impl(base, realm)
## Ported from Navigation.cpp:86 Web::HTML::Navigation::visit_edges (override).
pub func html_navigation_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlNavigation*)base
dom_event_target_visit_edges_impl(base, visitor)
for entry in ak.vector_span(&this.m_entry_list):
gc.cell_visitor_visit_t(visitor, entry)
gc.cell_visitor_visit_cell(visitor, this.m_transition)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_ongoing_navigate_event)
gc.cell_visitor_visit_cell(visitor, this.m_ongoing_api_method_tracker)
gc.cell_visitor_visit_cell(visitor, this.m_upcoming_non_traverse_api_method_tracker)
for tracker in ak.hash_map_begin(&this.m_upcoming_traverse_api_method_trackers):
gc.cell_visitor_visit_cell(visitor, tracker.value)
## Ported from Navigation.cpp:113 Web::HTML::Navigation::current_entry.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry
pub func html_navigation_current_entry(this: HtmlNavigation*) -> HtmlNavigationHistoryEntry*?:
# The current entry of a Navigation navigation is the result of running the following steps:
# 1. If navigation has entries and events disabled, then return null.
if html_navigation_has_entries_and_events_disabled(this):
return none
# 2. Assert: navigation's current entry index is not −1.
assert(this.m_current_entry_index != -1)
# 3. Return navigation's entry list[navigation's current entry index].
return *ak.vector_at(&this.m_entry_list, usize(this.m_current_entry_index))
## Ported from Navigation.cpp:451 Web::HTML::Navigation::has_entries_and_events_disabled.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#has-entries-and-events-disabled
pub func html_navigation_has_entries_and_events_disabled(this: HtmlNavigation*) -> bool:
# A Navigation navigation has entries and events disabled if the following steps return true:
# 1. Let document be navigation's relevant global object's associated Document.
let document = html_window_associated_document(as_html_window((gc.Cell*)html_relevant_global_object((JsObject*)this)))
# 2. If document is not fully active, then return true.
if not dom_document_is_fully_active(document):
return true
# 3. If document's is initial about:blank is true, then return true.
if dom_document_is_initial_about_blank(document):
return true
# 4. If document's origin is opaque, then return true.
let origin = dom_document_origin(document)
if web_url.origin_is_opaque(&origin):
return true
# AD-HOC: Keep entries and events disabled when document.open() is called on an initial about:blank document. This
# behavior matches other engines.
# Spec issue: https://github.com/whatwg/html/issues/11811
if this.m_was_initial_about_blank_opened:
return true
# 5. Return false.
return false
## Ported from Navigation.cpp:481 Web::HTML::Navigation::get_the_navigation_api_entry_index.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#getting-the-navigation-api-entry-index
pub func html_navigation_get_the_navigation_api_entry_index(this: HtmlNavigation*, she: const HtmlSessionHistoryEntry*) -> i64:
# To get the navigation API entry index of a session history entry she within a Navigation navigation:
# 1. Let index be 0.
var index: i64 = 0
# 2. For each nhe of navigation's entry list:
for nhe in ak.vector_span(&this.m_entry_list):
# 1. If nhe's session history entry is equal to she, then return index.
if (const HtmlSessionHistoryEntry*)html_navigation_history_entry_session_history_entry(nhe) == she:
return index
# 2. Increment index by 1.
index += 1
# 3. Return −1.
return -1
## Ported from Navigation.h:132 Web::HTML::Navigation::ongoing_navigate_event.
pub func html_navigation_ongoing_navigate_event(this: HtmlNavigation*) -> HtmlNavigateEvent*?:
return this.m_ongoing_navigate_event
## Ported from Navigation.h:134 Web::HTML::Navigation::focus_changed_during_ongoing_navigation.
pub func html_navigation_focus_changed_during_ongoing_navigation(this: HtmlNavigation*) -> bool:
return this.m_focus_changed_during_ongoing_navigation
## Ported from Navigation.h:135 Web::HTML::Navigation::set_focus_changed_during_ongoing_navigation.
pub func html_navigation_set_focus_changed_during_ongoing_navigation(this: HtmlNavigation*, value: bool):
this.m_focus_changed_during_ongoing_navigation = value
## Ported from Navigation.h:137 Web::HTML::Navigation::set_was_initial_about_blank_opened.
pub func html_navigation_set_was_initial_about_blank_opened(this: HtmlNavigation*, b: bool):
this.m_was_initial_about_blank_opened = b
## Ported from Navigation.cpp:1428 Web::HTML::Navigation::initialize_the_navigation_api_entries_for_a_new_document.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#initialize-the-navigation-api-entries-for-a-new-document
pub func html_navigation_initialize_the_navigation_api_entries_for_a_new_document(this: HtmlNavigation*, new_shes: ak.Vector[HtmlSessionHistoryEntry*], initial_she: HtmlSessionHistoryEntry*):
let realm = html_relevant_realm((JsObject*)this)
# 1. Assert: navigation's entry list is empty.
assert(ak.vector_is_empty(&this.m_entry_list))
# 2. Assert: navigation's current entry index is −1.
assert(this.m_current_entry_index == -1)
# AD-HOC: This flag is only set when an about:blank page is modified by document.open(). Reset it now that a new
# navigation is happening.
this.m_was_initial_about_blank_opened = false
# 3. If navigation has entries and events disabled, then return.
if html_navigation_has_entries_and_events_disabled(this):
return
# 4. For each newSHE of newSHEs:
for new_she in ak.vector_span_const(&new_shes):
# 1. Let newNHE be a new NavigationHistoryEntry created in the relevant realm of navigation.
# 2. Set newNHE's session history entry to newSHE.
let new_nhe = html_navigation_history_entry_create(realm, new_she)
# 3. Append newNHE to navigation's entry list.
ak.vector_append_t(&this.m_entry_list, new_nhe)
# 5. Set navigation's current entry index to the result of getting the navigation API entry index of initialSHE within navigation.
this.m_current_entry_index = html_navigation_get_the_navigation_api_entry_index(this, initial_she)
## Ported from Navigation.cpp:1461 Web::HTML::Navigation::update_the_navigation_api_entries_for_a_same_document_navigation.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
pub func html_navigation_update_the_navigation_api_entries_for_a_same_document_navigation(this: HtmlNavigation*, destination_she: HtmlSessionHistoryEntry*, navigation_type: BindNavigationType):
let realm = html_relevant_realm((JsObject*)this)
# 1. If navigation has entries and events disabled, then return.
if html_navigation_has_entries_and_events_disabled(this):
return
# 2. Let oldCurrentNHE be the current entry of navigation.
let old_current_nhe = html_navigation_current_entry(this)
# 3. Let disposedNHEs be a new empty list.
var disposed_nhes: ak.Vector[HtmlNavigationHistoryEntry*]
# 4. If navigationType is "traverse", then:
if navigation_type == BindNavigationType.traverse:
# 1. Set navigation's current entry index to the result of getting the navigation API entry index of destinationSHE within navigation.
this.m_current_entry_index = html_navigation_get_the_navigation_api_entry_index(this, destination_she)
# 2. Assert: navigation's current entry index is not −1.
# NOTE: This algorithm is only called for same-document traversals.
# Cross-document traversals will instead call either initialize the navigation API entries for a new document
# or update the navigation API entries for reactivation
assert(this.m_current_entry_index != -1)
# 5. Otherwise, if navigationType is "push", then:
elif navigation_type == BindNavigationType.push:
# 1. Set navigation's current entry index to navigation's current entry index + 1.
this.m_current_entry_index += 1
# 2. Let i be navigation's current entry index.
var i = this.m_current_entry_index
# 3. While i < navigation's entry list's size:
while i < (i64)ak.vector_size(&this.m_entry_list):
# 1. Append navigation's entry list[i] to disposedNHEs.
ak.vector_append_t(&disposed_nhes, *ak.vector_at(&this.m_entry_list, (usize)i))
# 2. Set i to i + 1.
i += 1
# 4. Remove all items in disposedNHEs from navigation's entry list.
while (i64)ak.vector_size(&this.m_entry_list) > this.m_current_entry_index:
ak.vector_remove(&this.m_entry_list, (usize)this.m_current_entry_index)
# 6. Otherwise, if navigationType is "replace", then:
elif navigation_type == BindNavigationType.replace:
# 1. Append oldCurrentNHE to disposedNHEs.
ak.vector_append_t(&disposed_nhes, old_current_nhe else trap("VERIFY: old_current_nhe != nullptr"))
# 7. If navigationType is "push" or "replace", then:
if navigation_type == BindNavigationType.push or navigation_type == BindNavigationType.replace:
# 1. Let newNHE be a new NavigationHistoryEntry created in the relevant realm of navigation.
# 2. Set newNHE's session history entry to destinationSHE.
let new_nhe = html_navigation_history_entry_create(realm, destination_she)
assert(this.m_current_entry_index != -1)
# 3. Set navigation's entry list[navigation's current entry index] to newNHE.
if this.m_current_entry_index < (i64)ak.vector_size(&this.m_entry_list):
*ak.vector_at(&this.m_entry_list, (usize)this.m_current_entry_index) = new_nhe
else:
assert(this.m_current_entry_index == (i64)ak.vector_size(&this.m_entry_list))
ak.vector_append_t(&this.m_entry_list, new_nhe)
# 8. If navigation's ongoing API method tracker is non-null, then notify about the committed-to entry
# given navigation's ongoing API method tracker and the current entry of navigation.
# NOTE: It is important to do this before firing the dispose or currententrychange events,
# since event handlers could start another navigation, or otherwise change the value of
# navigation's ongoing API method tracker.
if this.m_ongoing_api_method_tracker != none:
trap("unported (P3): Web::HTML::Navigation::notify_about_the_committed_to_entry (the API method trackers)")
# 9. Prepare to run script given navigation's relevant settings object.
let settings = html_relevant_settings_object_js_object((JsObject*)this)
html_prepare_to_run_script(settings)
# 10. Fire an event named currententrychange at navigation using NavigationCurrentEntryChangeEvent,
# with its navigationType attribute initialized to navigationType and its from initialized to oldCurrentNHE.
let event_init = HtmlNavigationCurrentEntryChangeEventInit(event_init = DomEventInit(), navigation_type = navigation_type, from_ = old_current_nhe)
let event = html_navigation_current_entry_change_event_construct_impl(realm, html_event_names_currententrychange, &event_init)
_ = dom_event_target_dispatch_event(this.dom_event_target(), event.dom_event())
# 11. For each disposedNHE of disposedNHEs:
for disposed_nhe in ak.vector_span(&disposed_nhes):
# 1. Fire an event named dispose at disposedNHE.
_ = dom_event_target_dispatch_event(disposed_nhe.dom_event_target(), dom_event_create(realm, html_event_names_dispose))
# 12. Clean up after running script given navigation's relevant settings object.
html_clean_up_after_running_script(settings)