Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / element_7.lucb

#==============================================================================================
#
#   element_7 - DOM::Element: scrollIntoView
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Element.cpp (lines 2705-2963): determining the scroll-into-view
#       position of a target, scrolling an element into view, and scrollIntoView().
#
#==============================================================================================

# mark: CSSPixels helpers =======================================================================

## `a <= b` on CSSPixels (an integer operand converts to CSSPixels first).
func dom_css_pixels_le(a: gfx.CssPixels, b: gfx.CssPixels) -> bool:
    return gfx.css_pixels_cmp(&a, b) <= 0

## `a >= b` on CSSPixels.
func dom_css_pixels_ge(a: gfx.CssPixels, b: gfx.CssPixels) -> bool:
    return gfx.css_pixels_cmp(&a, b) >= 0

## `a < b` on CSSPixels.
func dom_css_pixels_lt(a: gfx.CssPixels, b: gfx.CssPixels) -> bool:
    return gfx.css_pixels_cmp(&a, b) < 0

## `a > b` on CSSPixels.
func dom_css_pixels_gt(a: gfx.CssPixels, b: gfx.CssPixels) -> bool:
    return gfx.css_pixels_cmp(&a, b) > 0

## `a - b` on CSSPixels.
func dom_css_pixels_sub(a: gfx.CssPixels, b: gfx.CssPixels) -> gfx.CssPixels:
    return gfx.css_pixels_sub(&a, b)

## `a + b` on CSSPixels.
func dom_css_pixels_add(a: gfx.CssPixels, b: gfx.CssPixels) -> gfx.CssPixels:
    return gfx.css_pixels_add_css_pixels(&a, b)

## `value / 2` on CSSPixels: the integral operator/ yields CSSPixelFraction(value, CSSPixels(2)), which
## converts back to CSSPixels where it is added or subtracted.
func dom_css_pixels_half(value: gfx.CssPixels) -> gfx.CssPixels:
    var fraction: gfx.CssPixelFraction = ---
    gfx.css_pixel_fraction_construct_css_pixels(&fraction, value, gfx.css_pixels_from_int(2))
    return gfx.css_pixel_fraction_to_css_pixels(&fraction)

# mark: Scrolling into view =====================================================================

## Ported from Element.cpp:2706 Web::DOM::determine_the_scroll_into_view_position.
# https://drafts.csswg.org/cssom-view-1/#determine-the-scroll-into-view-position
func dom_determine_the_scroll_into_view_position(target: DomElement*, block: BindScrollLogicalPosition, inline_: BindScrollLogicalPosition, scrolling_box: DomNode*) -> gfx.CssPixelPoint:
    # To determine the scroll-into-view position of a target, which is an Element, pseudo-element, or Range, with a
    # block flow direction position block, an inline base direction position inline, and a scrolling box scrolling box,
    # run the following steps:

    if not dom_node_is_document(scrolling_box):
        # FIXME: Add support for scrolling boxes other than the viewport.
        var empty: gfx.CssPixelPoint
        return empty
    # NOTE: For a viewport scrolling box is initial containing block
    let scrolling_box_rect = dom_document_viewport_rect(dom_node_document(scrolling_box))

    # FIXME: All of this needs to support different block/inline directions.

    # 1. Let target bounding border box be the box represented by the return value of invoking Element’s
    #    getBoundingClientRect(), if target is an Element, or Range’s getBoundingClientRect(),
    #    if target is a Range.
    let target_bounding_border_box = dom_element_get_bounding_client_rect(target)

    # 2. Let scrolling box edge A be the beginning edge in the block flow direction of scrolling box, and
    #    let element edge A be target bounding border box’s edge on the same physical side as that of
    #    scrolling box edge A.
    let element_edge_a = gfx.css_pixel_rect_top(&target_bounding_border_box)
    let scrolling_box_edge_a = gfx.css_pixel_rect_top(&scrolling_box_rect)

    # 3. Let scrolling box edge B be the ending edge in the block flow direction of scrolling box, and let
    #    element edge B be target bounding border box’s edge on the same physical side as that of scrolling
    #    box edge B.
    let element_edge_b = gfx.css_pixel_rect_bottom(&target_bounding_border_box)
    let scrolling_box_edge_b = gfx.css_pixel_rect_bottom(&scrolling_box_rect)

    # 4. Let scrolling box edge C be the beginning edge in the inline base direction of scrolling box, and
    #    let element edge C be target bounding border box’s edge on the same physical side as that of scrolling
    #    box edge C.
    let element_edge_c = gfx.css_pixel_rect_left(&target_bounding_border_box)
    let scrolling_box_edge_c = gfx.css_pixel_rect_left(&scrolling_box_rect)

    # 5. Let scrolling box edge D be the ending edge in the inline base direction of scrolling box, and let element
    #    edge D be target bounding border box’s edge on the same physical side as that of scrolling box edge D.
    let element_edge_d = gfx.css_pixel_rect_right(&target_bounding_border_box)
    let scrolling_box_edge_d = gfx.css_pixel_rect_right(&scrolling_box_rect)

    # 6. Let element height be the distance between element edge A and element edge B.
    let element_height = dom_css_pixels_sub(element_edge_b, element_edge_a)

    # 7. Let scrolling box height be the distance between scrolling box edge A and scrolling box edge B.
    let scrolling_box_height = dom_css_pixels_sub(scrolling_box_edge_b, scrolling_box_edge_a)

    # 8. Let element width be the distance between element edge C and element edge D.
    let element_width = dom_css_pixels_sub(element_edge_d, element_edge_c)

    # 9. Let scrolling box width be the distance between scrolling box edge C and scrolling box edge D.
    let scrolling_box_width = dom_css_pixels_sub(scrolling_box_edge_d, scrolling_box_edge_c)

    let zero = gfx.css_pixels_from_int(0)

    # 10. Let position be the scroll position scrolling box would have by following these steps:
    var x = gfx.css_pixels_from_int(0)
    var y = gfx.css_pixels_from_int(0)

    # 1. If block is "start", then align element edge A with scrolling box edge A.
    if block == BindScrollLogicalPosition.start:
        y = element_edge_a
    # 2. Otherwise, if block is "end", then align element edge B with scrolling box edge B.
    elif block == BindScrollLogicalPosition.end:
        y = dom_css_pixels_sub(dom_css_pixels_add(element_edge_a, element_height), scrolling_box_height)
    # 3. Otherwise, if block is "center", then align the center of target bounding border box with the center of
    #    scrolling box in scrolling box’s block flow direction.
    elif block == BindScrollLogicalPosition.center:
        y = dom_css_pixels_sub(dom_css_pixels_add(element_edge_a, dom_css_pixels_half(element_height)), dom_css_pixels_half(scrolling_box_height))
    # 4. Otherwise, block is "nearest":
    else:
        # If element edge A and element edge B are both outside scrolling box edge A and scrolling box edge B
        #     Do nothing.
        let both_outside = dom_css_pixels_le(element_edge_a, zero) and dom_css_pixels_ge(element_edge_b, scrolling_box_height)
        # If element edge A is outside scrolling box edge A and element height is less than scrolling box height
        # If element edge B is outside scrolling box edge B and element height is greater than scrolling box height
        if not both_outside:
            if (dom_css_pixels_le(element_edge_a, zero) and dom_css_pixels_lt(element_height, scrolling_box_height)) or (dom_css_pixels_ge(element_edge_b, scrolling_box_height) and dom_css_pixels_gt(element_height, scrolling_box_height)):
                # Align element edge A with scrolling box edge A.
                y = element_edge_a
            # If element edge A is outside scrolling box edge A and element height is greater than scrolling box height
            # If element edge B is outside scrolling box edge B and element height is less than scrolling box height
            elif (dom_css_pixels_ge(element_edge_b, scrolling_box_height) and dom_css_pixels_lt(element_height, scrolling_box_height)) or (dom_css_pixels_le(element_edge_a, zero) and dom_css_pixels_gt(element_height, scrolling_box_height)):
                # Align element edge B with scrolling box edge B.
                y = dom_css_pixels_sub(dom_css_pixels_add(element_edge_a, element_height), scrolling_box_height)

    # 5. If inline is "start", then align element edge C with scrolling box edge C.
    if inline_ == BindScrollLogicalPosition.start:
        x = element_edge_c
    # 6. Otherwise, if inline is "end", then align element edge D with scrolling box edge D.
    elif inline_ == BindScrollLogicalPosition.end:
        x = dom_css_pixels_sub(dom_css_pixels_add(element_edge_d, element_width), scrolling_box_width)
    # 7. Otherwise, if inline is "center", then align the center of target bounding border box with the center of
    #    scrolling box in scrolling box’s inline base direction.
    elif inline_ == BindScrollLogicalPosition.center:
        x = dom_css_pixels_sub(dom_css_pixels_add(element_edge_c, dom_css_pixels_half(element_width)), dom_css_pixels_half(scrolling_box_width))
    # 8. Otherwise, inline is "nearest":
    else:
        # If element edge C and element edge D are both outside scrolling box edge C and scrolling box edge D
        #     Do nothing.
        let both_outside = dom_css_pixels_le(element_edge_c, zero) and dom_css_pixels_ge(element_edge_d, scrolling_box_width)
        # If element edge C is outside scrolling box edge C and element width is less than scrolling box width
        # If element edge D is outside scrolling box edge D and element width is greater than scrolling box width
        if not both_outside:
            if (dom_css_pixels_le(element_edge_c, zero) and dom_css_pixels_lt(element_width, scrolling_box_width)) or (dom_css_pixels_ge(element_edge_d, scrolling_box_width) and dom_css_pixels_gt(element_width, scrolling_box_width)):
                # Align element edge C with scrolling box edge C.
                x = element_edge_c
            # If element edge C is outside scrolling box edge C and element width is greater than scrolling box width
            # If element edge D is outside scrolling box edge D and element width is less than scrolling box width
            elif (dom_css_pixels_ge(element_edge_d, scrolling_box_width) and dom_css_pixels_lt(element_width, scrolling_box_width)) or (dom_css_pixels_le(element_edge_c, zero) and dom_css_pixels_gt(element_width, scrolling_box_width)):
                # Align element edge D with scrolling box edge D.
                x = dom_css_pixels_sub(dom_css_pixels_add(element_edge_d, element_width), scrolling_box_width)

    var position: gfx.CssPixelPoint = ---
    gfx.css_pixel_point_construct(&position, x, y)

    # 11. Return position.
    return position

## Ported from Element.cpp:2840 Web::DOM::scroll_an_element_into_view.
# https://drafts.csswg.org/cssom-view-1/#scroll-a-target-into-view
func dom_scroll_an_element_into_view(target: DomElement*, behavior: BindScrollBehavior, block: BindScrollLogicalPosition, inline_: BindScrollLogicalPosition, container: DomElement*?) -> JsPromiseCapability*:
    # FIXME: 1. Let ancestorPromises be an empty set of Promises.

    # 2. For each ancestor element or viewport that establishes a scrolling box scrolling box, in order of innermost
    #    to outermost scrolling box, run these substeps:
    var ancestor = tree_node_parent(target.dom_node())
    var scrolling_boxes: ak.Vector[DomNode*]
    while let current = ancestor:
        if let paintable_box = dom_node_paintable_box(current):
            if paint_paintable_box_has_scrollable_overflow(paintable_box):
                ak.vector_append_t(&scrolling_boxes, current)
        ancestor = tree_node_parent(current)

    for scrolling_box in ak.vector_span(&scrolling_boxes):
        # 1. If the Document associated with target is not same origin with the Document associated with the element
        #    or viewport associated with scrolling box, abort any remaining iteration of this loop.
        let target_origin = dom_document_origin(dom_element_document(target))
        let scrolling_box_origin = dom_document_origin(dom_node_document(scrolling_box))
        if not web_url.origin_eq(&target_origin, scrolling_box_origin):
            break

        # 2. Let position be the scroll position resulting from running the steps to determine the scroll-into-view
        #    position of target with behavior as the scroll behavior, block as the block flow position, inline as the
        #    inline base direction position and scrolling box as the scrolling box.
        # FIXME: Pass in behavior.
        var position = dom_determine_the_scroll_into_view_position(target, block, inline_, scrolling_box)

        # 3. If position is not the same as scrolling box’s current scroll position, or scrolling box has an ongoing
        #    smooth scroll,
        # FIXME: Actually check this condition.
        # -> If scrolling box is associated with an element
        #    FIXME: Perform a scroll of the element’s scrolling box to position, with the element as the associated element and behavior as the scroll behavior.
        # -> If scrolling box is associated with a viewport
        if not dom_node_is_element(scrolling_box) and dom_node_is_document(scrolling_box):
            # 1. Let document be the viewport’s associated Document.
            let document = (DomDocument*)scrolling_box

            # FIXME: 2. Let root element be document’s root element, if there is one, or null otherwise.
            # FIXME: 3. Perform a scroll of the viewport to position, with root element as the associated element and behavior as the scroll behavior.
            #           Add the Promise returned from this step in the set ancestorPromises.
            _ = behavior

            # AD-HOC:
            # NOTE: Since calculated position is relative to the viewport, we need to add the viewport's position to it
            #       before passing to perform_a_scroll_of_the_viewport() that expects a position relative to the page.
            let viewport_rect = dom_document_viewport_rect(document)
            gfx.css_pixel_point_set_y(&position, dom_css_pixels_add(gfx.css_pixel_point_y(&position), gfx.css_pixel_rect_y(&viewport_rect)))
            let navigable = dom_document_navigable(document) else trap("scroll_an_element_into_view: no navigable")
            _ = html_navigable_perform_a_scroll_of_the_viewport(navigable, position)

        # 4. If container is not null and either scrolling box is a shadow-including inclusive ancestor of container
        #    or is a viewport whose document is a shadow-including inclusive ancestor of container, abort any
        #    remaining iteration of this loop.
        # NB: Our viewports *are* Documents in the DOM, so both checks are equivalent.
        if let container_element = container:
            if dom_node_is_shadow_including_inclusive_ancestor_of(scrolling_box, container_element.dom_node()):
                break

    # 3. Let scrollPromise be a new Promise.
    let scroll_promise = idl_create_promise(dom_element_realm(target))

    # 4. Return scrollPromise, and run the remaining steps in parallel.
    # 5. Resolve scrollPromise when all Promises in ancestorPromises have settled.
    # FIXME: Actually wait for those promises.
    idl_resolve_promise(dom_element_realm(target), scroll_promise, js_undefined())

    return scroll_promise

## Ported from Element.cpp:2912 Web::DOM::Element::scroll_into_view.
# https://drafts.csswg.org/cssom-view/#dom-element-scrollintoview
pub func dom_element_scroll_into_view(this: DomElement*, arg: BoolOrDomScrollIntoViewOptions?) -> JsPromiseCapability*:
    # 1. Let behavior be "auto".
    var behavior = BindScrollBehavior.auto

    # 2. Let block be "start".
    var block = BindScrollLogicalPosition.start

    # 3. Let inline be "nearest".
    var inline_ = BindScrollLogicalPosition.nearest

    # 4. Let container be null.
    var container: DomElement*? = none

    if let argument = arg:
        match argument:
            # 5. If arg is a ScrollIntoViewOptions dictionary, then:
            .dom_scroll_into_view_options(options):
                # 1. Set behavior to the behavior dictionary member of options.
                behavior = options.scroll_options.behavior

                # 2. Set block to the block dictionary member of options.
                block = options.block

                # 3. Set inline to the inline dictionary member of options.
                inline_ = options.inline_

                # 4. If the container dictionary member of options is "nearest", set container to the element.
                if options.container == BindScrollIntoViewContainer.nearest:
                    container = this
            # 6. Otherwise, if arg is false, then set block to "end".
            .bool_(value):
                if value == false:
                    block = BindScrollLogicalPosition.end

    # 7. If the element does not have any associated box, or is not available to user-agent features, then return a
    #    resolved Promise and abort the remaining steps.
    dom_document_update_layout(dom_element_document(this), DomUpdateLayoutReason.element_scroll_into_view)
    var temporary_execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&temporary_execution_context, dom_element_realm(this))
    defer html_temporary_execution_context_destruct(&temporary_execution_context)
    if dom_element_layout_node(this) == none:
        return idl_create_resolved_promise(dom_element_realm(this), js_undefined())

    # 8. Scroll the element into view with behavior, block, inline, and container. Let scrollPromise be the Promise
    #    returned from this step.
    let scroll_promise = dom_scroll_an_element_into_view(this, behavior, block, inline_, container)

    # FIXME: 9. Optionally perform some other action that brings the element to the user’s attention.

    # 10. Return scrollPromise.
    return scroll_promise