Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / browsing_context.lucb

#==============================================================================================
#
#   browsing_context - HTML::BrowsingContext and HTML::BrowsingContextGroup: creating browsing
#   contexts with their initial about:blank document, and what they answer
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/BrowsingContext.cpp (lines 1-521) with the inline
#       functions of BrowsingContext.h, and of BrowsingContextGroup.cpp (lines 1-69) with the
#       inline functions of BrowsingContextGroup.h.
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: URL helpers =============================================================================

## Ported from BrowsingContext.cpp:34 Web::HTML::url_matches_about_blank.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
pub func html_url_matches_about_blank(url: web_url.Url) -> bool:
    # A URL matches about:blank if its scheme is "about", its path contains a single string "blank", its username and password are the empty string, and its host is null.
    let scheme = web_url.url_scheme(&url)
    let paths = web_url.url_paths(&url)
    let username = web_url.url_username(&url)
    let password = web_url.url_password(&url)
    return ak.string_eq_string_view(&scheme, ak.sv("about")) and ak.vector_size(&paths) == 1 and ak.string_eq_string_view(ak.vector_at_const(&paths, 0), ak.sv("blank")) and ak.string_is_empty(&username) and ak.string_is_empty(&password) and web_url.url_host(&url) == none

## Ported from BrowsingContext.cpp:45 Web::HTML::url_matches_about_srcdoc.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:srcdoc
pub func html_url_matches_about_srcdoc(url: web_url.Url) -> bool:
    # A URL matches about:srcdoc if its scheme is "about", its path contains a single string "srcdoc", its query is null, its username and password are the empty string, and its host is null.
    let scheme = web_url.url_scheme(&url)
    let paths = web_url.url_paths(&url)
    let username = web_url.url_username(&url)
    let password = web_url.url_password(&url)
    return ak.string_eq_string_view(&scheme, ak.sv("about")) and ak.vector_size(&paths) == 1 and ak.string_eq_string_view(ak.vector_at_const(&paths, 0), ak.sv("srcdoc")) and web_url.url_query(&url) == none and ak.string_is_empty(&username) and ak.string_is_empty(&password) and web_url.url_host(&url) == none

## Ported from BrowsingContext.cpp:57 Web::HTML::determine_the_origin.
# https://html.spec.whatwg.org/multipage/document-sequences.html#determining-the-origin
pub func html_determine_the_origin(url: const web_url.Url*?, sandbox_flags: HtmlSandboxingFlagSet, source_origin: web_url.Origin?) -> web_url.Origin:
    # 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin.
    if html_has_flag(sandbox_flags, HtmlSandboxingFlagSet.sandboxed_origin):
        return web_url.origin_create_opaque()

    # 2. If url is null, then return a new opaque origin.
    let the_url = url else return web_url.origin_create_opaque()

    # 3. If url is about:srcdoc, then:
    if web_url.url_eq(the_url, web_url.about_srcdoc()):
        # 1. Assert: sourceOrigin is non-null.
        # 2. Return sourceOrigin.
        return source_origin else trap("VERIFY: source_origin.has_value()")

    # 4. If url matches about:blank and sourceOrigin is non-null, then return sourceOrigin.
    if html_url_matches_about_blank(*the_url):
        if let origin = source_origin:
            return origin

    # 5. Return url's origin.
    return web_url.url_origin(the_url)

# mark: Creating browsing contexts ==============================================================

## Ported from BrowsingContext.cpp:87 Web::HTML::BrowsingContext::create_a_new_auxiliary_browsing_context_and_document.
# https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-auxiliary-browsing-context
pub func html_browsing_context_create_a_new_auxiliary_browsing_context_and_document(page: Page*, opener: HtmlBrowsingContext*) -> HtmlBrowsingContextBrowsingContextAndDocument:
    # 1. Let openerTopLevelBrowsingContext be opener's top-level traversable's active browsing context.
    let opener_top_level_browsing_context = html_navigable_active_browsing_context(html_browsing_context_top_level_traversable(opener).html_navigable()) else trap("no active browsing context")

    # 2. Let group be openerTopLevelBrowsingContext's group.
    # 3. Assert: group is non-null, as navigating invokes this directly.
    let group = html_browsing_context_group(opener_top_level_browsing_context) else trap("VERIFY: group")

    # 4. Set browsingContext and document be the result of creating a new browsing context and document with opener's active document, null, and group.
    let created = html_browsing_context_create_a_new_browsing_context_and_document(page, html_browsing_context_active_document(opener), none, group)
    let browsing_context = created.browsing_context
    let document = created.document

    # 5. Set browsingContext's is auxiliary to true.
    browsing_context.m_is_auxiliary = true

    # 6. Append browsingContext to group.
    html_browsing_context_group_append(group, browsing_context)

    # 7. Set browsingContext's opener browsing context to opener.
    html_browsing_context_set_opener_browsing_context(browsing_context, opener)

    # 8. Set browsingContext's virtual browsing context group ID to openerTopLevelBrowsingContext's virtual browsing context group ID.
    browsing_context.m_virtual_browsing_context_group_id = opener_top_level_browsing_context.m_virtual_browsing_context_group_id

    # 9. Set browsingContext's opener origin at creation to opener's active document's origin.
    browsing_context.m_opener_origin_at_creation = dom_document_origin(html_browsing_context_active_document(opener) else trap("no active document"))

    # 10. Return browsingContext and document.
    return HtmlBrowsingContextBrowsingContextAndDocument(browsing_context = browsing_context, document = document)

## Ported from BrowsingContext.cpp:120 Web::HTML::populate_with_html_head_body.
func html_populate_with_html_head_body(document: DomDocument*):
    let html_node = dom_create_element(document, html_tag_names_html, namespace_html) else trap("MUST: create_element")
    let head_element = dom_create_element(document, html_tag_names_head, namespace_html) else trap("MUST: create_element")
    _ = dom_node_append_child(html_node.dom_node(), head_element.dom_node()) else trap("MUST: append_child")
    let body_element = dom_create_element(document, html_tag_names_body, namespace_html) else trap("MUST: create_element")
    _ = dom_node_append_child(html_node.dom_node(), body_element.dom_node()) else trap("MUST: append_child")
    _ = dom_node_append_child(document.dom_node(), html_node.dom_node()) else trap("MUST: append_child")

## The captures of create_a_new_browsing_context_and_document's two realm customizations: the
## browsing context and `window` by reference.
struct HtmlBrowsingContextCreateRealmCustomizations:
    var browsing_context: HtmlBrowsingContext*
    var window: HtmlWindow*?

## create_a_new_browsing_context_and_document's first realm customization: the WindowProxy and,
## for the global object, a new Window.
func html_browsing_context_create_global_object(context: void*?, realm: Realm*) -> JsObject*?:
    let captures = (HtmlBrowsingContextCreateRealmCustomizations*)(context else trap("no captures"))
    let window_proxy = realm_create_html_window_proxy(realm, realm)
    html_browsing_context_set_window_proxy(captures.browsing_context, window_proxy)

    # - For the global object, create a new Window object.
    let window = html_window_create(realm)
    captures.window = window
    return (JsObject*)window

## create_a_new_browsing_context_and_document's second realm customization: the browsing
## context's WindowProxy.
func html_browsing_context_create_global_this_value(context: void*?, realm: Realm*) -> JsObject*?:
    let captures = (HtmlBrowsingContextCreateRealmCustomizations*)(context else trap("no captures"))
    # - For the global this binding, use browsingContext's WindowProxy object.
    return (JsObject*?)html_browsing_context_window_proxy(captures.browsing_context)

## `vm.heap().allocate<BrowsingContext>(page)`: a BrowsingContext cell constructed under DeferGC.
func heap_allocate_html_browsing_context(heap: gc.Heap*, page: Page*) -> HtmlBrowsingContext*:
    let browsing_context = (HtmlBrowsingContext*)web_heap_allocate_cell(heap, &html_browsing_context_class)
    gc.heap_defer_gc(heap)
    html_browsing_context_construct(browsing_context, page)
    gc.heap_undefer_gc(heap)
    return browsing_context

## Ported from BrowsingContext.cpp:131 Web::HTML::BrowsingContext::create_a_new_browsing_context_and_document.
# https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context
pub func html_browsing_context_create_a_new_browsing_context_and_document(page: Page*, creator: DomDocument*?, embedder: DomElement*?, group: HtmlBrowsingContextGroup*) -> HtmlBrowsingContextBrowsingContextAndDocument:
    let vm = js_cell_vm(group.js_cell())

    # 1. Let browsingContext be a new browsing context.
    let browsing_context = heap_allocate_html_browsing_context(vm_heap(vm), page)

    # 2. Let unsafeContextCreationTime be the unsafe shared current time.
    let unsafe_context_creation_time = high_resolution_time_unsafe_shared_current_time()

    # 3. Let creatorOrigin be null.
    var creator_origin: web_url.Origin? = none

    # 4. Let creatorBaseURL be null.
    var creator_base_url: web_url.Url? = none

    # 5. If creator is non-null, then:
    if let creator_document = creator:
        # 1. Set creatorOrigin to creator's origin.
        creator_origin = dom_document_origin(creator_document)

        # 2. Set creatorBaseURL to creator's document base URL.
        creator_base_url = dom_document_base_url(creator_document)

        # 3. Set browsingContext's virtual browsing context group ID to creator's browsing context's top-level browsing context's virtual browsing context group ID.
        let creator_browsing_context = dom_document_browsing_context(creator_document) else trap("VERIFY: creator->browsing_context()")
        let creator_top_level = html_browsing_context_top_level_browsing_context(creator_browsing_context) else trap("no top-level browsing context")
        browsing_context.m_virtual_browsing_context_group_id = creator_top_level.m_virtual_browsing_context_group_id

    # 6. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedder.
    let sandbox_flags = html_determine_the_creation_sandboxing_flags(browsing_context, embedder)

    # 7. Let origin be the result of determining the origin given about:blank, sandboxFlags, and creatorOrigin.
    let about_blank = web_url.about_blank()
    let origin = html_determine_the_origin(&about_blank, sandbox_flags, creator_origin)

    # FIXME: 8. Let permissionsPolicy be the result of creating a permissions policy given embedder and origin. [PERMISSIONSPOLICY]

    # FIXME: 9. Let agent be the result of obtaining a similar-origin window agent given origin, group, and false.

    var captures = HtmlBrowsingContextCreateRealmCustomizations(browsing_context = browsing_context, window = none)

    # 10. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
    let realm_execution_context = bind_create_a_new_javascript_realm(
        bind_main_thread_vm(),
        ak.Function1[Realm*, JsObject*?](call = html_browsing_context_create_global_object, context = &captures),
        ak.Function1[Realm*, JsObject*?](call = html_browsing_context_create_global_this_value, context = &captures),
    )

    let window = captures.window else trap("the realm has no Window")
    let realm = bind_platform_object_realm(window.bind_platform_object())

    # 11. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
    var top_level_creation_url = web_url.about_blank()
    # 12. Let topLevelOrigin be origin if embedder is null; otherwise embedder's relevant settings object's top-level origin.
    var top_level_origin = origin
    if let embedder_element = embedder:
        let embedder_settings = html_relevant_settings_object_dom_node(embedder_element.dom_node())
        top_level_creation_url = embedder_settings.environment.top_level_creation_url else trap("VERIFY: a top-level creation URL")
        top_level_origin = html_environment_settings_object_origin(embedder_settings)

    # 13. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
    html_window_environment_settings_object_setup(page, web_url.about_blank(), realm_execution_context, none, top_level_creation_url, top_level_origin)

    # 14. Let loadTimingInfo be a new document load timing info with its navigation start time set to the result of calling
    #     coarsen time with unsafeContextCreationTime and the new environment settings object's cross-origin isolated capability.
    var load_timing_info = DomDocumentLoadTimingInfo()
    let settings_object = as_html_window_environment_settings_object((gc.Cell*)bind_principal_host_defined_environment_settings_object(realm))
    load_timing_info.navigation_start_time = high_resolution_time_coarsen_time(unsafe_context_creation_time, html_environment_settings_object_cross_origin_isolated_capability(settings_object.html_environment_settings_object()))

    # 15. Let document be a new Document, with:
    let document = html_document_create(realm, web_url.about_blank()).dom_document()

    # Non-standard
    html_window_set_associated_document(window, document)

    # type: "html"
    dom_document_set_document_type(document, DocumentDomDocumentType.html)

    # content type: "text/html"
    dom_document_set_content_type(document, ak.string_from_str("text/html"))

    # mode: "quirks"
    dom_document_set_quirks_mode(document, DomQuirksMode.yes)

    # origin: origin
    dom_document_set_origin(document, origin)

    # browsing context: browsingContext
    dom_document_set_browsing_context(document, browsing_context)

    # FIXME: permissions policy: permissionsPolicy

    # active sandboxing flag set: sandboxFlags
    dom_document_set_active_sandboxing_flag_set(document, sandbox_flags)

    # load timing info: loadTimingInfo
    dom_document_set_load_timing_info(document, load_timing_info)

    # is initial about:blank: true
    dom_document_set_is_initial_about_blank(document, true)
    # Spec issue: https://github.com/whatwg/html/issues/10261
    dom_document_set_ready_to_run_scripts(document)

    # about base URL: creatorBaseURL
    dom_document_set_about_base_url(document, creator_base_url)

    # allow declarative shadow roots: true
    dom_document_set_allow_declarative_shadow_roots(document, true)

    # custom element registry: A new CustomElementRegistry object.
    dom_document_set_custom_element_registry(document, realm_create_html_custom_element_registry(realm, realm))

    # 16. If creator is non-null, then:
    if let creator_document = creator:
        # 1. Set document's referrer to the serialization of creator's URL.
        let creator_url = dom_document_url(creator_document)
        dom_document_set_referrer(document, web_url.url_serialize(&creator_url))

        # 2. Set document's policy container to a clone of creator's policy container.
        dom_document_set_policy_container(document, html_policy_container_clone(dom_document_policy_container(creator_document), gc.cell_heap(document.cell())))

        # 3. If creator's origin is same origin with creator's relevant settings object's top-level origin,
        let creator_origin_value = dom_document_origin(creator_document)
        if web_url.origin_is_same_origin(&creator_origin_value, dom_document_relevant_settings_object(creator_document).environment.top_level_origin else trap("VERIFY: a top-level origin")):
            # then set document's opener policy to creator's browsing context's top-level browsing context's active document's opener policy.
            let creator_browsing_context = dom_document_browsing_context(creator_document) else trap("VERIFY: creator->browsing_context()")
            let creator_top_level = html_browsing_context_top_level_browsing_context(creator_browsing_context) else trap("no top-level browsing context")
            let creator_top_document = html_browsing_context_active_document(creator_top_level) else trap("VERIFY: an active document")
            dom_document_set_opener_policy(document, dom_document_opener_policy(creator_top_document))

    # 17. Assert: document's URL and document's relevant settings object's creation URL are about:blank.
    let document_url = dom_document_url(document)
    assert(web_url.url_eq(&document_url, web_url.about_blank()))
    let creation_url = dom_document_relevant_settings_object(document).environment.creation_url
    assert(web_url.url_eq(&creation_url, web_url.about_blank()))

    # 18. Mark document as ready for post-load tasks.
    dom_document_set_ready_for_post_load_tasks(document, true)

    # 19. Populate with html/head/body given document.
    html_populate_with_html_head_body(document)

    # 20. Make active document.
    dom_document_make_active(document)

    # 21. Completely finish loading document.
    dom_document_completely_finish_loading(document)

    # 22. Return browsingContext and document.
    return HtmlBrowsingContextBrowsingContextAndDocument(browsing_context = browsing_context, document = document)

# mark: BrowsingContext =========================================================================

## Ported from BrowsingContext.cpp:292 Web::HTML::BrowsingContext::BrowsingContext.
pub func html_browsing_context_construct(this: HtmlBrowsingContext*, page: Page*):
    html_browsing_context_init_fields(this)
    this.m_page = page

## Ported from BrowsingContext.cpp:299 Web::HTML::BrowsingContext::visit_edges (override).
pub func html_browsing_context_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlBrowsingContext*)base
    gc.cell_visit_edges_impl(base, visitor)

    gc.cell_visitor_visit_t_2(visitor, this.m_page)
    gc.cell_visitor_visit_t(visitor, this.m_window_proxy)
    gc.cell_visitor_visit_t(visitor, this.m_group)
    gc.cell_visitor_visit_t(visitor, this.m_opener_browsing_context)

## Ported from BrowsingContext.cpp:310 Web::HTML::BrowsingContext::top_level_traversable.
# https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable
pub func html_browsing_context_top_level_traversable(this: HtmlBrowsingContext*) -> HtmlTraversableNavigable*:
    # A browsing context's top-level traversable is its active document's node navigable's top-level traversable.
    let active_document = html_browsing_context_active_document(this) else trap("no active document")
    let navigable = dom_document_navigable(active_document) else trap("no navigable")
    let traversable = html_navigable_top_level_traversable(navigable) else trap("VERIFY: traversable")
    assert(html_navigable_is_top_level_traversable(traversable.html_navigable()))
    return traversable

## Ported from BrowsingContext.cpp:320 Web::HTML::BrowsingContext::is_top_level.
# https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
pub func html_browsing_context_is_top_level(this: HtmlBrowsingContext*) -> bool:
    # A top-level browsing context is a browsing context whose active document's node navigable is a traversable navigable.
    let active_document = html_browsing_context_active_document(this) else return false
    let navigable = dom_document_navigable(active_document) else return false
    return html_navigable_is_traversable(navigable)

## Ported from BrowsingContext.cpp:326 Web::HTML::BrowsingContext::top_level_browsing_context.
pub func html_browsing_context_top_level_browsing_context(this: HtmlBrowsingContext*) -> HtmlBrowsingContext*?:
    let start = this
    let start_document = html_browsing_context_active_document(start) else trap("no active document")

    # 1. If start's active document is not fully active, then return null.
    if not dom_document_is_fully_active(start_document):
        return none

    # 2. Let navigable be start's active document's node navigable.
    var navigable = dom_document_navigable(start_document) else trap("no navigable")

    # 3. While navigable's parent is not null, set navigable to navigable's parent.
    while let parent = html_navigable_parent(navigable):
        navigable = parent

    # 4. Return navigable's active browsing context.
    return html_navigable_active_browsing_context(navigable)

## Ported from BrowsingContext.cpp:347 Web::HTML::BrowsingContext::active_document (const).
pub func html_browsing_context_active_document_const(this: HtmlBrowsingContext*) -> DomDocument*?:
    let window = html_browsing_context_active_window_const(this) else return none
    return html_window_associated_document_const(window)

## Ported from BrowsingContext.cpp:355 Web::HTML::BrowsingContext::active_document.
pub func html_browsing_context_active_document(this: HtmlBrowsingContext*) -> DomDocument*?:
    let window = html_browsing_context_active_window(this) else return none
    return html_window_associated_document(window)

## Ported from BrowsingContext.cpp:364 Web::HTML::BrowsingContext::active_window.
# https://html.spec.whatwg.org/multipage/browsers.html#active-window
pub func html_browsing_context_active_window(this: HtmlBrowsingContext*) -> HtmlWindow*?:
    return html_window_proxy_window(this.m_window_proxy else trap("dereferencing a null WindowProxy"))

## Ported from BrowsingContext.cpp:370 Web::HTML::BrowsingContext::active_window (const).
# https://html.spec.whatwg.org/multipage/browsers.html#active-window
pub func html_browsing_context_active_window_const(this: HtmlBrowsingContext*) -> HtmlWindow*?:
    return html_window_proxy_window(this.m_window_proxy else trap("dereferencing a null WindowProxy"))

## Ported from BrowsingContext.cpp:375 Web::HTML::BrowsingContext::window_proxy.
pub func html_browsing_context_window_proxy(this: HtmlBrowsingContext*) -> HtmlWindowProxy*?:
    return this.m_window_proxy

## Ported from BrowsingContext.cpp:380 Web::HTML::BrowsingContext::window_proxy (const).
pub func html_browsing_context_window_proxy_const(this: HtmlBrowsingContext*) -> HtmlWindowProxy*?:
    return this.m_window_proxy

## Ported from BrowsingContext.cpp:385 Web::HTML::BrowsingContext::set_window_proxy.
pub func html_browsing_context_set_window_proxy(this: HtmlBrowsingContext*, window_proxy: HtmlWindowProxy*?):
    this.m_window_proxy = window_proxy

## Ported from BrowsingContext.cpp:390 Web::HTML::BrowsingContext::group.
pub func html_browsing_context_group(this: HtmlBrowsingContext*) -> HtmlBrowsingContextGroup*?:
    return this.m_group

## Ported from BrowsingContext.cpp:395 Web::HTML::BrowsingContext::group (const).
pub func html_browsing_context_group_const(this: HtmlBrowsingContext*) -> HtmlBrowsingContextGroup*?:
    return this.m_group

## Ported from BrowsingContext.cpp:400 Web::HTML::BrowsingContext::set_group.
pub func html_browsing_context_set_group(this: HtmlBrowsingContext*, group: HtmlBrowsingContextGroup*?):
    this.m_group = group

## Ported from BrowsingContext.cpp:406 Web::HTML::BrowsingContext::remove.
# https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
pub func html_browsing_context_remove(this: HtmlBrowsingContext*):
    # 1. Assert: browsingContext's group is non-null, because a browsing context only gets discarded once.
    # 2. Let group be browsingContext's group.
    let group = html_browsing_context_group(this) else trap("VERIFY: group()")

    # 3. Set browsingContext's group to null.
    html_browsing_context_set_group(this, none)

    # 4. Remove browsingContext from group's browsing context set.
    _ = ak.ordered_hash_table_remove_t(html_browsing_context_group_browsing_context_set(group), this)

    # 5. If group's browsing context set is empty, then remove group from the user agent's browsing context group set.
    # NOTE: This is done by ~BrowsingContextGroup() when the refcount reaches 0.

## Ported from BrowsingContext.cpp:425 Web::HTML::BrowsingContext::the_one_permitted_sandboxed_navigator.
# https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
pub func html_browsing_context_the_one_permitted_sandboxed_navigator(this: HtmlBrowsingContext*) -> HtmlBrowsingContext*?:
    # FIXME: Implement this.
    return none

## Ported from BrowsingContext.h:70 Web::HTML::BrowsingContext::set_the_one_permitted_sandboxed_navigator.
pub func html_browsing_context_set_the_one_permitted_sandboxed_navigator(this: HtmlBrowsingContext*, arg0: HtmlBrowsingContext*?):
    # FIXME: Implement this
    return

## Ported from BrowsingContext.cpp:432 Web::HTML::BrowsingContext::is_ancestor_of.
# https://html.spec.whatwg.org/multipage/document-sequences.html#ancestor-browsing-context
pub func html_browsing_context_is_ancestor_of(this: HtmlBrowsingContext*, potential_descendant: HtmlBrowsingContext*) -> bool:
    # A browsing context potentialDescendant is said to be an ancestor of a browsing context potentialAncestor if the following algorithm returns true:

    # 1. Let potentialDescendantDocument be potentialDescendant's active document.
    let potential_descendant_document = html_browsing_context_active_document_const(potential_descendant) else trap("no active document")

    # 2. If potentialDescendantDocument is not fully active, then return false.
    if not dom_document_is_fully_active(potential_descendant_document):
        return false

    # 3. Let ancestorBCs be the list obtained by taking the browsing context of the active document of each member of potentialDescendantDocument's ancestor navigables.
    var ancestors = dom_document_ancestor_navigables(potential_descendant_document)
    for ancestor in ak.vector_span(&ancestors):
        let ancestor_browsing_context = html_navigable_active_browsing_context(ancestor else trap("no ancestor"))

        # 4. If ancestorBCs contains potentialAncestor, then return true.
        if ancestor_browsing_context == this:
            return true

    # 5. Return false.
    return false

## Ported from BrowsingContext.cpp:457 Web::HTML::BrowsingContext::is_familiar_with.
# https://html.spec.whatwg.org/multipage/document-sequences.html#familiar-with
pub func html_browsing_context_is_familiar_with(this: HtmlBrowsingContext*, other: HtmlBrowsingContext*) -> bool:
    # A browsing context A is familiar with a second browsing context B if the following algorithm returns true:
    let a = this
    let b = other
    let a_document = html_browsing_context_active_document_const(a) else trap("no active document")
    let b_document = html_browsing_context_active_document_const(b) else trap("no active document")

    # 1. If A's active document's origin is same origin with B's active document's origin, then return true.
    let a_origin = dom_document_origin(a_document)
    if web_url.origin_is_same_origin(&a_origin, dom_document_origin(b_document)):
        return true

    # 2. If A's top-level browsing context is B, then return true.
    if html_browsing_context_top_level_browsing_context(a) == b:
        return true

    # 3. If B is an auxiliary browsing context and A is familiar with B's opener browsing context, then return true.
    if let b_opener = html_browsing_context_opener_browsing_context(b):
        if html_browsing_context_is_familiar_with(a, b_opener):
            return true

    # 4. If there exists an ancestor browsing context of B whose active document has the same origin as the active document of A, then return true.
    # NOTE: This includes the case where A is an ancestor browsing context of B.

    # If B's active document is not fully active then it cannot have ancestor browsing context
    if not dom_document_is_fully_active(b_document):
        return false

    var ancestors = dom_document_ancestor_navigables(b_document)
    for ancestor in ak.vector_span(&ancestors):
        let ancestor_document = html_navigable_active_document(ancestor else trap("no ancestor")) else trap("no active document")
        let ancestor_origin = dom_document_origin(ancestor_document)
        if web_url.origin_is_same_origin(&ancestor_origin, dom_document_origin(a_document)):
            return true

    # 5. Return false.
    return false

## Ported from BrowsingContext.cpp:492 Web::HTML::determine_the_creation_sandboxing_flags.
# https://html.spec.whatwg.org/multipage/browsers.html#determining-the-creation-sandboxing-flags
pub func html_determine_the_creation_sandboxing_flags(browsing_context: HtmlBrowsingContext*, embedder: DomElement*?) -> HtmlSandboxingFlagSet:
    # To determine the creation sandboxing flags for a browsing context browsing context, given null or an element
    # embedder, return the union of the flags that are present in the following sandboxing flag sets:
    var sandboxing_flags = (HtmlSandboxingFlagSet)0

    # - If embedder is null, then: the flags set on browsing context's popup sandboxing flag set.
    if let embedder_element = embedder:
        # - If embedder is an element, then: the flags set on embedder's iframe sandboxing flag set.
        if let iframe_element = as_if_html_htmli_frame_element(embedder_element):
            sandboxing_flags |= html_htmli_frame_element_iframe_sandboxing_flag_set(iframe_element)

        # - If embedder is an element, then: the flags set on embedder's node document's active sandboxing flag set.
        sandboxing_flags |= dom_document_active_sandboxing_flag_set(dom_node_document(embedder_element.dom_node()))
    else:
        sandboxing_flags |= html_browsing_context_popup_sandboxing_flag_set(browsing_context)

    return sandboxing_flags

## Ported from BrowsingContext.cpp:515 Web::HTML::BrowsingContext::has_navigable_been_destroyed.
pub func html_browsing_context_has_navigable_been_destroyed(this: HtmlBrowsingContext*) -> bool:
    let active_document = html_browsing_context_active_document_const(this) else trap("no active document")
    let navigable = dom_document_navigable(active_document) else return true
    return html_navigable_has_been_destroyed(navigable)

# mark: BrowsingContext.h =======================================================================

## Ported from BrowsingContext.h:41 Web::HTML::BrowsingContext::is_auxiliary.
pub func html_browsing_context_is_auxiliary(this: HtmlBrowsingContext*) -> bool:
    return this.m_is_auxiliary

## Ported from BrowsingContext.h:54 Web::HTML::BrowsingContext::page.
pub func html_browsing_context_page(this: HtmlBrowsingContext*) -> Page*:
    return this.m_page

## Ported from BrowsingContext.h:55 Web::HTML::BrowsingContext::page (const).
pub func html_browsing_context_page_const(this: HtmlBrowsingContext*) -> Page*:
    return this.m_page

## Ported from BrowsingContext.h:57 Web::HTML::BrowsingContext::virtual_browsing_context_group_id.
pub func html_browsing_context_virtual_browsing_context_group_id(this: HtmlBrowsingContext*) -> u64:
    return this.m_virtual_browsing_context_group_id

## Ported from BrowsingContext.h:77 Web::HTML::BrowsingContext::opener_browsing_context.
pub func html_browsing_context_opener_browsing_context(this: HtmlBrowsingContext*) -> HtmlBrowsingContext*?:
    return this.m_opener_browsing_context

## Ported from BrowsingContext.h:78 Web::HTML::BrowsingContext::set_opener_browsing_context.
pub func html_browsing_context_set_opener_browsing_context(this: HtmlBrowsingContext*, browsing_context: HtmlBrowsingContext*?):
    this.m_opener_browsing_context = browsing_context

## Ported from BrowsingContext.h:80 Web::HTML::BrowsingContext::set_is_popup.
pub func html_browsing_context_set_is_popup(this: HtmlBrowsingContext*, is_popup: HtmlPopup):
    this.m_is_popup = is_popup

## Ported from BrowsingContext.h:81 Web::HTML::BrowsingContext::is_popup.
pub func html_browsing_context_is_popup(this: HtmlBrowsingContext*) -> HtmlPopup:
    return this.m_is_popup

## Ported from BrowsingContext.h:83 Web::HTML::BrowsingContext::popup_sandboxing_flag_set.
pub func html_browsing_context_popup_sandboxing_flag_set(this: HtmlBrowsingContext*) -> HtmlSandboxingFlagSet:
    return this.m_popup_sandboxing_flag_set

## Ported from BrowsingContext.h:84 Web::HTML::BrowsingContext::set_popup_sandboxing_flag_set.
pub func html_browsing_context_set_popup_sandboxing_flag_set(this: HtmlBrowsingContext*, value: HtmlSandboxingFlagSet):
    this.m_popup_sandboxing_flag_set = value

# mark: BrowsingContextGroup ====================================================================

## The static set of user_agent_browsing_context_group_set.
var html_user_agent_browsing_context_group_set_storage: ak.HashTable[HtmlBrowsingContextGroup*, ak.PtrTraits[HtmlBrowsingContextGroup]]

## Ported from BrowsingContextGroup.cpp:17 Web::HTML::user_agent_browsing_context_group_set.
# https://html.spec.whatwg.org/multipage/browsers.html#browsing-context-group-set
func html_user_agent_browsing_context_group_set() -> ak.HashTable[HtmlBrowsingContextGroup*, ak.PtrTraits[HtmlBrowsingContextGroup]]*:
    return &html_user_agent_browsing_context_group_set_storage

## Ported from BrowsingContextGroup.cpp:23 Web::HTML::BrowsingContextGroup::BrowsingContextGroup.
## (The user agent's browsing context group set holds raw pointers, as C++'s static HashTable of
## GC::Refs: the set does not keep its groups alive, and finalize removes them.)
pub func html_browsing_context_group_construct(this: HtmlBrowsingContextGroup*, page: Page*):
    this.m_page = page
    with memory.heap:
        _ = ak.hash_table_set(html_user_agent_browsing_context_group_set(), this)

## Ported from BrowsingContextGroup.cpp:29 Web::HTML::BrowsingContextGroup::finalize (override).
pub func html_browsing_context_group_finalize_impl(base: gc.Cell*):
    let this = (HtmlBrowsingContextGroup*)base
    gc.cell_finalize_impl(base)
    with memory.heap:
        _ = ak.hash_table_remove_t(html_user_agent_browsing_context_group_set(), this)

## Ported from BrowsingContextGroup.cpp:35 Web::HTML::BrowsingContextGroup::visit_edges (override).
pub func html_browsing_context_group_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlBrowsingContextGroup*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_page)
    gc.cell_visitor_visit_opaque_2(visitor, &this.m_browsing_context_set)

## `Bindings::main_thread_vm().heap().allocate<BrowsingContextGroup>(page)`: a BrowsingContextGroup
## cell constructed under DeferGC.
func heap_allocate_html_browsing_context_group(heap: gc.Heap*, page: Page*) -> HtmlBrowsingContextGroup*:
    let group = (HtmlBrowsingContextGroup*)web_heap_allocate_cell(heap, &html_browsing_context_group_class)
    gc.heap_defer_gc(heap)
    html_browsing_context_group_construct(group, page)
    gc.heap_undefer_gc(heap)
    return group

## Ported from BrowsingContextGroup.cpp:43 Web::HTML::BrowsingContextGroup::create_a_new_browsing_context_group_and_document.
# https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context-group-and-document
pub func html_browsing_context_group_create_a_new_browsing_context_group_and_document(page: Page*) -> HtmlBrowsingContextGroupBrowsingContextGroupAndDocument:
    # 1. Let group be a new browsing context group.
    # 2. Append group to the user agent's browsing context group set.
    let group = heap_allocate_html_browsing_context_group(vm_heap(bind_main_thread_vm()), page)

    # 3. Let browsingContext and document be the result of creating a new browsing context and document with null, null, and group.
    let created = html_browsing_context_create_a_new_browsing_context_and_document(page, none, none, group)

    # 4. Append browsingContext to group.
    html_browsing_context_group_append(group, created.browsing_context)

    # 5. Return group and document.
    return HtmlBrowsingContextGroupBrowsingContextGroupAndDocument(browsing_context = group, document = created.document)

## Ported from BrowsingContextGroup.cpp:60 Web::HTML::BrowsingContextGroup::append.
# https://html.spec.whatwg.org/multipage/browsers.html#bcg-append
pub func html_browsing_context_group_append(this: HtmlBrowsingContextGroup*, browsing_context: HtmlBrowsingContext*):
    # 1. Append browsingContext to group's browsing context set.
    _ = ak.ordered_hash_table_set(&this.m_browsing_context_set, browsing_context)

    # 2. Set browsingContext's group to group.
    html_browsing_context_set_group(browsing_context, this)

## Ported from BrowsingContextGroup.h:29 Web::HTML::BrowsingContextGroup::page.
pub func html_browsing_context_group_page(this: HtmlBrowsingContextGroup*) -> Page*:
    return this.m_page

## Ported from BrowsingContextGroup.h:30 Web::HTML::BrowsingContextGroup::page (const).
pub func html_browsing_context_group_page_const(this: HtmlBrowsingContextGroup*) -> Page*:
    return this.m_page

## Ported from BrowsingContextGroup.h:32 Web::HTML::BrowsingContextGroup::browsing_context_set.
pub func html_browsing_context_group_browsing_context_set(this: HtmlBrowsingContextGroup*) -> ak.OrderedHashTable[HtmlBrowsingContext*, ak.PtrTraits[HtmlBrowsingContext]]*:
    return &this.m_browsing_context_set

## Ported from BrowsingContextGroup.h:33 Web::HTML::BrowsingContextGroup::browsing_context_set (const).
pub func html_browsing_context_group_browsing_context_set_const(this: HtmlBrowsingContextGroup*) -> ak.OrderedHashTable[HtmlBrowsingContext*, ak.PtrTraits[HtmlBrowsingContext]]:
    return this.m_browsing_context_set