Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / history.lucb

#==============================================================================================
#
#   history - HTML::History as data: the session history's index, length and state that
#   navigations and traversals keep up to date
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/History.cpp lines 22-46 and 82-85 (create, the
#       constructor, initialize, visit_edges, unsafe_state) and 130-170
#       (can_have_its_url_rewritten), with the inline members of History.h (set_state and the
#       m_index and m_length members that Document, a friend, reads and sets). Loading a
#       document, an iframe's among them, makes its History and updates it with scripting off.
#
#       The JS-facing methods (pushState, replaceState, go, back, forward, length, state,
#       scrollRestoration and the delta traverse and push/replace state steps they share) are
#       phase 3.
#
#       Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from History.cpp:22 Web::HTML::History::create.
pub func html_history_create(realm: Realm*) -> HtmlHistory*:
    return realm_create_html_history(realm, realm)

## Ported from History.cpp:27 Web::HTML::History::History.
pub func html_history_construct(this: HtmlHistory*, realm: Realm*):
    html_history_init_fields(this)
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)

## Ported from History.cpp:34 Web::HTML::History::initialize (override).
pub func html_history_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.history)
    # Base::initialize(realm): PlatformObject inherits JS::Object's.
    js_cell_initialize_impl(base, realm)

## Ported from History.cpp:40 Web::HTML::History::visit_edges (override).
pub func html_history_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlHistory*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    js_visit_value(visitor, this.m_state)

## Ported from History.cpp:82 Web::HTML::History::unsafe_state.
pub func html_history_unsafe_state(this: HtmlHistory*) -> JsValue:
    return this.m_state

## Ported from History.h:41 Web::HTML::History::set_state.
pub func html_history_set_state(this: HtmlHistory*, state: JsValue):
    this.m_state = state

## History::m_index (History.h:37), which Document (a friend) reads.
pub func html_history_index(this: HtmlHistory*) -> usize:
    return (usize)this.m_index

## History::m_index (History.h:37), which Document (a friend) sets.
pub func html_history_set_index(this: HtmlHistory*, index: usize):
    this.m_index = (u64)index

## History::m_length (History.h:38), which Document (a friend) reads.
pub func html_history_length(this: HtmlHistory*) -> usize:
    return (usize)this.m_length

## History::m_length (History.h:38), which Document (a friend) sets.
pub func html_history_set_length(this: HtmlHistory*, length: usize):
    this.m_length = (u64)length

## Ported from History.cpp:130 Web::HTML::can_have_its_url_rewritten.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
pub func html_can_have_its_url_rewritten(document: DomDocument*, target_url: web_url.Url) -> bool:
    # 1. Let documentURL be document's URL.
    let document_url = dom_document_url(document)

    # 2. If targetURL and documentURL differ in their scheme, username, password, host, or port components,
    #    then return false.
    let target_scheme = web_url.url_scheme(&target_url)
    let document_scheme = web_url.url_scheme(&document_url)
    let target_username = web_url.url_username(&target_url)
    let document_username = web_url.url_username(&document_url)
    let target_password = web_url.url_password(&target_url)
    let document_password = web_url.url_password(&document_url)
    # (One condition in C++, checked a component at a time.)
    if not ak.string_equals(&target_scheme, &document_scheme) or not ak.string_equals(&target_username, &document_username):
        return false
    if not ak.string_equals(&target_password, &document_password):
        return false
    if not html_history_hosts_equal(web_url.url_host(&target_url), web_url.url_host(&document_url)) or web_url.url_port(&target_url) != web_url.url_port(&document_url):
        return false

    # 3. If targetURL's scheme is an HTTP(S) scheme, then return true.
    #    (Differences in path, query, and fragment are allowed for http: and https: URLs.)
    if ak.string_eq_string_view(&target_scheme, ak.sv("http")) or ak.string_eq_string_view(&target_scheme, ak.sv("https")):
        return true

    # 4. If targetURL's scheme is "file", then:
    #    (Differences in query and fragment are allowed for file: URLs.)
    if ak.string_eq_string_view(&target_scheme, ak.sv("file")):
        # 1. If targetURL and documentURL differ in their path component, then return false.
        if not html_history_paths_equal(&target_url, &document_url):
            return false

        # 2. Return true.
        return true

    # 5. If targetURL and documentURL differ in their path component or query components, then return false.
    #    (Only differences in fragment are allowed for other types of URLs.)
    if not html_history_paths_equal(&target_url, &document_url) or not html_history_optional_strings_equal(web_url.url_query(&target_url), web_url.url_query(&document_url)):
        return false

    # 6. Return true.
    return true

## `target_url.host() != document_url.host()`: Optional<Host>'s ==.
func html_history_hosts_equal(a: web_url.Host?, b: web_url.Host?) -> bool:
    if let a_host = a:
        if let b_host = b:
            return web_url.host_eq(&a_host, b_host)
        return false
    return b == none

## `target_url.paths() != document_url.paths()`: Vector<String>'s ==.
func html_history_paths_equal(a: const web_url.Url*, b: const web_url.Url*) -> bool:
    let a_paths = web_url.url_paths(a)
    let b_paths = web_url.url_paths(b)
    if ak.vector_size(&a_paths) != ak.vector_size(&b_paths):
        return false
    for index in 0..<ak.vector_size(&a_paths):
        if not ak.string_equals(ak.vector_at_const(&a_paths, index), ak.vector_at_const(&b_paths, index)):
            return false
    return true

## `target_url.query() != document_url.query()`: Optional<String>'s ==.
func html_history_optional_strings_equal(a: ak.String?, b: ak.String?) -> bool:
    if let a_string = a:
        if let b_string = b:
            return ak.string_equals(&a_string, &b_string)
        return false
    return b == none