Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / lazy_loading_element.lucb

#==============================================================================================
#
#   lazy_loading_element - HTML::LazyLoadingElement<T>: the lazy loading attribute of img and
#                          iframe elements
#
#   DESCRIPTION:
#       Port of the inline functions of LibWeb/HTML/LazyLoadingElement.h (lines 1-91). The CRTP
#       base is embedded in HTMLImageElement and HTMLIFrameElement at different offsets, so the
#       functions take the element itself (C++ `static_cast<T&>(*this)`) and reach the mixin
#       through the HasHtmlLazyLoadingElement interface both classes conform to. The
#       LAZY_LOADING_ELEMENT macro's overrides belong to those classes (regions r26, r27).
#
#==============================================================================================

## Ported from LazyLoadingElement.h:36 Web::HTML::LazyLoadingElement::lazy_loading_attribute.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-loading-attributes
pub func html_lazy_loading_element_lazy_loading_attribute[T: IsDomElement & HasHtmlLazyLoadingElement[T]](this: T*) -> HtmlLazyLoading:
    let element = this.dom_element()

    let value = dom_element_attribute(element, html_attribute_names_loading)
    if let present = value:
        if ak.string_equals_ignoring_ascii_case_string_view(&present, ak.sv("lazy")):
            return HtmlLazyLoading.lazy
    return HtmlLazyLoading.eager

## Ported from LazyLoadingElement.h:47 Web::HTML::LazyLoadingElement::will_lazy_load_element.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
pub func html_lazy_loading_element_will_lazy_load_element[T: IsDomElement & HasHtmlLazyLoadingElement[T]](this: T*) -> bool:
    let element = this.dom_element()

    # 1. If scripting is disabled for element, then return false.
    # Spec Note: This is an anti-tracking measure, because if a user agent supported lazy loading when scripting is
    #            disabled, it would still be possible for a site to track a user's approximate scroll position throughout
    #            a session, by strategically placing images in a page's markup such that a server can track how many
    #            images are requested and when.
    if dom_node_is_scripting_disabled(element.dom_node()):
        return false

    # 2. If element's lazy loading attribute is in the Lazy state, then return true.
    # 3. Return false.
    return html_lazy_loading_element_lazy_loading_attribute(this) == HtmlLazyLoading.lazy

## Ported from LazyLoadingElement.h:62 Web::HTML::LazyLoadingElement::set_lazy_load_resumption_steps.
pub func html_lazy_loading_element_set_lazy_load_resumption_steps[T: IsDomElement & HasHtmlLazyLoadingElement[T]](this: T*, steps: ak.Function0[unit]):
    let element = this.dom_element()

    let heap = vm_heap(js_cell_vm(element.js_cell()))
    this.html_lazy_loading_element().m_lazy_load_resumption_steps = gc.create_function0(heap, steps)

## Ported from LazyLoadingElement.h:69 Web::HTML::LazyLoadingElement::visit_lazy_loading_element.
pub func html_lazy_loading_element_visit_lazy_loading_element[T: IsDomElement & HasHtmlLazyLoadingElement[T]](this: T*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t[gc.Function0[unit]](visitor, this.html_lazy_loading_element().m_lazy_load_resumption_steps)

## Ported from LazyLoadingElement.h:78 Web::HTML::LazyLoadingElement::take_lazy_load_resumption_steps_internal.
pub func html_lazy_loading_element_take_lazy_load_resumption_steps_internal[T: IsDomElement & HasHtmlLazyLoadingElement[T]](this: T*) -> gc.Function0[unit]*?:
    let mixin = this.html_lazy_loading_element()
    let lazy_load_resumption_steps = mixin.m_lazy_load_resumption_steps
    mixin.m_lazy_load_resumption_steps = none
    return lazy_load_resumption_steps