Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_image_element_3.lucb

#==============================================================================================
#
#   html_image_element_3 - HTML::HTMLImageElement: environment changes, source sets, animation
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLImageElement.cpp (lines 991-1398): reacting to viewport and
#       environment changes, upgrading the pending request, restarting the animation,
#       updating the source set of an img (from its srcset/sizes/src or a picture's source
#       children) and selecting an image source, animating, and "allows auto-sizes".
#       (HTMLImageElement.h declares handle_successful_fetch, which the donor never defines;
#       it has no port.)
#
#==============================================================================================

# mark: Environment changes =====================================================================

## did_set_viewport_rect's batched callback (its context is the element).
func html_image_element_react_to_viewport_change(context: void*?):
    html_image_element_react_to_changes_in_the_environment((HtmlImageElement*)(context else trap("no image element")))

## Ported from HTMLImageElement.cpp:991 Web::HTML::HTMLImageElement::did_set_viewport_rect (override).
pub func html_image_element_did_set_viewport_rect_impl(base: DomViewportClient*, viewport_rect: gfx.CssPixelRect):
    let this = (HtmlImageElement*)((u8*)base - memory.offset_of(HtmlImageElement, viewport_client))
    let size = gfx.css_pixel_rect_size(&viewport_rect)
    if gfx.css_pixel_size_eq(&size, this.m_last_seen_viewport_size):
        return
    this.m_last_seen_viewport_size = size
    html_batching_dispatcher_enqueue(html_batching_dispatcher(), gc.create_function0(realm_heap(html_element_realm(this.html_element())), ak.Function0[unit](call = html_image_element_react_to_viewport_change, context = this)))

## The captures of react_to_changes_in_the_environment's step 16 and its callbacks:
## `[this, selected_source, image_request, key, image_data]`.
struct HtmlImageElementEnvironmentChange:
    var this: HtmlImageElement*
    var selected_source: ak.String
    var image_request: HtmlImageRequest*
    var key: HtmlListOfAvailableImagesKey
    var image_data: HtmlDecodedImageData*?

    ## step_16: queue the task that upgrades the pending request.
    static func step_16(captures: HtmlImageElementEnvironmentChange*):
        # 16. Queue an element task on the DOM manipulation task source given the img element and the following steps:
        _ = dom_element_queue_an_element_task(captures.this.dom_element(), HtmlTaskSource.dom_manipulation, ak.Function0[unit](call = HtmlImageElementEnvironmentChange.step_16_task, context = captures))

    ## step_16's task.
    static func step_16_task(context: void*?):
        let captures = (HtmlImageElementEnvironmentChange*)(context else trap("environment change: no captures"))
        let this = captures.this
        # 1. FIXME: If the img element has experienced relevant mutations since this algorithm started, then set the pending request to null and abort these steps.
        # AD-HOC: Check if we have a pending request still, otherwise we will crash when upgrading the request. This will happen if the image has experienced mutations,
        #        but since the pending request may be set by another task soon after it is cleared, this check is probably not sufficient.
        if this.m_pending_request == none:
            return

        # 2. Set the img element's last selected source to selected source and the img element's current pixel density to selected pixel density.
        # FIXME: pixel density
        this.m_last_selected_source = captures.selected_source

        # 3. Set the image request's state to completely available.
        html_image_request_set_state(captures.image_request, HtmlImageRequestState.completely_available)

        # 4. Add the image to the list of available images using the key key, with the ignore higher-layer caching flag set.
        html_list_of_available_images_add(dom_document_list_of_available_images(dom_node_document(this.dom_node())), captures.key, captures.image_data else trap("VERIFY: image data"), true)

        # 5. Upgrade the pending request to the current request.
        html_image_element_upgrade_pending_request_to_current_request(this)

        # 6. Prepare image request for presentation given the img element.
        html_image_request_prepare_for_presentation(captures.image_request, this)
        # FIXME: This is ad-hoc, updating the layout here should probably be handled by prepare_for_presentation().
        dom_node_set_needs_style_update(this.dom_node(), true)
        dom_node_set_needs_layout_update(this.dom_node(), DomSetNeedsLayoutReason.html_image_element_react_to_changes_in_the_environment)

        # 7. Fire an event named load at the img element.
        _ = dom_event_target_dispatch_event(this.dom_event_target(), dom_event_create(html_element_realm(this.html_element()), html_event_names_load))

    ## The image request's on_finish callback (steps 6 and 7).
    static func on_finish(context: void*?):
        let captures = (HtmlImageElementEnvironmentChange*)(context else trap("environment change: no captures"))
        # 6. If response's unsafe response is a network error
        # NOTE: This is handled in the second callback below.

        # FIXME: or if the image format is unsupported (as determined by applying the image sniffing rules, again as mentioned earlier),

        # or if the user agent is able to determine that image request's image is corrupted in some
        # fatal way such that the image dimensions cannot be obtained,
        # NOTE: This is also handled in the other callback.

        # FIXME: or if the resource type is multipart/x-mixed-replace,

        # then set the pending request to null and abort these steps.

        html_batching_dispatcher_enqueue(html_batching_dispatcher(), gc.create_function0(gc.cell_heap(captures.this.cell()), ak.Function0[unit](call = HtmlImageElementEnvironmentChange.on_finish_batched, context = captures)))

    ## on_finish's batched step 7.
    static func on_finish_batched(context: void*?):
        let captures = (HtmlImageElementEnvironmentChange*)(context else trap("environment change: no captures"))
        # 7. Otherwise, response's unsafe response is image request's image data. It can be either CORS-same-origin
        #    or CORS-cross-origin; this affects the image's interaction with other APIs (e.g., when used on a canvas).
        let shared_resource_request = html_image_request_shared_resource_request(captures.image_request) else trap("VERIFY: image_request->shared_resource_request()")
        let image_data = html_shared_resource_request_image_data(shared_resource_request)
        html_image_request_set_image_data(captures.image_request, image_data)
        captures.image_data = image_data
        HtmlImageElementEnvironmentChange.step_16(captures)

    ## The image request's on_fail callback (step 6).
    static func on_fail(context: void*?):
        let captures = (HtmlImageElementEnvironmentChange*)(context else trap("environment change: no captures"))
        # 6. If response's unsafe response is a network error
        #    or if the image format is unsupported (as determined by applying the image sniffing rules, again as mentioned earlier),
        #    ...
        #    or if the user agent is able to determine that image request's image is corrupted in some
        #    fatal way such that the image dimensions cannot be obtained,
        captures.this.m_pending_request = none

## Ported from HTMLImageElement.cpp:1002 Web::HTML::HTMLImageElement::react_to_changes_in_the_environment.
# https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
pub func html_image_element_react_to_changes_in_the_environment(this: HtmlImageElement*):
    let document = dom_node_document(this.dom_node())
    let realm = html_element_realm(this.html_element())
    # FIXME: 1. Await a stable state.
    #           The synchronous section consists of all the remaining steps of this algorithm
    #           until the algorithm says the synchronous section has ended.
    #           (Steps in synchronous sections are marked with ⌛.)

    # 2. ⌛ If the img element does not use srcset or picture,
    #       its node document is not fully active,
    #       FIXME: it has image data whose resource type is multipart/x-mixed-replace,
    #       or its pending request is not null,
    #       then return.
    if not html_image_element_uses_srcset_or_picture(this) or not dom_document_is_fully_active(document) or this.m_pending_request != none:
        return

    # 3. ⌛ Let selected source and selected pixel density be the URL and pixel density
    #       that results from selecting an image source, respectively.
    var selected_source: ak.String? = none
    var pixel_density: f32? = none
    if let result = html_image_element_select_an_image_source(this):
        selected_source = result.source.url
        pixel_density = (f32)result.pixel_density

    # 4. ⌛ If selected source is null, then return.
    let source = selected_source else return

    # 5. ⌛ If selected source and selected pixel density are the same
    #       as the element's last selected source and current pixel density, then return.
    var same_source = false
    if let last_selected_source = this.m_last_selected_source:
        same_source = ak.string_equals(&source, &last_selected_source)
    if same_source and pixel_density == html_image_request_current_pixel_density(html_image_element_current_request(this)):
        return

    # 6. ⌛ Let urlString be the result of encoding-parsing-and-serializing a URL given selected source, relative to the element's node document.
    let url_string = dom_document_encoding_parse_and_serialize_url(document, ak.string_bytes_as_string_view(&source))

    # 7. ⌛ If urlString is failure, then return.
    let url = url_string else return

    # 8. ⌛ Let corsAttributeState be the state of the element's crossorigin content attribute.
    let cors_attribute_state = this.m_cors_setting

    # 9. ⌛ Let origin be the img element's node document's origin.
    let origin = dom_document_origin(document)

    # 10. ⌛ Let client be the img element's node document's relevant settings object.
    let client = dom_document_relevant_settings_object(document)

    # 11. ⌛ Let key be a tuple consisting of urlString, corsAttributeState, and, if corsAttributeState is not No CORS, origin.
    var key = HtmlListOfAvailableImagesKey(url = url, mode = this.m_cors_setting, origin = none, cached_hash = none)
    if cors_attribute_state != HtmlCorsSettingAttribute.no_cors:
        key.origin = dom_document_origin(document)
    _ = origin

    # 12. ⌛ Let image request be a new image request whose current URL is urlString
    let image_request = html_image_request_create(realm, dom_document_page(document))
    html_image_request_set_current_url(image_request, realm, url)

    # 13. ⌛ Set the element's pending request to image request.
    this.m_pending_request = image_request

    # FIXME: 14. End the synchronous section, continuing the remaining steps in parallel.

    let captures = new HtmlImageElementEnvironmentChange(this = this, selected_source = source, image_request = image_request, key = key, image_data = none) else trap("out of memory")

    # 15. If the list of available images contains an entry for key, then set image request's image data to that of the entry.
    #     Continue to the next step.
    if let entry = html_list_of_available_images_get(dom_document_list_of_available_images(document), key):
        html_image_request_set_image_data(image_request, entry.image_data)
        captures.image_data = entry.image_data
        HtmlImageElementEnvironmentChange.step_16(captures)
    # Otherwise:
    else:
        # AD-HOC: create_potential_CORS_request expects a url, but the following step passes a URL string.
        let url_record = dom_document_encoding_parse_url(document, ak.string_bytes_as_string_view(&source)) else trap("VERIFY: url_record.has_value()")

        # 1. Let request be the result of creating a potential-CORS request given urlString, "image", and corsAttributeState.
        let request = html_create_potential_cors_request(realm_vm(realm), url_record, FetchRequestDestination.image, this.m_cors_setting)

        # 2. Set request's client to client, set request's initiator to "imageset", and set request's synchronous flag.
        fetch_request_set_client(request, client)
        fetch_request_set_initiator(request, FetchRequestInitiator.image_set)

        # 3. Set request's referrer policy to the current state of the element's referrerpolicy attribute.
        let referrer_policy_attribute = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_referrerpolicy)
        fetch_request_set_referrer_policy(request, referrer_policy_from_string(ak.string_bytes_as_string_view(&referrer_policy_attribute)) else ReferrerPolicyReferrerPolicy.empty_string)

        # FIXME: 4. Set request's priority to the current state of the element's fetchpriority attribute.

        # Set the callbacks to handle steps 6 and 7 before starting the fetch request.
        html_image_request_add_callbacks(image_request, ak.Function0[unit](call = HtmlImageElementEnvironmentChange.on_finish, context = captures), ak.Function0[unit](call = HtmlImageElementEnvironmentChange.on_fail, context = captures))

        # 5. Let response be the result of fetching request.
        html_image_request_fetch_image(image_request, realm, request)

# mark: Requests and animation ==================================================================

## Ported from HTMLImageElement.cpp:1164 Web::HTML::HTMLImageElement::upgrade_pending_request_to_current_request.
# https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
pub func html_image_element_upgrade_pending_request_to_current_request(this: HtmlImageElement*):
    # 1. Set the img element's current request to the pending request.
    assert(this.m_pending_request != none)
    this.m_current_request = this.m_pending_request

    # 2. Set the img element's pending request to null.
    this.m_pending_request = none

## Ported from HTMLImageElement.cpp:1174 Web::HTML::HTMLImageElement::handle_failed_fetch.
pub func html_image_element_handle_failed_fetch(this: HtmlImageElement*):
    # AD-HOC: This should be closer to the spec
    _ = dom_event_target_dispatch_event(this.dom_event_target(), dom_event_create(html_element_realm(this.html_element()), html_event_names_error))

## Ported from HTMLImageElement.cpp:1181 Web::HTML::HTMLImageElement::restart_the_animation.
# https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
pub func html_image_element_restart_the_animation(this: HtmlImageElement*):
    this.m_current_frame_index = 0

    let animation_timer = this.m_animation_timer else trap("the animation timer")
    let image_data = html_image_request_image_data(html_image_element_current_request(this))
    var multiple_frames = false
    if let data = image_data:
        multiple_frames = html_decoded_image_data_frame_count(data) > 1
    if multiple_frames:
        core_timer_start(animation_timer)
    else:
        core_timer_stop(animation_timer)

# mark: Source sets =============================================================================

## Ported from HTMLImageElement.cpp:1194 Web::HTML::update_the_source_set.
# https://html.spec.whatwg.org/multipage/images.html#update-the-source-set
func html_update_the_source_set(element: DomElement*):
    # When asked to update the source set for a given img or link element el, user agents must do the following:
    assert(is_html_image_element(element) or is_html_link_element(element))

    # 1. Set el's source set to an empty source set.
    if let image_element = as_if_html_image_element(element):
        html_image_element_set_source_set(image_element, html_source_set_new())
    elif is_html_link_element(element):
        trap("TODO")

    # 2. Let elements be « el ».
    var elements: ak.Vector[DomElement*]
    ak.vector_append_t(&elements, element)

    # 3. If el is an img element whose parent node is a picture element,
    #    then replace the contents of elements with el's parent node's child elements, retaining relative order.
    if is_html_image_element(element):
        if let parent = tree_node_parent(element.dom_node()):
            if is_html_picture_element(parent):
                ak.vector_clear(&elements)
                for child in dom_node_children(parent):
                    if let child_element = as_if_dom_element(child):
                        ak.vector_append_t(&elements, child_element)

    # 4. Let img be el if el is an img element, otherwise null.
    let img = as_if_html_image_element(element)

    # 5. For each child in elements:
    for child in ak.vector_span(&elements):
        # 1. If child is el:
        if child == element:
            # 1. Let default source be the empty string.
            var default_source = ak.string_new()

            # 2. Let srcset be the empty string.
            var srcset = ak.string_new()

            # 3. Let sizes be the empty string.
            var sizes = ak.string_new()

            # 4. If el is an img element that has a srcset attribute, then set srcset to that attribute's value.
            if is_html_image_element(element):
                if let srcset_value = dom_element_attribute(element, html_attribute_names_srcset):
                    srcset = srcset_value

            # 5. Otherwise, if el is a link element that has an imagesrcset attribute, then set srcset to that attribute's value.
            elif is_html_link_element(element):
                if let imagesrcset_value = dom_element_attribute(element, html_attribute_names_imagesrcset):
                    srcset = imagesrcset_value

            # 6. If el is an img element that has a sizes attribute, then set sizes to that attribute's value.
            if is_html_image_element(element):
                if let sizes_value = dom_element_attribute(element, html_attribute_names_sizes):
                    sizes = sizes_value

            # 7. Otherwise, if el is a link element that has an imagesizes attribute, then set sizes to that attribute's value.
            elif is_html_link_element(element):
                if let imagesizes_value = dom_element_attribute(element, html_attribute_names_imagesizes):
                    sizes = imagesizes_value

            # 8. If el is an img element that has a src attribute, then set default source to that attribute's value.
            if is_html_image_element(element):
                if let src_value = dom_element_attribute(element, html_attribute_names_src):
                    default_source = src_value

            # 9. Otherwise, if el is a link element that has an href attribute, then set default source to that attribute's value.
            elif is_html_link_element(element):
                if let href_value = dom_element_attribute(element, html_attribute_names_href):
                    default_source = href_value

            # 10. Set el's source set to the result of creating a source set given default source, srcset, sizes, and img.
            if let image_element = as_if_html_image_element(element):
                html_image_element_set_source_set(image_element, html_source_set_create(element, default_source, srcset, sizes, img))
            elif is_html_link_element(element):
                trap("TODO")

            # 11. Return.
            return
        # 2. If child is not a source element, then continue.
        if not is_html_source_element(child):
            continue

        # 3. If child does not have a srcset attribute, continue to the next child.
        if not dom_element_has_attribute(child, html_attribute_names_srcset):
            continue

        # 4. Parse child's srcset attribute and let source set be the returned source set.
        let child_srcset = dom_element_get_attribute_value(child, html_attribute_names_srcset)
        var source_set = html_parse_a_srcset_attribute(ak.string_bytes_as_string_view(&child_srcset))

        # 5. If source set has zero image sources, continue to the next child.
        if html_source_set_is_empty(&source_set):
            continue

        # 6. If child has a media attribute, and its value does not match the environment, continue to the next child.
        if dom_element_has_attribute(child, html_attribute_names_media):
            let media = dom_element_get_attribute_value(child, html_attribute_names_media)
            let media_query = parse_media_query(html_document_parsing_params(dom_node_document(element.dom_node())), ak.string_bytes_as_string_view(&media))
            let query = media_query else continue
            if not css_media_query_evaluate(query, dom_node_document(element.dom_node())):
                continue

        # 7. Parse child's sizes attribute with img, and let source set's source size be the returned value.
        let child_sizes = dom_element_get_attribute_value(child, html_attribute_names_sizes)
        source_set.m_source_size = html_parse_a_sizes_attribute(element, ak.string_bytes_as_string_view(&child_sizes), img)

        # 8. If child has a type attribute, and its value is an unknown or unsupported MIME type, continue to the next child.
        if dom_element_has_attribute(child, html_attribute_names_type):
            let mime_type = dom_element_get_attribute_value(child, html_attribute_names_type)
            if not html_is_supported_image_type(ak.string_bytes_as_string_view(&mime_type)):
                continue

        # 9. If child has width or height attributes, set el's dimension attribute source to child.
        #    Otherwise, set el's dimension attribute source to el.
        let image = img else trap("VERIFY: an img element")
        if dom_element_has_attribute(child, html_attribute_names_width) or dom_element_has_attribute(child, html_attribute_names_height):
            html_image_element_set_dimension_attribute_source(image, child)
        else:
            html_image_element_set_dimension_attribute_source(image, none)

        # 10. Normalize the source densities of source set.
        html_source_set_normalize_source_densities(&source_set, element)

        # 11. Set el's source set to source set.
        html_image_element_set_source_set(image, source_set)

        # 12. Return.
        return

## Ported from HTMLImageElement.cpp:1335 Web::HTML::HTMLImageElement::select_an_image_source.
# https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
pub func html_image_element_select_an_image_source(this: HtmlImageElement*) -> HtmlImageSourceAndPixelDensity?:
    # 1. Update the source set for el.
    html_update_the_source_set(this.dom_element())

    # 2. If el's source set is empty, return null as the URL and undefined as the pixel density.
    if html_source_set_is_empty(&this.m_source_set):
        return none

    # 3. Return the result of selecting an image from el's source set.
    return html_source_set_select_an_image_source(&this.m_source_set)

## Ported from HTMLImageElement.cpp:1348 Web::HTML::HTMLImageElement::set_source_set.
pub func html_image_element_set_source_set(this: HtmlImageElement*, source_set: HtmlSourceSet):
    this.m_source_set = source_set

## Ported from HTMLImageElement.cpp:1353 Web::HTML::HTMLImageElement::animate.
pub func html_image_element_animate(this: HtmlImageElement*):
    let image_data = html_image_request_image_data(html_image_element_current_request(this)) else return

    let frame_count = html_decoded_image_data_frame_count(image_data)
    this.m_current_frame_index = (this.m_current_frame_index + 1) % frame_count
    this.m_current_frame_index = html_decoded_image_data_notify_frame_advanced(image_data, this.m_current_frame_index)
    let current_frame_duration = html_decoded_image_data_frame_duration(image_data, this.m_current_frame_index)

    let animation_timer = this.m_animation_timer else trap("the animation timer")
    if current_frame_duration != core_timer_interval(animation_timer):
        core_timer_restart_i32(animation_timer, current_frame_duration)

    if this.m_current_frame_index == frame_count -% 1:
        this.m_loops_completed += 1
        if this.m_loops_completed > 0 and this.m_loops_completed == html_decoded_image_data_loop_count(image_data):
            core_timer_stop(animation_timer)

    dom_node_set_needs_repaint(this.dom_node())

## Ported from HTMLImageElement.cpp:1378 Web::HTML::HTMLImageElement::allows_auto_sizes.
pub func html_image_element_allows_auto_sizes(this: HtmlImageElement*) -> bool:
    # An img element allows auto-sizes if:
    # - its loading attribute is in the Lazy state, and
    # - its sizes attribute's value is "auto" (ASCII case-insensitive), or starts with "auto," (ASCII case-insensitive).
    if html_lazy_loading_element_lazy_loading_attribute(this) != HtmlLazyLoading.lazy:
        return false
    let sizes = dom_element_attribute(this.dom_element(), html_attribute_names_sizes) else return false
    return ak.string_equals_ignoring_ascii_case_string_view(&sizes, ak.sv("auto")) or ak.string_starts_with_bytes(&sizes, ak.sv("auto,"), ak.CaseSensitivity.case_insensitive)

## Ported from HTMLImageElement.cpp:1391 Web::HTML::HTMLImageElement::decoded_image_data (override).
pub func html_image_element_decoded_image_data_impl(base: LayoutImageProvider*) -> HtmlDecodedImageData*?:
    let this = html_image_element_from_image_provider(base)
    let current_request = this.m_current_request else return none
    return html_image_request_image_data(current_request)