Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / page / event_handler_1.lucb

#==============================================================================================
#
#   event_handler_1 - Web::EventHandler: finding the node an event is dispatched to, cursors,
#   mouse event offsets, nested navigables, user selection, auto scrolling and construction
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Page/EventHandler.cpp lines 60-566 (the file's static helpers
#       through set_user_selection, process_auto_scroll, update_mouse_selection,
#       apply_mouse_selection, light_dismiss_activities, set_node_and_ancestors_being_activated,
#       the constructor and paint_root).
#
#       Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2026, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: The node an event is dispatched to ======================================================

## Ported from EventHandler.cpp:66 Web::dom_node_for_event_dispatch.
func dom_node_for_event_dispatch(paintable: PaintPaintable*) -> DomNode*?:
    if let node = paint_paintable_dom_node(paintable):
        return node
    var parent = tree_node_parent(paintable)
    while let current = parent:
        if let node = paint_paintable_dom_node(current):
            return node
        parent = tree_node_parent(current)
    return none

## Ported from EventHandler.cpp:79 Web::parent_element_for_event_dispatch.
func parent_element_for_event_dispatch(paintable: PaintPaintable*, node: (DomNode*?)*, layout_node: (LayoutNode*?)*) -> bool:
    *layout_node = paint_paintable_layout_node(paintable)
    let first_layout_node = *layout_node else trap("unreachable")
    if layout_node_is_generated_for_backdrop_pseudo_element(first_layout_node) or layout_node_is_generated_for_after_pseudo_element(first_layout_node) or layout_node_is_generated_for_before_pseudo_element(first_layout_node):
        let generator = layout_node_pseudo_element_generator(first_layout_node) else trap("no pseudo-element generator")
        *node = generator.dom_node()
        *layout_node = (LayoutNode*?)dom_element_layout_node(generator)

    var current_ancestor_node = *node
    while let ancestor = current_ancestor_node:
        if let form_associated_element = as_if_form_associated_element(ancestor):
            if not html_form_associated_element_enabled(form_associated_element):
                return false
        current_ancestor_node = tree_node_parent(ancestor)

    while true:
        let current_layout_node = *layout_node else break
        let current_node = *node else break
        if dom_node_is_element(current_node):
            break
        let parent = layout_node_parent(current_layout_node) else break
        *layout_node = (LayoutNode*?)parent
        if layout_node_is_anonymous(parent.layout_node()):
            continue
        *node = layout_node_dom_node(parent.layout_node())
    return *node != none and *layout_node != none

# mark: Cursors =================================================================================

## Ported from EventHandler.cpp:106 Web::css_to_gfx_cursor.
func css_to_gfx_cursor(css_cursor: css_data.CursorPredefined) -> gfx.Cursor:
    match css_cursor:
        .crosshair, .cell:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.crosshair)
        .grab:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.open_hand)
        .grabbing:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.drag)
        .pointer:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.hand)
        .help:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.help)
        .none_:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.hidden)
        .not_allowed:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.disallowed)
        .text, .vertical_text:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.i_beam)
        .move, .all_scroll:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.move)
        .progress, .wait:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.wait)
        .col_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_column)
        .e_resize, .w_resize, .ew_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_horizontal)
        .row_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_row)
        .n_resize, .s_resize, .ns_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_vertical)
        .ne_resize, .sw_resize, .nesw_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_diagonal_bltr)
        .nw_resize, .se_resize, .nwse_resize:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.resize_diagonal_tlbr)
        .zoom_in, .zoom_out:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.zoom)
        .default:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.arrow)
        _:
            # ContextMenu, Alias, Copy, NoDrop:
            # FIXME: No corresponding GFX Standard Cursor, fallthrough to None
            # Auto, and anything else:
            return gfx.Cursor.standard_cursor(gfx.StandardCursor.none_)

## Ported from EventHandler.cpp:169 Web::resolve_cursor.
func resolve_cursor(layout_node: LayoutNodeWithStyle*, cursor_data: ak.Vector[CssCursorData], auto_cursor: gfx.StandardCursor) -> gfx.Cursor:
    for cursor in &ak.vector_span_const(&cursor_data):
        var result: gfx.Cursor? = none
        match *cursor:
            .cursor_predefined(css_cursor):
                if css_cursor == css_data.CursorPredefined.auto:
                    result = gfx.Cursor.standard_cursor(auto_cursor)
                else:
                    result = css_to_gfx_cursor(css_cursor)
            .css_cursor_style_value(cursor_style_value):
                if let image_cursor = css_cursor_style_value_make_image_cursor(cursor_style_value, layout_node):
                    result = gfx.Cursor.image_cursor(image_cursor)
        if let resolved = result:
            return resolved

    # We should never get here
    return gfx.Cursor.standard_cursor(gfx.StandardCursor.none_)

# mark: Mouse event offsets and nested navigables ===============================================

## Ported from EventHandler.cpp:192 Web::compute_mouse_event_offset.
# https://drafts.csswg.org/cssom-view/#dom-mouseevent-offsetx
func compute_mouse_event_offset(position: gfx.CssPixelPoint, paintable: PaintPaintable*) -> gfx.CssPixelPoint:
    # If the event’s dispatch flag is set,
    # FIXME: Is this guaranteed to be dispatched?

    # return the x-coordinate of the position where the event occurred,
    # ignoring the transforms that apply to the element and its ancestors,
    var offset_position = position
    if let paintable_box = as_if_paint_paintable_box(paintable):
        offset_position = paint_paintable_box_inverse_transform_point(paintable_box, position)
    elif let containing_block = paint_paintable_containing_block(paintable):
        offset_position = paint_paintable_box_inverse_transform_point(containing_block, position)

    # relative to the origin of the padding edge of the target node
    let top_left_of_layout_node = paint_paintable_box_type_agnostic_position(paintable)
    let offset = gfx.css_pixel_point_sub(&offset_position, top_left_of_layout_node)

    # and terminate these steps.
    return offset

## Ported from EventHandler.cpp:214 Web::dispatch_event_to_nested_navigable.
func dispatch_event_to_nested_navigable(paintable: PaintPaintable*, viewport_position: gfx.CssPixelPoint, dispatch: ak.Function2[EventHandler*, gfx.CssPixelPoint, EventResult]) -> EventResult?:
    let node = dom_node_for_event_dispatch(paintable) else return none

    if let navigable_paintable = as_if_paint_navigable_container_viewport_paintable(paintable):
        let paintable_box = navigable_paintable.paint_paintable_box()
        let local_position = paint_paintable_box_transform_to_local_coordinates(paintable_box, viewport_position)
        let absolute_rect = paint_paintable_box_absolute_rect(paintable_box)
        let position = gfx.css_pixel_point_sub(&local_position, gfx.css_pixel_rect_location(&absolute_rect))
        if let content_navigable = html_navigable_container_content_navigable(as_if_html_navigable_container(node) else trap("not a navigable container")):
            return ak.function2_call(&dispatch, html_navigable_event_handler(content_navigable), position)
        return EventResult.dropped

    return none

# mark: Selections ==============================================================================

## Ported from EventHandler.cpp:232 Web::find_paragraph_range.
# Find paragraph boundaries for triple-click selection. A paragraph is delimited by block nodes or <br> elements.
func find_paragraph_range(text_node: DomText*, offset: u32) -> DomRange*:
    var start_node: DomNode*? = text_node.dom_node()
    var start_offset = offset
    var end_node: DomNode*? = text_node.dom_node()
    var end_offset = offset

    # Walk backwards to find the paragraph start (a block boundary point).
    if not editing_is_block_start_point(DomBoundaryPoint(node = start_node else trap("unreachable"), offset = start_offset)):
        while true:
            let current = start_node else trap("unreachable")
            if start_offset == 0:
                start_offset = (u32)tree_node_index(current)
                start_node = tree_node_parent(current)
            else:
                start_offset -= 1
            let next = start_node else break
            if editing_is_block_boundary_point(DomBoundaryPoint(node = next, offset = start_offset)):
                break

    # Walk forwards to find the paragraph end (a block boundary point).
    if not editing_is_block_end_point(DomBoundaryPoint(node = end_node else trap("unreachable"), offset = end_offset)):
        while true:
            let current = end_node else trap("unreachable")
            if (usize)end_offset == dom_node_length(current):
                end_offset = (u32)tree_node_index(current) + 1
                end_node = tree_node_parent(current)
            else:
                end_offset += 1
            let next = end_node else break
            if editing_is_block_boundary_point(DomBoundaryPoint(node = next, offset = end_offset)):
                break

    # Fallback if we couldn't find boundaries.
    if start_node == none:
        start_node = text_node.dom_node()
        start_offset = 0
    if end_node == none:
        end_node = text_node.dom_node()
        end_offset = (u32)dom_node_length(text_node.dom_node())

    return dom_range_create_dom_node(start_node else trap("unreachable"), start_offset, end_node else trap("unreachable"), end_offset)

## Whether `node` is an element whose layout node's used user-select is `value`.
func event_handler_node_user_select_is(node: DomNode*, value: css_data.UserSelect) -> bool:
    let layout_node = dom_node_layout_node(node) else trap("no layout node")
    return layout_node_user_select_used_value(layout_node) == value

## The used user-select of `node`'s layout node.
func event_handler_node_user_select(node: DomNode*) -> css_data.UserSelect:
    let layout_node = dom_node_layout_node(node) else trap("no layout node")
    return layout_node_user_select_used_value(layout_node)

## Ported from EventHandler.cpp:277 Web::set_user_selection.
# https://drafts.csswg.org/css-ui/#propdef-user-select
func set_user_selection(a_anchor_node: DomNode*?, a_anchor_offset: usize, a_focus_node: DomNode*?, a_focus_offset: usize, selection: SelectionSelection*?, a_user_select: css_data.UserSelect):
    var anchor_node = a_anchor_node else trap("no anchor node")
    var anchor_offset = a_anchor_offset
    var focus_node = a_focus_node else trap("no focus node")
    var focus_offset = a_focus_offset
    var user_select = a_user_select

    # https://drafts.csswg.org/css-ui/#valdef-user-select-contain
    # NB: This is clamping the focus node to any node with user-select: contain that stands between it and the anchor node.
    if focus_node != anchor_node:
        # UAs must not allow a selection which is started in this element to be extended outside of this element.
        var potential_contain_node = anchor_node

        # NB: The way we do this is searching up the tree from the anchor, to find 'this element', i.e. its nearest
        #     contain ancestor. We stop the search early when we reach an element that contains both the anchor and the
        #     focus node, as this means they are inside the same contain element, or not in a contain element at all.
        #     This takes care of the "selection trying to escape from a contain" case.
        while (not dom_node_is_element(potential_contain_node) or not event_handler_node_user_select_is(potential_contain_node, css_data.UserSelect.contain)) and tree_node_parent(potential_contain_node) != none and not tree_node_is_inclusive_ancestor_of(potential_contain_node, focus_node):
            potential_contain_node = tree_node_parent(potential_contain_node) else trap("unreachable")

        if event_handler_node_user_select_is(potential_contain_node, css_data.UserSelect.contain) and not tree_node_is_inclusive_ancestor_of(potential_contain_node, focus_node):
            if tree_node_is_before(focus_node, potential_contain_node):
                focus_offset = 0
            else:
                focus_offset = dom_node_length(potential_contain_node)
            focus_node = potential_contain_node
            # NB: Prevents this from being handled again further down
            user_select = css_data.UserSelect.contain
        else:
            # A selection started outside of this element must not end in this element. If the user attempts to create
            # such a selection, the UA must instead end the selection range at the element boundary.

            # NB: This branch takes care of the "selection trying to intrude into a contain" case. This is done by
            #     searching up the tree from the focus node, to see if there is a contain element between it and the
            #     common ancestor that also includes the anchor. We stop once reaching target_node, which is the common
            #     ancestor identified in step 1. If target_node wasn't a common ancestor, we would not be here.
            let target_node = potential_contain_node
            potential_contain_node = focus_node
            while (not dom_node_is_element(potential_contain_node) or not event_handler_node_user_select_is(potential_contain_node, css_data.UserSelect.contain)) and tree_node_parent(potential_contain_node) != none and potential_contain_node != target_node:
                potential_contain_node = tree_node_parent(potential_contain_node) else trap("unreachable")
            if event_handler_node_user_select_is(potential_contain_node, css_data.UserSelect.contain) and not tree_node_is_inclusive_ancestor_of(potential_contain_node, anchor_node):
                if tree_node_is_before(potential_contain_node, anchor_node):
                    focus_node = tree_node_next_in_pre_order(potential_contain_node) else trap("no next node")
                    while tree_node_is_inclusive_ancestor_of(potential_contain_node, focus_node):
                        focus_node = tree_node_next_in_pre_order(focus_node) else trap("no next node")
                    focus_offset = 0
                else:
                    focus_node = tree_node_previous_in_pre_order(potential_contain_node) else trap("no previous node")
                    while tree_node_is_inclusive_ancestor_of(potential_contain_node, focus_node):
                        focus_node = tree_node_previous_in_pre_order(focus_node) else trap("no previous node")
                    focus_offset = dom_node_length(focus_node)
                # NB: Prevents this from being handled again further down
                user_select = css_data.UserSelect.contain

    match user_select:
        .none_:
            # https://drafts.csswg.org/css-ui/#valdef-user-select-none

            # The UA must not allow selections to be started in this element.
            if anchor_node == focus_node:
                return

            # A selection started outside of this element must not end in this element. If the user attempts to create such
            # a selection, the UA must instead end the selection range at the element boundary.
            while true:
                let parent = tree_node_parent(focus_node) else break
                if event_handler_node_user_select(parent) != css_data.UserSelect.none_:
                    break
                focus_node = parent
            if tree_node_is_before(focus_node, anchor_node):
                let none_element = focus_node
                while true:
                    focus_node = tree_node_next_in_pre_order(focus_node) else trap("no next node")
                    if not tree_node_is_inclusive_ancestor_of(none_element, focus_node):
                        break
                focus_offset = 0
            else:
                focus_node = tree_node_previous_in_pre_order(focus_node) else trap("no previous node")
                focus_offset = dom_node_length(focus_node)
        .all:
            # https://drafts.csswg.org/css-ui/#valdef-user-select-all

            # The content of the element must be selected atomically: If a selection would contain part of the element,
            # then the selection must contain the entire element including all its descendants. If the element is selected
            # and the used value of 'user-select' on its parent is 'all', then the parent must be included in the selection,
            # recursively.
            while true:
                let parent = tree_node_parent(focus_node) else break
                if event_handler_node_user_select(parent) != css_data.UserSelect.all:
                    break
                if anchor_node == focus_node:
                    anchor_node = parent
                focus_node = parent

            if focus_node == anchor_node:
                if anchor_offset > focus_offset:
                    anchor_offset = dom_node_length(focus_node)
                    focus_offset = 0
                else:
                    anchor_offset = 0
                    focus_offset = dom_node_length(focus_node)
            elif tree_node_is_before(focus_node, anchor_node):
                focus_offset = 0
            else:
                focus_offset = dom_node_length(focus_node)
        .contain:
            # NB: This is handled at the start of this function
            _ = 0
        .text:
            # https://drafts.csswg.org/css-ui/#valdef-user-select-text
            # The element imposes no constraint on the selection.
            _ = 0
        _:
            # Auto:
            trap("VERIFY_NOT_REACHED")

    selection_selection_set_base_and_extent(selection else trap("no selection"), anchor_node, (u32)anchor_offset, focus_node, (u32)focus_offset) catch:
        # (void): the C++ drops the result.
        _ = idl_take_pending_exception()
        recover ()

# mark: Auto scrolling and mouse selection ======================================================

## Ported from EventHandler.cpp:407 Web::EventHandler::process_auto_scroll.
pub func event_handler_process_auto_scroll(this: EventHandler*):
    if let auto_scroll_handler = this.m_auto_scroll_handler:
        auto_scroll_handler_perform_tick(auto_scroll_handler)
    if let middle_button_scroll_handler = this.m_middle_button_scroll_handler:
        middle_button_scroll_handler_perform_tick(middle_button_scroll_handler)

## Ported from EventHandler.cpp:415 Web::EventHandler::update_mouse_selection.
func event_handler_update_mouse_selection(this: EventHandler*, visual_viewport_position: gfx.CssPixelPoint):
    if this.m_selection_mode == EventHandlerSelectionMode.none_:
        return

    var clamped_position = visual_viewport_position
    if let auto_scroll_handler = this.m_auto_scroll_handler:
        clamped_position = auto_scroll_handler_process(auto_scroll_handler, visual_viewport_position)
    event_handler_apply_mouse_selection(this, clamped_position)

## Ported from EventHandler.cpp:426 Web::EventHandler::apply_mouse_selection.
func event_handler_apply_mouse_selection(this: EventHandler*, visual_viewport_position: gfx.CssPixelPoint):
    let paint_root = event_handler_paint_root(this) else trap("no paint root")
    let hit = paint_paintable_box_hit_test(paint_root, visual_viewport_position, PaintHitTestType.text_cursor) else return
    let hit_paintable = hit.paintable else trap("no hit paintable")
    let hit_dom_node = paint_paintable_dom_node(hit_paintable) else return

    let document = html_navigable_active_document(this.m_navigable) else trap("no active document")
    var focus_node: DomNode* = hit_dom_node
    var focus_index = hit.index_in_node
    var anchor_node: DomNode*? = none
    var anchor_offset: usize? = none

    # In word selection mode, extend selection by whole words.
    if this.m_selection_mode == EventHandlerSelectionMode.word and this.m_selection_origin != none and is_dom_text(focus_node):
        let selection_origin = this.m_selection_origin else trap("unreachable")
        let hit_text_node = as_dom_text(focus_node)
        let segmenter = dom_character_data_word_segmenter(hit_text_node.dom_character_data())
        let word_start = (usize)(web_unicode.segmenter_previous_boundary(segmenter, focus_index, web_unicode.SegmenterInclusive.yes) else 0)
        let word_end = (usize)(web_unicode.segmenter_next_boundary(segmenter, focus_index, web_unicode.SegmenterInclusive.no) else (u64)dom_node_length(focus_node))

        # Determine cursor position relative to anchor.
        let range = selection_origin.dom_abstract_range()
        let position: i16? = dom_range_compare_point(selection_origin, focus_node, (u32)focus_index) catch:
            _ = idl_take_pending_exception()
            recover none
        if let value = position:
            if value < 0:
                # Cursor is before anchor: select from anchor end to current word start.
                anchor_node = dom_abstract_range_end_container(range)
                anchor_offset = (usize)dom_abstract_range_end_offset(range)
                focus_index = word_start
            elif value > 0:
                # Cursor is after anchor: select from anchor start to current word end.
                anchor_node = dom_abstract_range_start_container(range)
                anchor_offset = (usize)dom_abstract_range_start_offset(range)
                focus_index = word_end
            else:
                # Cursor is within anchor: keep original word selected.
                anchor_node = dom_abstract_range_start_container(range)
                anchor_offset = (usize)dom_abstract_range_start_offset(range)
                focus_index = (usize)dom_abstract_range_end_offset(range)

    # In paragraph selection mode, extend selection by whole lines/paragraphs.
    if this.m_selection_mode == EventHandlerSelectionMode.paragraph and this.m_selection_origin != none and is_dom_text(focus_node):
        let selection_origin = this.m_selection_origin else trap("unreachable")
        let focus_text_node = as_dom_text(focus_node)

        # For input/textarea, find line boundaries using newline characters.
        # For regular content, find paragraph boundaries using block elements.
        var paragraph_range: DomRange* = ---
        if this.m_mouse_selection_target != none:
            let data = dom_character_data_data(focus_text_node.dom_character_data())
            let view = ak.utf16_string_base_utf16_view(&data.utf16_string_base)
            paragraph_range = dom_range_create_dom_node(focus_text_node.dom_node(), (u32)find_line_start(view, focus_index), focus_text_node.dom_node(), (u32)find_line_end(view, focus_index))
        else:
            paragraph_range = find_paragraph_range(focus_text_node, (u32)focus_index)

        # Determine cursor position relative to origin.
        let range = selection_origin.dom_abstract_range()
        let paragraph = paragraph_range.dom_abstract_range()
        let position: i16? = dom_range_compare_point(selection_origin, focus_node, (u32)focus_index) catch:
            _ = idl_take_pending_exception()
            recover none
        if let value = position:
            if value < 0:
                # Cursor is before origin: select from origin end to current paragraph start.
                anchor_node = dom_abstract_range_end_container(range)
                anchor_offset = (usize)dom_abstract_range_end_offset(range)
                focus_node = dom_abstract_range_start_container(paragraph)
                focus_index = (usize)dom_abstract_range_start_offset(paragraph)
            elif value > 0:
                # Cursor is after origin: select from origin start to current paragraph end.
                anchor_node = dom_abstract_range_start_container(range)
                anchor_offset = (usize)dom_abstract_range_start_offset(range)
                focus_node = dom_abstract_range_end_container(paragraph)
                focus_index = (usize)dom_abstract_range_end_offset(paragraph)
            else:
                # Cursor is within origin: keep original paragraph selected.
                anchor_node = dom_abstract_range_start_container(range)
                anchor_offset = (usize)dom_abstract_range_start_offset(range)
                focus_node = dom_abstract_range_end_container(range)
                focus_index = (usize)dom_abstract_range_end_offset(range)

    if let target = this.m_mouse_selection_target:
        if let offset = anchor_offset:
            input_events_target_set_selection_anchor(target, anchor_node else focus_node, offset)
        input_events_target_set_selection_focus(target, focus_node, focus_index)
    else:
        if let selection = dom_document_get_selection(document):
            var selection_anchor_node = anchor_node
            if selection_anchor_node == none:
                selection_anchor_node = selection_selection_anchor_node(selection)
            let user_select = layout_node_user_select_used_value(paint_paintable_layout_node(hit_paintable))
            if let anchor = selection_anchor_node:
                if tree_node_root(anchor) == tree_node_root(focus_node):
                    let selection_anchor_offset = anchor_offset else (usize)selection_selection_anchor_offset(selection)
                    set_user_selection(anchor, selection_anchor_offset, focus_node, focus_index, selection, user_select)
            else:
                set_user_selection(focus_node, focus_index, focus_node, focus_index, selection, user_select)

            dom_document_set_needs_repaint_t(document)

# mark: Activation ==============================================================================

## Ported from EventHandler.cpp:526 Web::light_dismiss_activities.
# https://html.spec.whatwg.org/multipage/interactive-elements.html#run-light-dismiss-activities
func light_dismiss_activities(event: UiEventsPointerEvent*, target: DomNode*):
    # To run light dismiss activities, given a PointerEvent event:

    # 1. Run light dismiss open popovers with event.
    html_element_light_dismiss_open_popovers(event, target)

    # 2. Run light dismiss open dialogs with event.
    html_dialog_element_light_dismiss_open_dialogs(event, target)

## Ported from EventHandler.cpp:537 Web::set_node_and_ancestors_being_activated.
func set_node_and_ancestors_being_activated(node: DomNode*?, activated: bool):
    var ancestor = node
    while let current = ancestor:
        if let element = as_if_dom_element(current):
            dom_element_set_being_activated(element, activated)
        ancestor = tree_node_parent(current)

# mark: Construction ============================================================================

## Ported from EventHandler.cpp:545 Web::EventHandler::EventHandler, with the default member
## initializers.
pub func event_handler_construct(this: EventHandler*, navigable: HtmlNavigable*):
    this.m_navigable = navigable
    this.m_selection_mode = EventHandlerSelectionMode.none_
    this.m_mouse_selection_target = none
    this.m_mousedown_click_count = 0
    this.m_mousedown_target_is_drag_candidate = false
    this.m_prevent_mouse_event = false
    # make<DragAndDropEventHandler>(): managed memory the navigable's event handler refers to.
    this.m_drag_and_drop_event_handler = new DragAndDropEventHandler else trap("out of memory")

## Ported from EventHandler.cpp:553 Web::EventHandler::paint_root.
func event_handler_paint_root(this: EventHandler*) -> PaintPaintableBox*?:
    let document = html_navigable_active_document(this.m_navigable) else return none
    return dom_node_paintable_box(document.dom_node())

## Ported from EventHandler.cpp:560 Web::EventHandler::paint_root.
func event_handler_paint_root_const(this: const EventHandler*) -> PaintPaintableBox*?:
    let document = html_navigable_active_document(this.m_navigable) else return none
    return dom_node_paintable_box(document.dom_node())