Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / viewport.lucb

#==============================================================================================
#
#   viewport - Layout::Viewport: the root of the layout tree, the document's box
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/Viewport.cpp (lines 1-104) and Viewport.h: the constructor,
#       visit_edges, the ViewportPaintable, and the text blocks find-in-page searches (the
#       rendered text of each run of text nodes between boxes, with the offset of each node).
#
#       Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:20 Web::Layout::Viewport::Viewport.
pub func layout_viewport_construct(this: LayoutViewport*, document: DomDocument*, style: CssComputedProperties*):
    layout_block_container_construct_css_computed_properties(this.layout_block_container(), document, document.dom_node(), style)

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:27 Web::Layout::Viewport::dom_node.
pub func layout_viewport_dom_node(this: LayoutViewport*) -> DomDocument*:
    let dom_node = layout_node_dom_node(this.layout_node()) else trap("VERIFY: the viewport has a document")
    return (DomDocument*)dom_node

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:32 Web::Layout::Viewport::create_paintable (override).
pub func layout_viewport_create_paintable_impl(base: LayoutNode*) -> PaintPaintable*?:
    return paint_viewport_paintable_create((LayoutViewport*)base).paint_paintable()

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:37 Web::Layout::Viewport::visit_edges (override).
pub func layout_viewport_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (LayoutViewport*)base
    layout_box_visit_edges_impl(base, visitor)
    let text_blocks = this.m_text_blocks else return

    for text_block in ak.vector_span_const(&text_blocks):
        for text_position in ak.vector_span_const(&text_block.positions):
            gc.cell_visitor_visit_t_2(visitor, text_position.dom_node)

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:49 Web::Layout::Viewport::text_blocks.
pub func layout_viewport_text_blocks(this: LayoutViewport*) -> ak.Vector[LayoutViewportTextBlock]:
    if this.m_text_blocks == none:
        layout_viewport_update_text_blocks(this)

    # C++ answers a const reference; the copy shares the vector's buffer, and callers only read it.
    return this.m_text_blocks else trap("unreachable: the text blocks were just updated")

## Ported from Libraries/LibWeb/Layout/Viewport.h:31 Web::Layout::Viewport::invalidate_text_blocks_cache.
pub func layout_viewport_invalidate_text_blocks_cache(this: LayoutViewport*):
    this.m_text_blocks = none

## Ported from Libraries/LibWeb/Layout/Viewport.cpp:57 Web::Layout::Viewport::update_text_blocks.
func layout_viewport_update_text_blocks(this: LayoutViewport*):
    var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
    var current_start_position: usize = 0
    var text_positions: ak.Vector[LayoutViewportTextPosition]
    var text_blocks: ak.Vector[LayoutViewportTextBlock]

    for layout_node in layout_node_inclusive_subtree(this.layout_node()):
        let display = layout_node_display(layout_node)
        if css_display_is_none(&display):
            continue
        let first_paintable = layout_node_first_paintable(layout_node) else continue
        if not paint_paintable_is_visible(first_paintable):
            continue

        if layout_node_is_box(layout_node) or layout_node_is_generated_for_pseudo_element(layout_node):
            if not ak.string_builder_is_empty(&builder):
                ak.vector_append_t(&text_blocks, LayoutViewportTextBlock(text = ak.string_builder_to_utf16_string(&builder), positions = ak.vector_clone(&text_positions)))
                current_start_position = 0
                ak.vector_clear_with_capacity(&text_positions)
                ak.string_builder_clear(&builder)
            continue

        if let text_node = as_if_layout_text_node(layout_node):
            # https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees
            # When a node is inert:
            # - The user agent should ignore the node for the purposes of find-in-page.
            let dom_node = layout_text_node_dom_node(text_node)
            if not dom_node_is_inert(dom_node.dom_node()):
                if ak.vector_is_empty(&text_positions):
                    ak.vector_append_t(&text_positions, LayoutViewportTextPosition(dom_node = dom_node, start_offset = 0))
                else:
                    ak.vector_append_t(&text_positions, LayoutViewportTextPosition(dom_node = dom_node, start_offset = current_start_position))

                let current_node_text = layout_text_node_text_for_rendering(text_node)
                current_start_position += ak.utf16_string_base_length_in_code_units(&current_node_text.utf16_string_base)
                ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_utf16_view(&current_node_text.utf16_string_base))

    if not ak.string_builder_is_empty(&builder):
        ak.vector_append_t(&text_blocks, LayoutViewportTextBlock(text = ak.string_builder_to_utf16_string(&builder), positions = text_positions))

    this.m_text_blocks = text_blocks

## Ported from Libraries/LibWeb/Layout/Viewport.h:42 Web::Layout::Viewport::is_viewport (override).
pub func layout_viewport_is_viewport_impl(base: LayoutNode*) -> bool:
    _ = base
    return true