Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / document_3.lucb

#==============================================================================================
#
#   document_3 - DOM::Document: base URLs, viewport propagation, updating layout
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Document.cpp (lines 1309-1751): base elements and base URLs,
#       setting the URL, encoding-parsing URLs and invalidating the layout tree, partial SVG
#       relayout, overflow and scrollbar-width propagation to the viewport, and
#       update_layout() with its up-to-date test.
#
#==============================================================================================

# mark: Base URLs ===============================================================================

## Ported from Document.cpp:1309 Web::DOM::Document::update_base_element.
pub func dom_document_update_base_element(this: DomDocument*):
    var base_element_with_href: HtmlBaseElement*? = none
    var base_element_with_target: HtmlBaseElement*? = none

    # for_each_in_subtree_of_type<HTML::HTMLBaseElement>
    for node in dom_node_subtree(this.dom_node()):
        let base_element_in_tree = as_if_html_base_element(node) else continue
        if base_element_with_href == none and dom_element_has_attribute(base_element_in_tree.dom_element(), html_attribute_names_href):
            base_element_with_href = base_element_in_tree
            if base_element_with_target != none:
                break
        if base_element_with_target == none and dom_element_has_attribute(base_element_in_tree.dom_element(), html_attribute_names_target):
            base_element_with_target = base_element_in_tree
            if base_element_with_href != none:
                break

    this.m_first_base_element_with_href_in_tree_order = base_element_with_href
    this.m_first_base_element_with_target_in_tree_order = base_element_with_target

## Ported from Document.cpp:1333 Web::DOM::Document::first_base_element_with_href_in_tree_order.
pub func dom_document_first_base_element_with_href_in_tree_order(this: DomDocument*) -> HtmlBaseElement*?:
    return this.m_first_base_element_with_href_in_tree_order

## Ported from Document.cpp:1338 Web::DOM::Document::first_base_element_with_target_in_tree_order.
pub func dom_document_first_base_element_with_target_in_tree_order(this: DomDocument*) -> HtmlBaseElement*?:
    return this.m_first_base_element_with_target_in_tree_order

## Ported from Document.cpp:1344 Web::DOM::Document::respond_to_base_url_changes.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#respond-to-base-url-changes
pub func dom_document_respond_to_base_url_changes(this: DomDocument*):
    # To respond to base URL changes for a Document document:

    # 1. The user agent should update any user interface elements which are displaying affected URLs, or data derived
    #    from such URLs, to the user. Examples of such user interface elements would be a status bar that displays a
    #    hyperlink's url, or some user interface which displays the URL specified by a q, blockquote, ins, or del
    #    element's cite attribute.
    # FIXME: Update those UI elements.

    # 2. Ensure that the CSS :link/:visited/etc. pseudo-classes are updated appropriately.
    dom_node_invalidate_style(this.dom_node(), DomStyleInvalidationReason.base_url_changed)

    # FIXME: 3. For each descendant of document's shadow-including descendants:
    #        ...

    # FIXME: 4. Consider speculative loads given document.

## Ported from Document.cpp:1364 Web::DOM::Document::set_url.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#set-the-url
pub func dom_document_set_url(this: DomDocument*, url: web_url.Url):
    # OPTIMIZATION: Avoid unnecessary work if the URL is already set.
    if web_url.url_eq(&this.m_url, url):
        return

    dom_document_ensure_cookie_version_index(this, url, this.m_url)

    # To set the URL for a Document document to a URL record url:

    # 1. Set document's URL to url.
    this.m_url = url

    # 2. Respond to base URL changes given document.
    dom_document_respond_to_base_url_changes(this)

## Ported from Document.cpp:1382 Web::DOM::Document::fallback_base_url.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url
pub func dom_document_fallback_base_url(this: DomDocument*) -> web_url.Url:
    # 1. If document is an iframe srcdoc document, then:
    if html_url_matches_about_srcdoc(this.m_url):
        # 1. Assert: document's about base URL is non-null.
        # 2. Return document's about base URL.
        return this.m_about_base_url else trap("VERIFY: an iframe srcdoc document has an about base URL")

    # 2. If document's URL matches about:blank and document's about base URL is non-null, then return document's about base URL.
    if html_url_matches_about_blank(this.m_url):
        if let about_base_url = this.m_about_base_url:
            return about_base_url

    # 3. Return document's URL.
    return this.m_url

## Ported from Document.cpp:1402 Web::DOM::Document::base_url.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url
pub func dom_document_base_url(this: DomDocument*) -> web_url.Url:
    # 1. If document has no descendant base element that has an href attribute, then return document's fallback base URL.
    let base_element = dom_document_first_base_element_with_href_in_tree_order(this) else return dom_document_fallback_base_url(this)

    # 2. Otherwise, return the frozen base URL of the first base element in document that has an href attribute, in tree order.
    return html_base_element_frozen_base_url(base_element)

## Ported from Document.cpp:1414 Web::DOM::Document::encoding_parse_url.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-a-url
pub func dom_document_encoding_parse_url(this: DomDocument*, url: ak.StringView) -> web_url.Url?:
    # 1. Let encoding be UTF-8.
    # 2. If environment is a Document object, then set encoding to environment's character encoding.
    let encoding = dom_document_encoding_or_default(this)

    # 3. Otherwise, if environment's relevant global object is a Window object, set encoding to environment's relevant
    #    global object's associated Document's character encoding.

    # 4. Let baseURL be environment's base URL, if environment is a Document object; otherwise environment's API base URL.
    let base_url = dom_document_base_url(this)

    # 5. Return the result of applying the URL parser to url, with baseURL and encoding.
    return domurl_parse(url, base_url, ak.string_bytes_as_string_view(&encoding))

## Ported from Document.cpp:1431 Web::DOM::Document::encoding_parse_and_serialize_url.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-and-serializing-a-url
pub func dom_document_encoding_parse_and_serialize_url(this: DomDocument*, url: ak.StringView) -> ak.String?:
    # 1. Let url be the result of encoding-parsing a URL given url, relative to environment.
    # 2. If url is failure, then return failure.
    let parsed_url = dom_document_encoding_parse_url(this, url) else return none

    # 3. Return the result of applying the URL serializer to url.
    return web_url.url_serialize(&parsed_url)

## Ported from Document.cpp:1444 Web::DOM::Document::invalidate_layout_tree.
pub func dom_document_invalidate_layout_tree(this: DomDocument*, reason: DomInvalidateLayoutTreeReason):
    if this.m_layout_root != none:
        if update_layout_debug:
            ak.dbgln(f"DROP TREE {ak.string_view_text(dom_to_string_dom_invalidate_layout_tree_rea(reason))}")
    dom_document_tear_down_layout_tree(this)

# mark: Partial SVG relayout ====================================================================

## Ported from Document.cpp:1451 Web::DOM::Document::mark_svg_root_as_needing_relayout.
pub func dom_document_mark_svg_root_as_needing_relayout(this: DomDocument*, svg_root: LayoutSvgsvgBox*):
    _ = ak.hash_table_set(&this.m_svg_roots_needing_relayout, svg_root)

## Ported from Document.cpp:1456 Web::DOM::relayout_svg_root.
func dom_relayout_svg_root(svg_root: LayoutSvgsvgBox*):
    var layout_state: LayoutState
    layout_state_construct(&layout_state, svg_root.layout_node_with_style())

    # Pre-populate the svg_root itself.
    if let paintable = layout_box_paintable_box(svg_root.layout_box()):
        _ = layout_state_populate_from_paintable(&layout_state, svg_root.layout_node_with_style(), paintable)

    # Pre-populate SVGGraphicsBox ancestors (up to outer SVG) for get_parent_svg_transform().
    var ancestor = layout_node_parent(svg_root.layout_node())
    while let current = ancestor:
        if let svg_graphics_ancestor = as_if_layout_svg_graphics_box(current):
            if let paintable = layout_box_paintable_box(svg_graphics_ancestor.layout_box()):
                _ = layout_state_populate_from_paintable(&layout_state, svg_graphics_ancestor.layout_node_with_style(), paintable)
        if is_layout_svgsvg_box(current):
            break
        ancestor = layout_node_parent(current.layout_node())

    # Pre-populate the viewport for position:fixed elements inside <foreignObject>.
    let viewport = layout_node_root(svg_root.layout_node())
    if let paintable = layout_box_paintable_box(viewport.layout_box()):
        _ = layout_state_populate_from_paintable(&layout_state, viewport.layout_node_with_style(), paintable)

    let svg_state = layout_state_get(&layout_state, svg_root.layout_node_with_style())
    let content_width = layout_state_used_values_content_width(svg_state)
    let content_height = layout_state_used_values_content_height(svg_state)

    var identity: gfx.AffineTransform = ---
    gfx.affine_transform_construct(&identity)
    var svg_context: LayoutSvgFormattingContext = ---
    layout_svg_formatting_context_construct(&svg_context, &layout_state, LayoutMode.normal, svg_root.layout_box(), none, identity)
    var available_space: LayoutAvailableSpace = ---
    layout_available_space_construct(&available_space, layout_available_size_make_definite(content_width), layout_available_size_make_definite(content_height))
    layout_formatting_context_run(&svg_context.formatting_context, available_space)
    layout_state_commit(&layout_state, svg_root.layout_box())

    for node in layout_node_inclusive_subtree(svg_root.layout_node()):
        layout_node_reset_needs_layout_update(node)

# mark: Propagation to the viewport =============================================================

## Ported from Document.cpp:1493 Web::DOM::propagate_scrollbar_width_to_viewport.
func dom_propagate_scrollbar_width_to_viewport(root_element: DomElement*, viewport: LayoutViewport*):
    # https://drafts.csswg.org/css-scrollbars/#scrollbar-width
    # UAs must apply the scrollbar-color value set on the root element to the viewport.
    let viewport_computed_values = layout_node_with_style_mutable_computed_values(viewport.layout_node_with_style())
    # NB: Called during layout tree construction.
    let root_element_layout_node = dom_element_unsafe_layout_node(root_element) else trap("the root element has no layout node")
    let root_element_computed_values = layout_node_with_style_computed_values(root_element_layout_node)
    css_mutable_computed_values_set_scrollbar_width(viewport_computed_values, css_computed_values_scrollbar_width(&root_element_computed_values.computed_values))

## `!element.computed_properties()->contain().is_empty()`.
func dom_element_has_containment(element: DomElement*) -> bool:
    let computed_properties = dom_element_computed_properties(element) else trap("the element has no computed properties")
    let contain = css_computed_properties_contain(computed_properties)
    return not css_containment_is_empty(&contain)

## The overflow values to apply to the viewport: 'visible' as 'auto', 'clip' as 'hidden'.
func dom_overflow_to_apply_to_viewport(overflow: css_data.Overflow) -> css_data.Overflow:
    if overflow == css_data.Overflow.visible:
        return css_data.Overflow.auto
    elif overflow == css_data.Overflow.clip:
        return css_data.Overflow.hidden
    return overflow

## Ported from Document.cpp:1504 Web::DOM::propagate_overflow_to_viewport.
# https://drafts.csswg.org/css-overflow-3/#overflow-propagation
func dom_propagate_overflow_to_viewport(root_element: DomElement*, viewport: LayoutViewport*):
    # https://drafts.csswg.org/css-contain-2/#contain-property
    # Additionally, when any containments are active on either the HTML <html> or <body> elements, propagation of
    # properties from the <body> element to the initial containing block, the viewport, or the canvas background, is
    # disabled. Notably, this affects:
    # - 'overflow' and its longhands (see CSS Overflow 3 § 3.3 Overflow Viewport Propagation)
    if dom_node_is_html_html_element(root_element.dom_node()) and dom_element_has_containment(root_element):
        return

    if let body_element = tree_node_first_child_of_type(root_element.dom_node(), &html_body_element_class):
        if dom_element_has_containment((DomElement*)body_element):
            return

    # UAs must apply the overflow-* values set on the root element to the viewport
    # when the root element’s display value is not none.
    # NB: Called during layout tree construction.
    var overflow_origin_node = dom_element_unsafe_layout_node(root_element) else trap("the root element has no layout node")
    let viewport_computed_values = layout_node_with_style_mutable_computed_values(viewport.layout_node_with_style())

    # However, when the root element is an [HTML] html element (including XML syntax for HTML)
    # whose overflow value is visible (in both axes), and that element has as a child
    # a body element whose display value is also not none,
    # user agents must instead apply the overflow-* values of the first such child element to the viewport.
    if dom_node_is_html_html_element(root_element.dom_node()):
        let root_element_layout_node = dom_element_unsafe_layout_node(root_element) else trap("the root element has no layout node")
        let root_element_computed_values = layout_node_with_style_mutable_computed_values(root_element_layout_node)
        if css_computed_values_overflow_x(&root_element_computed_values.computed_values) == css_data.Overflow.visible and css_computed_values_overflow_y(&root_element_computed_values.computed_values) == css_data.Overflow.visible:
            if let body_element = tree_node_first_child_of_type(root_element.dom_node(), &html_body_element_class):
                if let body_layout_node = dom_element_unsafe_layout_node((DomElement*)body_element):
                    overflow_origin_node = body_layout_node

    # If 'visible' is applied to the viewport, it must be interpreted as 'auto'. If 'clip' is applied to the viewport, it must be interpreted as 'hidden'.
    let overflow_origin_computed_values = layout_node_with_style_mutable_computed_values(overflow_origin_node)
    let overflow_x_to_apply = dom_overflow_to_apply_to_viewport(css_computed_values_overflow_x(&overflow_origin_computed_values.computed_values))
    let overflow_y_to_apply = dom_overflow_to_apply_to_viewport(css_computed_values_overflow_y(&overflow_origin_computed_values.computed_values))
    css_mutable_computed_values_set_overflow_x(viewport_computed_values, overflow_x_to_apply)
    css_mutable_computed_values_set_overflow_y(viewport_computed_values, overflow_y_to_apply)

    # The element from which the value is propagated must then have a used overflow value of visible.
    # FIXME: Apply this to the used values, not the computed ones.
    css_mutable_computed_values_set_overflow_x(overflow_origin_computed_values, css_data.Overflow.visible)
    css_mutable_computed_values_set_overflow_y(overflow_origin_computed_values, css_data.Overflow.visible)

# mark: Updating layout =========================================================================

## Ported from Document.cpp:1561 Web::DOM::Document::update_layout_if_needed_for_node.
pub func dom_document_update_layout_if_needed_for_node(this: DomDocument*, node: DomNode*, reason: DomUpdateLayoutReason):
    if dom_node_is_connected(node):
        dom_document_update_layout(this, reason)

## Microseconds since `start` (Core::ElapsedTimer::elapsed_time().to_microseconds()).
func dom_document_elapsed_microseconds(start: ak.MonotonicTime) -> i64:
    let now = ak.monotonic_time_now()
    return (ak.monotonic_time_nanoseconds(&now) - ak.monotonic_time_nanoseconds(&start)) // 1000

## Ported from Document.cpp:1567 Web::DOM::Document::update_layout.
pub func dom_document_update_layout(this: DomDocument*, reason: DomUpdateLayoutReason):
    let memory_tag = memory_tag_enter(MemoryTag.layout)
    defer memory_tag_leave(memory_tag)
    let navigable = dom_document_navigable(this) else return
    if html_navigable_active_document(navigable) != this:
        return

    assert(not this.m_is_running_update_layout)
    defer this.m_is_running_update_layout = false
    this.m_is_running_update_layout = true

    dom_document_update_style(this)

    if dom_document_layout_is_up_to_date(this):
        return

    # move(m_svg_roots_needing_relayout) leaves the member empty.
    var svg_roots_to_relayout = this.m_svg_roots_needing_relayout
    this.m_svg_roots_needing_relayout = ak.HashTable[LayoutSvgsvgBox*, ak.PtrTraits[LayoutSvgsvgBox]]()

    # NOTE: If this is a document hosting <template> contents, layout is unnecessary.
    if this.m_created_for_appropriate_template_contents:
        return

    let needs_layout_tree_rebuild = this.m_layout_root == none or dom_node_needs_layout_tree_update(this.dom_node()) or dom_node_child_needs_layout_tree_update(this.dom_node()) or dom_document_needs_full_layout_tree_update(this)

    # Partial SVG relayout
    if not needs_layout_tree_rebuild and not ak.hash_table_is_empty(&svg_roots_to_relayout):
        let layout_root = this.m_layout_root else trap("unreachable: the layout tree needs no rebuild")
        if not layout_node_needs_layout_update(layout_root.layout_node()):
            for svg_root in ak.hash_table_begin(&svg_roots_to_relayout):
                dom_relayout_svg_root(*svg_root)

            dom_document_invalidate_stacking_context_tree(this)
            dom_document_invalidate_display_list(this)

            dom_document_set_needs_accumulated_visual_contexts_update(this, true)
            dom_document_update_paint_and_hit_testing_properties_if_needed(this)
            dom_document_set_needs_repaint_invalidate_display_list(dom_node_document(this.dom_node()))
            return

    # Clear text blocks cache so we rebuild them on the next find action.
    if let layout_root = this.m_layout_root:
        layout_viewport_invalidate_text_blocks_cache(layout_root)

    dom_document_invalidate_display_list(this)

    let document_element = dom_document_document_element(this)
    let viewport_rect = html_navigable_viewport_rect(navigable)

    let timer = ak.monotonic_time_now()  # Core::ElapsedTimer::start_new(Core::TimerType::Precise)

    if needs_layout_tree_rebuild:
        var tree_builder: LayoutTreeBuilder = ---
        layout_tree_builder_construct(&tree_builder)
        let built = layout_tree_builder_build(&tree_builder, this.dom_node()) else trap("the tree builder built nothing")
        let layout_root = as_layout_viewport(built)
        this.m_layout_root = layout_root

        # NB: Called during layout update.
        if let root_element = document_element:
            if dom_element_unsafe_layout_node(root_element) != none:
                dom_propagate_overflow_to_viewport(root_element, layout_root)
                dom_propagate_scrollbar_width_to_viewport(root_element, layout_root)

        dom_document_set_needs_full_layout_tree_update(this, false)

        if update_layout_debug:
            ak.dbgln(f"TREEBUILD {dom_document_elapsed_microseconds(timer)} µs")

    let layout_root = this.m_layout_root else trap("no layout tree after the rebuild")

    var layout_index_counter: u32 = 0
    for layout_node in layout_node_inclusive_subtree(layout_root.layout_node()):
        if let node_with_style = as_if_layout_node_with_style(layout_node):
            layout_node_with_style_set_layout_index(node_with_style, layout_index_counter)
            layout_index_counter += 1

        layout_node_recompute_containing_block(layout_node)

        let box = as_if_layout_box(layout_node) else continue

        layout_box_clear_contained_abspos_children(box)

        if not layout_node_is_absolutely_positioned(box.layout_node()):
            continue

        if let containing_block = layout_node_containing_block(box.layout_node()):
            var closest_box_that_establishes_formatting_context: LayoutBox*? = containing_block
            while let candidate = closest_box_that_establishes_formatting_context:
                if candidate == layout_root.layout_box():
                    break
                if layout_formatting_context_formatting_context_type_created_by_box(candidate) != none:
                    break
                closest_box_that_establishes_formatting_context = layout_node_containing_block(candidate.layout_node())
            let establishing_box = closest_box_that_establishes_formatting_context else trap("VERIFY: no box establishes a formatting context")
            layout_box_add_contained_abspos_child(establishing_box, box.layout_node())

    var layout_state: LayoutState
    layout_state_ensure_capacity(&layout_state, layout_index_counter)

    let viewport = layout_root
    let viewport_state = layout_state_get_mutable(&layout_state, viewport.layout_node_with_style())
    layout_state_used_values_set_content_width(viewport_state, gfx.css_pixel_rect_width(&viewport_rect))
    layout_state_used_values_set_content_height(viewport_state, gfx.css_pixel_rect_height(&viewport_rect))

    # NB: Called during layout update.
    if let root_element = document_element:
        if let root_layout_node = dom_element_unsafe_layout_node(root_element):
            let icb_state = layout_state_get_mutable(&layout_state, as_layout_node_with_style_and_box_model_metrics(root_layout_node).layout_node_with_style())
            layout_state_used_values_set_content_width(icb_state, gfx.css_pixel_rect_width(&viewport_rect))

    var available_space: LayoutAvailableSpace = ---
    layout_available_space_construct(&available_space, layout_available_size_make_definite(gfx.css_pixel_rect_width(&viewport_rect)), layout_available_size_make_definite(gfx.css_pixel_rect_height(&viewport_rect)))

    var standalone_svg_root: LayoutSvgsvgBox*? = none
    if let first_child = tree_node_first_child(layout_root.layout_node()):
        if layout_node_is_svg_svg_box(first_child):
            standalone_svg_root = as_layout_svgsvg_box(first_child)
    if let svg_root = standalone_svg_root:
        # NOTE: If we are laying out a standalone SVG document, we give it some special treatment:
        #       The root <svg> container gets the same size as the viewport,
        #       and we call directly into the SVG layout code from here.
        let svg_containing_block = layout_node_containing_block(svg_root.layout_node()) else trap("the root svg box has no containing block")
        let containing_block_state = layout_state_get(&layout_state, svg_containing_block.layout_node_with_style())
        let content_height = layout_state_used_values_content_height(containing_block_state)
        layout_state_used_values_set_content_height(layout_state_get_mutable(&layout_state, svg_root.layout_node_with_style()), content_height)
        var identity: gfx.AffineTransform = ---
        gfx.affine_transform_construct(&identity)
        var svg_formatting_context: LayoutSvgFormattingContext = ---
        layout_svg_formatting_context_construct(&svg_formatting_context, &layout_state, LayoutMode.normal, svg_root.layout_box(), none, identity)
        layout_formatting_context_run(&svg_formatting_context.formatting_context, available_space)
    else:
        var root_formatting_context: LayoutBlockFormattingContext = ---
        layout_block_formatting_context_construct(&root_formatting_context, &layout_state, LayoutMode.normal, layout_root.layout_block_container(), none)
        # ~BlockFormattingContext() runs when the context goes out of scope.
        defer layout_block_formatting_context_destruct(&root_formatting_context)
        layout_formatting_context_run(&root_formatting_context.formatting_context, available_space)

    layout_state_commit(&layout_state, layout_root.layout_box())

    # Broadcast the current viewport rect to any new paintables, so they know whether they're visible or not.
    dom_document_inform_all_viewport_clients_about_the_current_viewport_rect(this)

    dom_document_set_needs_repaint_invalidate_display_list(dom_node_document(this.dom_node()))

    # NB: Called during layout update.
    let viewport_paintable = dom_document_unsafe_paintable(this) else trap("layout produced no viewport paintable")
    paint_viewport_paintable_assign_scroll_frames(viewport_paintable)

    dom_document_set_needs_accumulated_visual_contexts_update(this, true)
    dom_document_update_paint_and_hit_testing_properties_if_needed(this)

    # if (auto range = get_selection()->range()): a document without a browsing context has no
    # selection, and the Selection is outside the phase-1 closure (it is null until it is ported).
    if let selection = dom_document_get_selection(this):
        if let range = selection_selection_range(selection):
            paint_viewport_paintable_recompute_selection_states(viewport_paintable, range)

    # Collect elements with content-visibility: auto. This is used in the HTML event loop to avoid traversing the whole tree every time.
    var paintable_boxes_with_auto_content_visibility: ak.Vector[PaintPaintableBox*]
    for paintable in paint_paintable_subtree(viewport_paintable.paint_paintable()):
        let paintable_box = as_if_paint_paintable_box(paintable) else continue
        let dom_node = paint_paintable_dom_node(paintable_box.paint_paintable()) else continue
        if not dom_node_is_element(dom_node):
            continue
        let computed_values = paint_paintable_computed_values(paintable_box.paint_paintable())
        if css_computed_values_content_visibility(&computed_values.computed_values) == css_data.ContentVisibility.auto:
            ak.vector_append_t(&paintable_boxes_with_auto_content_visibility, paintable_box)
    paint_viewport_paintable_set_paintable_boxes_with_auto_content_visibility(viewport_paintable, paintable_boxes_with_auto_content_visibility)

    for node in layout_node_inclusive_subtree(layout_root.layout_node()):
        layout_node_reset_needs_layout_update(node)

    if update_layout_debug:
        ak.dbgln(f"LAYOUT {ak.string_view_text(dom_to_string_dom_update_layout_reason(reason))} {dom_document_elapsed_microseconds(timer)} µs")

    assert(dom_document_layout_is_up_to_date(this))

## Ported from Document.cpp:1739 Web::DOM::Document::layout_is_up_to_date.
pub func dom_document_layout_is_up_to_date(this: DomDocument*) -> bool:
    let navigable = dom_document_navigable(this) else return true
    if html_navigable_active_document(navigable) != this:
        return true
    let layout_root = this.m_layout_root else return false
    return not layout_node_needs_layout_update(layout_root.layout_node()) and not dom_node_needs_layout_tree_update(this.dom_node()) and not dom_node_child_needs_layout_tree_update(this.dom_node()) and not dom_document_needs_full_layout_tree_update(this) and ak.hash_table_is_empty(&this.m_svg_roots_needing_relayout)