Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_body_element.lucb

#==============================================================================================
#
#   html_body_element - HTML::HTMLBodyElement: <body>, its presentational hints and the
#                       window-reflecting event handlers
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLBodyElement.cpp (lines 1-174) and the inlines of
#       HTMLBodyElement.h: construction (which moves the mixin tables to HTMLBodyElement's, as
#       C++'s vptrs move, and sets up the WindowEventHandlers base), visit_edges, initialize, the
#       bgcolor / text / background / margin presentational hints, the link colors and the
#       background image of attribute_changed, and the event handler targets that map to the
#       window.
#
#       The ENUMERATE_WINDOW_EVENT_HANDLERS expansion of attribute_changed is
#       html_window_event_handler_attribute_changed, which HTMLFrameSetElement's
#       attribute_changed (the same macro expansion) calls too.
#
#==============================================================================================

# mark: Construction and visiting ================================================================

## Ported from HTMLBodyElement.cpp:28 Web::HTML::HTMLBodyElement::HTMLBodyElement.
pub func html_body_element_construct(this: HtmlBodyElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The WindowEventHandlers base (no state), then the vptrs of the mixin bases move to
    # HTMLBodyElement's tables.
    this.window_event_handlers.vtable = &html_body_element_window_event_handlers_vtable
    this.window_event_handlers.class_id = this.cell().class_id
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_body_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_body_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_body_element_animatable_vtable
    element.global_event_handlers.vtable = &html_body_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_body_element_form_associated_element_vtable

## Ported from HTMLBodyElement.cpp:35 Web::HTML::HTMLBodyElement::visit_edges (override).
pub func html_body_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlBodyElement*)base
    html_element_visit_edges_impl(base, visitor)
    if let background_style_value = this.m_background_style_value:
        css_style_value_visit_edges(background_style_value.css_style_value(), visitor)

## Ported from HTMLBodyElement.cpp:42 Web::HTML::HTMLBodyElement::initialize (override).
pub func html_body_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_body_element)
    html_element_initialize_impl(base, realm)

# mark: Presentational hints =====================================================================

## Ported from HTMLBodyElement.cpp:48 Web::HTML::HTMLBodyElement::is_presentational_hint (override).
pub func html_body_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
    if dom_element_is_presentational_hint_impl(base, name):
        return true

    let names: ak.FlyString[9] = [
        html_attribute_names_bgcolor,
        html_attribute_names_text,
        html_attribute_names_background,
        html_attribute_names_marginheight,
        html_attribute_names_marginwidth,
        html_attribute_names_topmargin,
        html_attribute_names_rightmargin,
        html_attribute_names_bottommargin,
        html_attribute_names_leftmargin]
    return html_name_is_one_of(name, names)

## The get_margin_value lambda of apply_presentational_hints: the first of the body's two
## attributes that is present, else the attribute of the body's navigable's container (the
## <frame> or <iframe> the document is shown in).
func html_body_element_get_margin_value(this: HtmlBodyElement*, first_body_attr_name: ak.FlyString, second_body_attr_name: ak.FlyString, container_frame_attr_name: ak.FlyString) -> ak.String?:
    if let value = dom_element_get_attribute(this.dom_element(), first_body_attr_name):
        return value
    if let value = dom_element_get_attribute(this.dom_element(), second_body_attr_name):
        return value
    let navigable = dom_document_navigable(dom_node_document(this.dom_node())) else return none
    let container = html_navigable_container(navigable) else return none
    if let value = dom_element_get_attribute(container.dom_element(), container_frame_attr_name):
        return value
    return none

## The apply_margin_value lambda of apply_presentational_hints.
func html_body_element_apply_margin_value(properties: ak.Vector[CssStyleProperty]*, property_id: css_data.PropertyId, value: ak.String?):
    let present = value else return
    if let parsed_value = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&present)):
        html_append_px_hint(properties, property_id, f64(parsed_value))

## Ported from HTMLBodyElement.cpp:65 Web::HTML::HTMLBodyElement::apply_presentational_hints (override).
pub func html_body_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    let this = (HtmlBodyElement*)base
    dom_element_apply_presentational_hints_impl(base, properties)
    # for_each_attribute([&](auto& name, auto& value) { ... })
    if let attributes = base.m_attributes:
        for i in 0..<dom_named_node_map_length(attributes):
            let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
            let name = dom_attr_name(attribute)
            let value = dom_attr_value(attribute)
            if html_name_is(name, html_attribute_names_bgcolor):
                # https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value
                if let color = html_parse_legacy_color_value(ak.string_bytes_as_string_view(&value)):
                    html_append_presentational_hint(properties, css_data.PropertyId.background_color, css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value())
            elif html_name_is(name, html_attribute_names_text):
                # https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2
                if let color = html_parse_legacy_color_value(ak.string_bytes_as_string_view(&value)):
                    html_append_presentational_hint(properties, css_data.PropertyId.color, css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value())
            elif html_name_is(name, html_attribute_names_background):
                let background_style_value = this.m_background_style_value else trap("VERIFY: the body's background style value")
                var layers = ak.Vector[CssStyleValue*]()
                ak.vector_append_t(&layers, background_style_value.css_style_value())
                html_append_presentational_hint(properties, css_data.PropertyId.background_image, css_style_value_list_create(layers, CssStyleValueListSeparator.comma).css_style_value())

    let margin_top_value = html_body_element_get_margin_value(this, html_attribute_names_marginheight, html_attribute_names_topmargin, html_attribute_names_marginheight)
    let margin_bottom_value = html_body_element_get_margin_value(this, html_attribute_names_marginheight, html_attribute_names_bottommargin, html_attribute_names_marginheight)
    let margin_left_value = html_body_element_get_margin_value(this, html_attribute_names_marginwidth, html_attribute_names_leftmargin, html_attribute_names_marginwidth)
    let margin_right_value = html_body_element_get_margin_value(this, html_attribute_names_marginwidth, html_attribute_names_rightmargin, html_attribute_names_marginwidth)

    html_body_element_apply_margin_value(properties, css_data.PropertyId.margin_top, margin_top_value)
    html_body_element_apply_margin_value(properties, css_data.PropertyId.margin_bottom, margin_bottom_value)
    html_body_element_apply_margin_value(properties, css_data.PropertyId.margin_left, margin_left_value)
    html_body_element_apply_margin_value(properties, css_data.PropertyId.margin_right, margin_right_value)

# mark: attribute_changed ========================================================================

## The `on_animate` lambda of the background image: `[this] { if (paintable()) paintable()->set_needs_repaint(); }`;
## the context is the body element.
func html_body_element_background_on_animate(context: void*?):
    let this = (HtmlBodyElement*)(context else trap("on_animate: no body element"))
    if let paintable = dom_node_paintable(this.dom_node()):
        paint_paintable_set_needs_repaint(paintable, InvalidateDisplayList.yes)

## Ported from HTMLBodyElement.cpp:118 Web::HTML::HTMLBodyElement::attribute_changed (override).
pub func html_body_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlBodyElement*)base
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)
    let document = dom_node_document(this.dom_node())
    let value_or_empty = value else ak.string_new()

    if html_name_is(name, html_attribute_names_link):
        # https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
        if let color = html_parse_legacy_color_value(ak.string_bytes_as_string_view(&value_or_empty)):
            dom_document_set_normal_link_color(document, color)
    elif html_name_is(name, html_attribute_names_alink):
        # https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
        if let color = html_parse_legacy_color_value(ak.string_bytes_as_string_view(&value_or_empty)):
            dom_document_set_active_link_color(document, color)
    elif html_name_is(name, html_attribute_names_vlink):
        # https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
        if let color = html_parse_legacy_color_value(ak.string_bytes_as_string_view(&value_or_empty)):
            dom_document_set_visited_link_color(document, color)
    elif html_name_is(name, html_attribute_names_background):
        # https://html.spec.whatwg.org/multipage/rendering.html#the-page:attr-background
        if let background_url = dom_document_encoding_parse_url(document, ak.string_bytes_as_string_view(&value_or_empty)):
            let background_style_value = css_image_style_value_create_url(background_url)
            background_style_value.on_animate = ak.Function0[unit](call = html_body_element_background_on_animate, context = this)
            this.m_background_style_value = background_style_value

    html_window_event_handler_attribute_changed(this.dom_event_target(), name, value)

## The ENUMERATE_WINDOW_EVENT_HANDLERS expansion in HTMLBodyElement::attribute_changed and
## HTMLFrameSetElement::attribute_changed: `#define __ENUMERATE(attribute_name, event_name) if
## (name == HTML::AttributeNames::attribute_name) { element_event_handler_attribute_changed(event_name, value); }`.
func html_window_event_handler_attribute_changed(target: DomEventTarget*, name: ak.FlyString, value: ak.String?):
    if html_name_is(name, html_attribute_names_onafterprint):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_afterprint, value)
    if html_name_is(name, html_attribute_names_onbeforeprint):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_beforeprint, value)
    if html_name_is(name, html_attribute_names_onbeforeunload):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_beforeunload, value)
    if html_name_is(name, html_attribute_names_ongamepadconnected):
        dom_event_target_element_event_handler_attribute_changed(target, gamepad_event_names_gamepadconnected, value)
    if html_name_is(name, html_attribute_names_ongamepaddisconnected):
        dom_event_target_element_event_handler_attribute_changed(target, gamepad_event_names_gamepaddisconnected, value)
    if html_name_is(name, html_attribute_names_onhashchange):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_hashchange, value)
    if html_name_is(name, html_attribute_names_onlanguagechange):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_languagechange, value)
    if html_name_is(name, html_attribute_names_onmessage):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_message, value)
    if html_name_is(name, html_attribute_names_onmessageerror):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_messageerror, value)
    if html_name_is(name, html_attribute_names_onoffline):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_offline, value)
    if html_name_is(name, html_attribute_names_ononline):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_online, value)
    if html_name_is(name, html_attribute_names_onpagehide):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_pagehide, value)
    if html_name_is(name, html_attribute_names_onpagereveal):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_pagereveal, value)
    if html_name_is(name, html_attribute_names_onpageshow):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_pageshow, value)
    if html_name_is(name, html_attribute_names_onpageswap):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_pageswap, value)
    if html_name_is(name, html_attribute_names_onpopstate):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_popstate, value)
    if html_name_is(name, html_attribute_names_onrejectionhandled):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_rejectionhandled, value)
    if html_name_is(name, html_attribute_names_onstorage):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_storage, value)
    if html_name_is(name, html_attribute_names_onunhandledrejection):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_unhandledrejection, value)
    if html_name_is(name, html_attribute_names_onunload):
        dom_event_target_element_event_handler_attribute_changed(target, html_event_names_unload, value)

# mark: Event handler targets ====================================================================

## Ported from HTMLBodyElement.cpp:157 Web::HTML::HTMLBodyElement::global_event_handlers_to_event_target (override).
pub func html_body_element_global_event_handlers_to_event_target_impl(base: HtmlGlobalEventHandlers*, event_name: ak.FlyString) -> DomEventTarget*?:
    let this = (HtmlBodyElement*)((u8*)base - memory.offset_of(HtmlElement, global_event_handlers))
    # NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
    # NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
    if dom_is_window_reflecting_body_element_event_handler(event_name):
        let window = dom_document_window(dom_node_document(this.dom_node())) else return none
        return window.dom_event_target()

    return this.dom_event_target()

## Ported from HTMLBodyElement.cpp:167 Web::HTML::HTMLBodyElement::window_event_handlers_to_event_target (override).
pub func html_body_element_window_event_handlers_to_event_target_impl(base: HtmlWindowEventHandlers*) -> DomEventTarget*?:
    let this = (HtmlBodyElement*)((u8*)base - memory.offset_of(HtmlBodyElement, window_event_handlers))
    # All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
    # NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
    let window = dom_document_window(dom_node_document(this.dom_node())) else return none
    return window.dom_event_target()

# mark: Inline members of HTMLBodyElement.h ======================================================

## Ported from HTMLBodyElement.h:29 Web::HTML::HTMLBodyElement::default_role (override).
# https://www.w3.org/TR/html-aria/#el-body
pub func html_body_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    return AriaRole.generic

## Ported from HTMLBodyElement.h:37 Web::HTML::HTMLBodyElement::is_html_body_element (override).
pub func html_body_element_is_html_body_element_impl(base: DomNode*) -> bool:
    return true