Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_hr_element.lucb

#==============================================================================================
#
#   html_hr_element - HTML::HTMLHRElement: <hr> and its presentational hints
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLHRElement.cpp (lines 1-115) and the default_role inline of
#       HTMLHRElement.h: construction (which moves the mixin tables to HTMLHRElement's, as C++'s
#       vptrs move), initialize and the align / color / noshade / size / width presentational
#       hints.
#
#==============================================================================================

## Ported from HTMLHRElement.cpp:22 Web::HTML::HTMLHRElement::HTMLHRElement.
pub func html_htmlhr_element_construct(this: HtmlHtmlhrElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLHRElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_htmlhr_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_htmlhr_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_htmlhr_element_animatable_vtable
    element.global_event_handlers.vtable = &html_htmlhr_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_htmlhr_element_form_associated_element_vtable

## Ported from HTMLHRElement.cpp:29 Web::HTML::HTMLHRElement::initialize (override).
pub func html_htmlhr_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.htmlhr_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLHRElement.cpp:35 Web::HTML::HTMLHRElement::is_presentational_hint (override).
pub func html_htmlhr_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[5] = [html_attribute_names_align, html_attribute_names_color, html_attribute_names_noshade, html_attribute_names_width, html_attribute_names_size]
    return html_name_is_one_of(name, names)

## Ported from HTMLHRElement.cpp:43 Web::HTML::HTMLHRElement::apply_presentational_hints (override).
pub func html_htmlhr_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    dom_element_apply_presentational_hints_impl(base, properties)
    # for_each_attribute([&](auto& name, auto& value) { ... })
    let attributes = base.m_attributes else return
    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)
        # https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2
        if html_name_is(name, html_attribute_names_color) or html_name_is(name, html_attribute_names_noshade):
            html_append_keyword_hint(properties, css_data.PropertyId.border_top_style, css_data.Keyword.solid)
            html_append_keyword_hint(properties, css_data.PropertyId.border_right_style, css_data.Keyword.solid)
            html_append_keyword_hint(properties, css_data.PropertyId.border_bottom_style, css_data.Keyword.solid)
            html_append_keyword_hint(properties, css_data.PropertyId.border_left_style, css_data.Keyword.solid)

        if html_name_is(name, html_attribute_names_align):
            if html_value_is_ignoring_case(value, "left"):
                html_append_px_hint(properties, css_data.PropertyId.margin_left, 0.0)
                html_append_keyword_hint(properties, css_data.PropertyId.margin_right, css_data.Keyword.auto)
            elif html_value_is_ignoring_case(value, "right"):
                html_append_keyword_hint(properties, css_data.PropertyId.margin_left, css_data.Keyword.auto)
                html_append_px_hint(properties, css_data.PropertyId.margin_right, 0.0)
            elif html_value_is_ignoring_case(value, "center"):
                html_append_keyword_hint(properties, css_data.PropertyId.margin_left, css_data.Keyword.auto)
                html_append_keyword_hint(properties, css_data.PropertyId.margin_right, css_data.Keyword.auto)

        # https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2:attr-hr-color-3
        # When an hr element has a color attribute, its value is expected to be parsed using the rules for parsing a legacy color value, and if that does not return failure,
        # the user agent is expected to treat the attribute as a presentational hint setting the element's 'color' property to the resulting color.
        if html_name_is(name, html_attribute_names_color):
            if let parsed_value = 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(parsed_value, CssColorSyntax.legacy).css_style_value())

        # If an hr element has either a color attribute or a noshade attribute, and furthermore also has a size attribute,
        # and parsing that attribute's value using the rules for parsing non-negative integers doesn't generate an error,
        # then the user agent is expected to use the parsed value divided by two as a pixel length for presentational hints
        # for the properties 'border-top-width', 'border-right-width', 'border-bottom-width', and 'border-left-width' on the element.
        let has_color_or_noshade = dom_element_has_attribute(base, html_attribute_names_color) or dom_element_has_attribute(base, html_attribute_names_noshade)
        if html_name_is(name, html_attribute_names_size) and has_color_or_noshade:
            if let parsed_value = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&value)):
                let size_value = css_length_style_value_create(css_length_make_px_f64(f64(parsed_value) / 2.0)).css_style_value()
                html_append_presentational_hint(properties, css_data.PropertyId.border_top_width, size_value)
                html_append_presentational_hint(properties, css_data.PropertyId.border_right_width, size_value)
                html_append_presentational_hint(properties, css_data.PropertyId.border_bottom_width, size_value)
                html_append_presentational_hint(properties, css_data.PropertyId.border_left_width, size_value)
        elif html_name_is(name, html_attribute_names_size) and not has_color_or_noshade:
            # Otherwise, if an hr element has neither a color attribute nor a noshade attribute, but does have a size attribute,
            # and parsing that attribute's value using the rules for parsing non-negative integers doesn't generate an error,
            # then: if the parsed value is one, then the user agent is expected to use the attribute as a presentational hint
            # setting the element's 'border-bottom-width' to 0; otherwise, if the parsed value is greater than one,
            # then the user agent is expected to use the parsed value minus two as a pixel length for presentational hints
            # for the 'height' property on the element.
            if let parsed_value = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&value)):
                if parsed_value == 1:
                    html_append_px_hint(properties, css_data.PropertyId.border_bottom_width, 0.0)
                elif parsed_value > 1:
                    # make_px(parsed_value.value() - 2): the u32 subtraction, then the conversion to double.
                    html_append_px_hint(properties, css_data.PropertyId.height, f64(parsed_value - 2))

        # https://html.spec.whatwg.org/multipage/rendering.html#the-hr-element-2:maps-to-the-dimension-property
        if html_name_is(name, html_attribute_names_width):
            if let parsed_value = html_parse_dimension_value(ak.string_bytes_as_string_view(&value)):
                html_append_presentational_hint(properties, css_data.PropertyId.width, parsed_value)

## Ported from HTMLHRElement.h:22 Web::HTML::HTMLHRElement::default_role (override).
# https://www.w3.org/TR/html-aria/#el-hr
pub func html_htmlhr_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    return AriaRole.separator