Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / painting / paintable.lucb

#==============================================================================================
#
#   paintable - Painting::Paintable: the base of every node of the paint tree
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/Paintable.cpp (lines 1-398) and Paintable.h's inline members:
#       a paintable's flags taken from its layout node (positioned, floating, inline, display),
#       its DOM node, computed values, containing block and document, the stacking context it
#       belongs to, repaint invalidation, the inspector overlay, ::selection styling and
#       selection states, normalize_border_radii_data, and HitTestResult::dom_node.
#
#       Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2026, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: HitTestResult (Paintable.h) ==============================================================

## Ported from Libraries/LibWeb/Painting/Paintable.h:199 Web::Painting::HitTestResult::dom_node.
pub func paint_hit_test_result_dom_node(this: PaintHitTestResult*) -> DomNode*?:
    let paintable = this.paintable else trap("a hit test result without a paintable")
    return paint_paintable_dom_node(paintable)

## Ported from Libraries/LibWeb/Painting/Paintable.h:204 Web::Painting::HitTestResult::dom_node.
pub func paint_hit_test_result_dom_node_const(this: const PaintHitTestResult*) -> DomNode*?:
    let paintable = this.paintable else trap("a hit test result without a paintable")
    return paint_paintable_dom_node_const(paintable)

# mark: Flags (Paintable.h) ======================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.h:69 Web::Painting::Paintable::is_visible.
pub func paint_paintable_is_visible(this: PaintPaintable*) -> bool:
    let cv = &paint_paintable_computed_values(this).computed_values
    return css_computed_values_visibility(cv) == css_data.Visibility.visible and css_computed_values_opacity(cv) != 0.0

## Ported from Libraries/LibWeb/Painting/Paintable.h:74 Web::Painting::Paintable::is_positioned.
pub func paint_paintable_is_positioned(this: PaintPaintable*) -> bool:
    return this.m_positioned

## Ported from Libraries/LibWeb/Painting/Paintable.h:75 Web::Painting::Paintable::is_fixed_position.
pub func paint_paintable_is_fixed_position(this: PaintPaintable*) -> bool:
    return this.m_fixed_position

## Ported from Libraries/LibWeb/Painting/Paintable.h:76 Web::Painting::Paintable::is_sticky_position.
pub func paint_paintable_is_sticky_position(this: PaintPaintable*) -> bool:
    return this.m_sticky_position

## Ported from Libraries/LibWeb/Painting/Paintable.h:77 Web::Painting::Paintable::is_absolutely_positioned.
pub func paint_paintable_is_absolutely_positioned(this: PaintPaintable*) -> bool:
    return this.m_absolutely_positioned

## Ported from Libraries/LibWeb/Painting/Paintable.h:78 Web::Painting::Paintable::is_floating.
pub func paint_paintable_is_floating(this: PaintPaintable*) -> bool:
    return this.m_floating

## Ported from Libraries/LibWeb/Painting/Paintable.h:79 Web::Painting::Paintable::is_inline.
pub func paint_paintable_is_inline(this: PaintPaintable*) -> bool:
    return this.m_inline

## Ported from Libraries/LibWeb/Painting/Paintable.h:80 Web::Painting::Paintable::display.
pub func paint_paintable_display(this: PaintPaintable*) -> CssDisplay:
    return this.m_display

# mark: Virtual defaults (Paintable.h) ===========================================================

## Ported from Libraries/LibWeb/Painting/Paintable.h:85 Web::Painting::Paintable::paint (virtual).
pub func paint_paintable_paint_impl(this: PaintPaintable*, arg0: DisplayListRecordingContext*, arg1: PaintPhase):
    _ = this
    _ = arg0
    _ = arg1

## Ported from Libraries/LibWeb/Painting/Paintable.h:90 Web::Painting::Paintable::forms_unconnected_subtree (virtual).
pub func paint_paintable_forms_unconnected_subtree_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:94 Web::Painting::Paintable::layout_node.
pub func paint_paintable_layout_node_const(this: PaintPaintable*) -> LayoutNode*:
    return this.m_layout_node

## Ported from Libraries/LibWeb/Painting/Paintable.h:95 Web::Painting::Paintable::layout_node.
pub func paint_paintable_layout_node(this: PaintPaintable*) -> LayoutNode*:
    return this.m_layout_node

## Ported from Libraries/LibWeb/Painting/Paintable.h:111 Web::Painting::Paintable::is_navigable_container_viewport_paintable (virtual).
pub func paint_paintable_is_navigable_container_viewport_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:112 Web::Painting::Paintable::is_viewport_paintable (virtual).
pub func paint_paintable_is_viewport_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:113 Web::Painting::Paintable::is_paintable_box (virtual).
pub func paint_paintable_is_paintable_box_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:114 Web::Painting::Paintable::is_paintable_with_lines (virtual).
pub func paint_paintable_is_paintable_with_lines_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:115 Web::Painting::Paintable::is_svg_paintable (virtual).
pub func paint_paintable_is_svg_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:116 Web::Painting::Paintable::is_svg_svg_paintable (virtual).
pub func paint_paintable_is_svg_svg_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:117 Web::Painting::Paintable::is_svg_path_paintable (virtual).
pub func paint_paintable_is_svg_path_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:118 Web::Painting::Paintable::is_svg_graphics_paintable (virtual).
pub func paint_paintable_is_svg_graphics_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:119 Web::Painting::Paintable::is_text_paintable (virtual).
pub func paint_paintable_is_text_paintable_impl(this: PaintPaintable*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.h:132 Web::Painting::Paintable::selection_state.
pub func paint_paintable_selection_state(this: PaintPaintable*) -> PaintPaintableSelectionState:
    return this.m_selection_state

## Ported from Libraries/LibWeb/Painting/Paintable.h:174 Web::Painting::Paintable::paint_inspector_overlay_internal (virtual).
pub func paint_paintable_paint_inspector_overlay_internal_impl(this: PaintPaintable*, arg0: DisplayListRecordingContext*):
    _ = this
    _ = arg0

# mark: Construction and edges ===================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:26 Web::Painting::Paintable::Paintable.
pub func paint_paintable_construct(this: PaintPaintable*, layout_node: LayoutNode*):
    this.m_layout_node = layout_node
    let computed_values = &layout_node_computed_values(layout_node).computed_values
    if (layout_node_is_flex_item(layout_node) or layout_node_is_grid_item(layout_node)) and css_computed_values_z_index(computed_values) != none:
        # https://drafts.csswg.org/css-flexbox-1/#painting
        # https://drafts.csswg.org/css-grid-2/#z-order
        # Flex and grid items with z-index values other than "auto" behave as if position were "relative".
        this.m_positioned = true
    else:
        this.m_positioned = css_computed_values_position(computed_values) != css_data.Positioning.static_

    this.m_fixed_position = css_computed_values_position(computed_values) == css_data.Positioning.fixed
    this.m_sticky_position = css_computed_values_position(computed_values) == css_data.Positioning.sticky
    this.m_absolutely_positioned = css_computed_values_position(computed_values) == css_data.Positioning.absolute
    this.m_floating = layout_node_is_floating(layout_node)
    this.m_inline = layout_node_is_inline(layout_node)
    this.m_display = layout_node_display(layout_node)

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:49 Web::Painting::Paintable::finalize (override).
pub func paint_paintable_finalize_impl(base: gc.Cell*):
    let this = (PaintPaintable*)base
    gc.cell_finalize_impl(base)
    if ak.intrusive_list_node_is_in_list(&this.m_list_node):
        ak.intrusive_list_node_remove(&this.m_list_node)

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:56 Web::Painting::Paintable::visit_edges (override).
pub func paint_paintable_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (PaintPaintable*)base
    gc.cell_visit_edges_impl(base, visitor)
    tree_node_visit_edges(this, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_dom_node)
    gc.cell_visitor_visit_t_2(visitor, this.m_layout_node)
    gc.cell_visitor_visit_t(visitor, this.m_containing_block)

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:65 Web::Painting::Paintable::debug_description.
pub func paint_paintable_debug_description(this: PaintPaintable*) -> ak.String:
    var builder = ak.string_builder_new()
    ak.string_builder_append_string_view(&builder, ak.sv(this.cell().vtable.class_info.name))
    ak.string_builder_append(&builder, "(")
    let layout_description = layout_node_debug_description(paint_paintable_layout_node(this))
    ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&layout_description))
    ak.string_builder_append(&builder, ")")
    return ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")

# mark: The document, the containing block and the computed values ==============================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:70 Web::Painting::Paintable::document.
pub func paint_paintable_document_const(this: PaintPaintable*) -> DomDocument*:
    return layout_node_document(paint_paintable_layout_node(this))

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:75 Web::Painting::Paintable::document.
pub func paint_paintable_document(this: PaintPaintable*) -> DomDocument*:
    return layout_node_document(paint_paintable_layout_node(this))

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:80 Web::Painting::Paintable::containing_block.
pub func paint_paintable_containing_block(this: PaintPaintable*) -> PaintPaintableBox*?:
    # m_containing_block.ensure([&] { ... }): computed once, null included.
    if not this.m_has_containing_block:
        var containing_block: PaintPaintableBox*? = none
        if let containing_layout_box = layout_node_containing_block(this.m_layout_node):
            containing_block = layout_box_paintable_box(containing_layout_box)
        this.m_containing_block = containing_block
        this.m_has_containing_block = true
    return this.m_containing_block

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:90 Web::Painting::Paintable::computed_values.
pub func paint_paintable_computed_values(this: PaintPaintable*) -> const CssImmutableComputedValues*:
    return layout_node_computed_values(this.m_layout_node)

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:95 Web::Painting::Paintable::visible_for_hit_testing.
pub func paint_paintable_visible_for_hit_testing(this: PaintPaintable*) -> bool:
    if let node = paint_paintable_dom_node(this):
        if dom_node_is_inert(node):
            return false
    return css_computed_values_pointer_events(&paint_paintable_computed_values(this).computed_values) != css_data.PointerEvents.none_

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:102 Web::Painting::Paintable::set_dom_node.
pub func paint_paintable_set_dom_node(this: PaintPaintable*, dom_node: DomNode*?):
    this.m_dom_node = dom_node

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:107 Web::Painting::Paintable::dom_node.
pub func paint_paintable_dom_node(this: PaintPaintable*) -> DomNode*?:
    return this.m_dom_node

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:112 Web::Painting::Paintable::dom_node.
pub func paint_paintable_dom_node_const(this: PaintPaintable*) -> DomNode*?:
    return this.m_dom_node

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:117 Web::Painting::Paintable::navigable.
pub func paint_paintable_navigable(this: PaintPaintable*) -> HtmlNavigable*?:
    return dom_document_navigable(paint_paintable_document(this))

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:122 Web::Painting::Paintable::handle_mousewheel (virtual).
pub func paint_paintable_handle_mousewheel_impl(this: PaintPaintable*, arg0: gfx.CssPixelPoint, buttons: u32, modifiers: u32, wheel_delta_x: i32, wheel_delta_y: i32) -> bool:
    _ = this
    _ = arg0
    _ = buttons
    _ = modifiers
    _ = wheel_delta_x
    _ = wheel_delta_y
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:127 Web::Painting::Paintable::hit_test (virtual).
pub func paint_paintable_hit_test_impl(this: PaintPaintable*, arg0: gfx.CssPixelPoint, arg1: PaintHitTestType, callback: ak.Function1[PaintHitTestResult, TraversalDecision]) -> TraversalDecision:
    _ = this
    _ = arg0
    _ = arg1
    _ = callback
    return TraversalDecision.continue_

# mark: Stacking contexts ========================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:132 Web::Painting::Paintable::has_stacking_context.
pub func paint_paintable_has_stacking_context(this: PaintPaintable*) -> bool:
    if paint_paintable_is_paintable_box(this):
        return paint_paintable_box_stacking_context((PaintPaintableBox*)this) != none
    return false

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:139 Web::Painting::Paintable::enclosing_stacking_context.
pub func paint_paintable_enclosing_stacking_context(this: PaintPaintable*) -> PaintStackingContext*?:
    var ancestor_or_none = tree_node_parent(this)
    while let ancestor = ancestor_or_none:
        ancestor_or_none = tree_node_parent(ancestor)
        if not paint_paintable_is_paintable_box(ancestor):
            continue
        if let stacking_context = paint_paintable_box_stacking_context((PaintPaintableBox*)ancestor):
            return stacking_context
    # We should always reach the viewport's stacking context.
    trap("VERIFY_NOT_REACHED")

# mark: The inspector overlay ====================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:151 Web::Painting::Paintable::paint_inspector_overlay.
pub func paint_paintable_paint_inspector_overlay(this: PaintPaintable*, context: DisplayListRecordingContext*):
    let display_list_recorder = display_list_recording_context_display_list_recorder(context)
    var paintable_box = as_if_paint_paintable_box(this)
    if paintable_box == none:
        if let ancestor = tree_node_first_ancestor_of_type(this, &paint_paintable_box_class):
            paintable_box = (PaintPaintableBox*)ancestor

    if let box = paintable_box:
        let viewport_paintable = dom_document_paintable(paint_paintable_document(this)) else trap("the document has no paintable")
        let visual_context_tree = paint_viewport_paintable_visual_context_tree(viewport_paintable)
        let visual_context_index = paint_paintable_box_accumulated_visual_context_index(box)

        if visual_context_index != 0:
            var relevant_indices: ak.Vector[u64]
            var i = visual_context_index
            while i != 0:
                let node = display_list.accumulated_visual_context_tree_node_at(visual_context_tree, i)
                let should_keep = match node.data:
                    .scroll_data(_) => true
                    .clip_data(_) => false
                    .transform_data(_) => true
                    .perspective_data(_) => true
                    .clip_path_data(_) => false
                    .effects_data(_) => false
                if should_keep:
                    ak.vector_append_t(&relevant_indices, i)
                i = node.parent_index

            var overlay_visual_context_index: u64 = 0
            var index = ak.vector_size(&relevant_indices)
            while index > 0:
                index -= 1
                let source_visual_context_index = *ak.vector_at(&relevant_indices, index)
                let source_node = display_list.accumulated_visual_context_tree_node_at(visual_context_tree, source_visual_context_index)
                overlay_visual_context_index = display_list.accumulated_visual_context_tree_append(visual_context_tree, source_node.data, overlay_visual_context_index)

            if overlay_visual_context_index != 0:
                display_list.display_list_recorder_set_accumulated_visual_context(display_list_recorder, overlay_visual_context_index)

    paint_paintable_paint_inspector_overlay_internal(this, context)
    display_list.display_list_recorder_set_accumulated_visual_context(display_list_recorder, 0)

# mark: Repainting ================================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:187 Web::Painting::Paintable::set_needs_repaint (virtual).
pub func paint_paintable_set_needs_repaint_impl(this: PaintPaintable*, should_invalidate_display_list: InvalidateDisplayList):
    if should_invalidate_display_list == InvalidateDisplayList.yes:
        if let containing_block = paint_paintable_containing_block(this):
            paint_paintable_box_invalidate_paint_cache(containing_block)
    dom_document_set_needs_repaint_t(paint_paintable_document(this), should_invalidate_display_list)

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:196 Web::Painting::Paintable::box_type_agnostic_position.
pub func paint_paintable_box_type_agnostic_position(this: PaintPaintable*) -> gfx.CssPixelPoint:
    if paint_paintable_is_paintable_box(this):
        return paint_paintable_box_absolute_position((PaintPaintableBox*)this)

    assert(paint_paintable_is_inline(this))

    var position = gfx.CssPixelPoint()
    if let block = paint_paintable_containing_block(this):
        if is_paint_paintable_with_lines(block):
            let fragments = paint_paintable_with_lines_fragments((PaintPaintableWithLines*)block)
            if not ak.vector_is_empty(fragments):
                let rect = paint_paintable_fragment_absolute_rect(ak.vector_at(fragments, 0))
                position = gfx.css_pixel_rect_location(&rect)

    return position

# mark: Border radii ==============================================================================

## `min(f, numerator / denominator)` for a CSSPixelFraction (AK::min: `b < a ? b : a`).
func paint_min_fraction(f: gfx.CssPixelFraction, numerator: gfx.CssPixels, denominator: gfx.CssPixels) -> gfx.CssPixelFraction:
    let other = numerator.div(denominator)
    if gfx.css_pixel_fraction_cmp_css_pixel_fraction(&other, f) < 0:
        return other
    return f

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:213 Web::Painting::normalize_border_radii_data.
pub func paint_normalize_border_radii_data(node: LayoutNode*, border_rect: gfx.CssPixelRect, reference_rect: gfx.CssPixelRect, top_left_radius: CssBorderRadiusData, top_right_radius: CssBorderRadiusData, bottom_right_radius: CssBorderRadiusData, bottom_left_radius: CssBorderRadiusData) -> display_list.BorderRadiiData:
    let reference_width = gfx.css_pixel_rect_width(&reference_rect)
    let reference_height = gfx.css_pixel_rect_height(&reference_rect)
    var radii_px = display_list.BorderRadiiData(
        top_left = display_list.BorderRadiusData(
            horizontal_radius = css_percentage_or_to_px(&top_left_radius.horizontal_radius.percentage_or, node, reference_width),
            vertical_radius = css_percentage_or_to_px(&top_left_radius.vertical_radius.percentage_or, node, reference_height)),
        top_right = display_list.BorderRadiusData(horizontal_radius = css_percentage_or_to_px(&top_right_radius.horizontal_radius.percentage_or, node, reference_width), vertical_radius = css_percentage_or_to_px(&top_right_radius.vertical_radius.percentage_or, node, reference_height)),
        bottom_right = display_list.BorderRadiusData(horizontal_radius = css_percentage_or_to_px(&bottom_right_radius.horizontal_radius.percentage_or, node, reference_width), vertical_radius = css_percentage_or_to_px(&bottom_right_radius.vertical_radius.percentage_or, node, reference_height)),
        bottom_left = display_list.BorderRadiusData(horizontal_radius = css_percentage_or_to_px(&bottom_left_radius.horizontal_radius.percentage_or, node, reference_width), vertical_radius = css_percentage_or_to_px(&bottom_left_radius.vertical_radius.percentage_or, node, reference_height)),
    )

    # Scale overlapping curves according to https://www.w3.org/TR/css-backgrounds-3/#corner-overlap
    # Let f = min(Li/Si), where i ∈ {top, right, bottom, left},
    # Si is the sum of the two corresponding radii of the corners on side i,
    # and Ltop = Lbottom = the width of the box, and Lleft = Lright = the height of the box.
    #
    # NOTE: We iterate twice as a form of iterative refinement. A single scaling pass using
    # fixed-point arithmetic can result in small rounding errors, causing the scaled radii to
    # still slightly overflow the box dimensions. A second pass corrects this remaining error.
    let border_width = gfx.css_pixel_rect_width(&border_rect)
    let border_height = gfx.css_pixel_rect_height(&border_rect)
    for iteration in 0..<2:
        _ = iteration
        let s_top = radii_px.top_left.horizontal_radius.add(radii_px.top_right.horizontal_radius)
        let s_right = radii_px.top_right.vertical_radius.add(radii_px.bottom_right.vertical_radius)
        let s_bottom = radii_px.bottom_right.horizontal_radius.add(radii_px.bottom_left.horizontal_radius)
        let s_left = radii_px.bottom_left.vertical_radius.add(radii_px.top_left.vertical_radius)

        var f: gfx.CssPixelFraction = ---
        gfx.css_pixel_fraction_construct_int(&f, 1)
        if s_top.gt(border_width):
            f = paint_min_fraction(f, border_width, s_top)
        if s_right.gt(border_height):
            f = paint_min_fraction(f, border_height, s_right)
        if s_bottom.gt(border_width):
            f = paint_min_fraction(f, border_width, s_bottom)
        if s_left.gt(border_height):
            f = paint_min_fraction(f, border_height, s_left)

        # If f is 1 or more, the radii fit perfectly and no more scaling is needed
        if gfx.css_pixel_fraction_cmp_int(&f, 1) >= 0:
            break

        let corners: (display_list.BorderRadiusData*)[4] = [&radii_px.top_left, &radii_px.top_right, &radii_px.bottom_right, &radii_px.bottom_left]

        for corner in corners:
            gfx.css_pixels_mul_assign_css_pixel_fraction(&corner.horizontal_radius, f)
            gfx.css_pixels_mul_assign_css_pixel_fraction(&corner.vertical_radius, f)

    return radii_px

# mark: ::selection styling ======================================================================

## The `style_from_element` lambda of Paintable::selection_style.
func paint_paintable_selection_style_from_element(element: DomElement*) -> PaintPaintableSelectionStyle?:
    let element_layout_node = dom_element_layout_node(element) else return none

    let computed_selection_style = dom_element_computed_properties(element, css_data.PseudoElement.selection) else return none

    let context = css_color_resolution_context_for_layout_node_with_style(element_layout_node)

    var style = PaintPaintableSelectionStyle()
    style.background_color = css_computed_properties_color(computed_selection_style, css_data.PropertyId.background_color, context)

    # Only use text color if it was explicitly set in the ::selection rule, not inherited.
    if not css_computed_properties_is_property_inherited(computed_selection_style, css_data.PropertyId.color):
        style.text_color = css_computed_properties_color(computed_selection_style, css_data.PropertyId.color, context)

    # Only use text-shadow if it was explicitly set in the ::selection rule, not inherited.
    if not css_computed_properties_is_property_inherited(computed_selection_style, css_data.PropertyId.text_shadow):
        var css_shadows = css_computed_properties_text_shadow(computed_selection_style, element_layout_node.layout_node())
        var shadows: ak.Vector[PaintShadowData]
        ak.vector_ensure_capacity(&shadows, ak.vector_size(&css_shadows))
        for shadow in ak.vector_span(&css_shadows):
            ak.vector_unchecked_append_u(&shadows, paint_shadow_data_from_css(shadow, element_layout_node.layout_node()))
        style.text_shadow = shadows

    # Only use text-decoration if it was explicitly set in the ::selection rule, not inherited.
    if not css_computed_properties_is_property_inherited(computed_selection_style, css_data.PropertyId.text_decoration_line):
        style.text_decoration = PaintPaintableTextDecorationStyle(
            line = css_computed_properties_text_decoration_line(computed_selection_style),
            style = css_computed_properties_text_decoration_style(computed_selection_style),
            color = css_computed_properties_color(computed_selection_style, css_data.PropertyId.text_decoration_color, context),
        )

    # Only return a style if there's a meaningful customization. This allows us to continue checking shadow hosts
    # when the current element only has UA default styles.
    if not paint_paintable_selection_style_has_styling(&style):
        return none

    return style

## Ported from Libraries/LibWeb/Painting/Paintable.h:154 Web::Painting::Paintable::SelectionStyle::has_styling.
func paint_paintable_selection_style_has_styling(this: const PaintPaintableSelectionStyle*) -> bool:
    return gfx.color_alpha(&this.background_color) > 0 or this.text_color != none or this.text_shadow != none or this.text_decoration != none

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:271 Web::Painting::Paintable::selection_style.
# https://drafts.csswg.org/css-pseudo-4/#highlight-styling
# FIXME: Support additional ::selection properties: text-underline-offset, text-underline-position, stroke-color,
#        fill-color, stroke-width, and CSS custom properties.
pub func paint_paintable_selection_style(this: PaintPaintable*) -> PaintPaintableSelectionStyle:
    let color_scheme = css_computed_values_color_scheme(&paint_paintable_computed_values(this).computed_values)
    let default_style = PaintPaintableSelectionStyle(background_color = css_system_color_highlight(color_scheme))

    # For text nodes, check the parent element since text nodes don't have computed properties.
    let node = paint_paintable_dom_node(this) else return default_style

    var element_or_none = as_if_dom_element(node)
    if element_or_none == none:
        element_or_none = dom_node_parent_element(node)
    let element = element_or_none else return default_style

    # Check the element itself.
    if let style = paint_paintable_selection_style_from_element(element):
        return style

    # If inside a shadow tree, check the shadow host. This enables ::selection styling on elements like <input> to
    # apply to text rendered inside their shadow DOM.
    if let shadow_root = dom_node_containing_shadow_root(element.dom_node()):
        if dom_shadow_root_is_user_agent_internal(shadow_root):
            if let host = dom_document_fragment_host(shadow_root.dom_document_fragment()):
                if let style = paint_paintable_selection_style_from_element(host):
                    return style

    return default_style

# mark: Selection states ==========================================================================

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:345 Web::Painting::Paintable::set_selection_state.
pub func paint_paintable_set_selection_state(this: PaintPaintable*, state: PaintPaintableSelectionState):
    if this.m_selection_state == state:
        return
    this.m_selection_state = state
    if let box = as_if_paint_paintable_box(this):
        paint_paintable_box_invalidate_paint_cache(box)
    elif let containing_block = paint_paintable_containing_block(this):
        paint_paintable_box_invalidate_paint_cache(containing_block)
        let containing_block_layout_node = paint_paintable_layout_node(containing_block.paint_paintable())
        var ancestor_or_none = layout_node_parent(paint_paintable_layout_node(this))
        while let ancestor = ancestor_or_none:
            if ancestor.layout_node() == containing_block_layout_node:
                break
            for paintable in ak.intrusive_list_begin(layout_node_paintables(ancestor.layout_node())):
                if let ancestor_box = as_if_paint_paintable_box(paintable):
                    paint_paintable_box_invalidate_paint_cache(ancestor_box)
            ancestor_or_none = layout_node_parent(ancestor.layout_node())

## Ported from Libraries/LibWeb/Painting/Paintable.cpp:362 Web::Painting::Paintable::scroll_ancestor_to_offset_into_view.
pub func paint_paintable_scroll_ancestor_to_offset_into_view(this: PaintPaintable*, offset: usize):
    # Walk up to find the containing PaintableWithLines.
    var paintable_with_lines_or_none: PaintPaintableWithLines*? = none
    var ancestor_or_none: PaintPaintable*? = this
    while let ancestor = ancestor_or_none:
        paintable_with_lines_or_none = as_if_paint_paintable_with_lines(ancestor)
        if paintable_with_lines_or_none != none:
            break
        ancestor_or_none = tree_node_parent(ancestor)
    let paintable_with_lines = paintable_with_lines_or_none else return

    # Find the fragment containing the offset and compute a cursor rect.
    let fragments = paint_paintable_with_lines_fragments(paintable_with_lines)
    for fragment in &ak.vector_span(fragments):
        if paint_paintable_fragment_paintable(fragment) != this:
            continue
        if offset < paint_paintable_fragment_start_offset(fragment) or offset > paint_paintable_fragment_start_offset(fragment) + paint_paintable_fragment_length_in_code_units(fragment):
            continue

        let cursor_rect = paint_paintable_fragment_range_rect(fragment, PaintPaintableSelectionState.start_and_end, offset, offset)

        # Walk up the containing block chain to find the nearest scrollable ancestor.
        var block_or_none = paint_paintable_containing_block(this)
        while let block = block_or_none:
            if paint_paintable_box_has_scrollable_overflow(block):
                paint_paintable_box_scroll_into_view(block, cursor_rect)
                break
            block_or_none = paint_paintable_containing_block(block.paint_paintable())
        return