Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / math_ml / math_ml_mspace_element.lucb

#==============================================================================================
#
#   math_ml_mspace_element - MathML::MathMLMspaceElement: <mspace> and its width, height and
#                            depth presentational hints
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MathML/MathMLMspaceElement.cpp (lines 1-73): construction
#       (which moves the mixin tables to MathMLMspaceElement's, as C++'s vptrs move) and the
#       width, height and depth presentational hints.
#
#       Copyright (c) 2025, Lorenz Ackermann, <me@lorenzackermann.xyz>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/MathML/MathMLMspaceElement.cpp:17 Web::MathML::MathMLMspaceElement::MathMLMspaceElement.
pub func math_ml_math_ml_mspace_element_construct(this: MathMlMathMlMspaceElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    math_ml_math_ml_element_construct(this.math_ml_math_ml_element(), document, qualified_name)
    # The vptrs of the mixin bases move to MathMLMspaceElement's tables.
    let element = this.math_ml_math_ml_element()
    element.element.slottable_mixin.vtable = &math_ml_math_ml_mspace_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &math_ml_math_ml_mspace_element_aria_mixin_vtable
    element.element.animatable.vtable = &math_ml_math_ml_mspace_element_animatable_vtable
    element.global_event_handlers.vtable = &math_ml_math_ml_mspace_element_global_event_handlers_vtable

## Ported from Libraries/LibWeb/MathML/MathMLMspaceElement.cpp:22 Web::MathML::MathMLMspaceElement::is_presentational_hint (override).
pub func math_ml_math_ml_mspace_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
    if math_ml_math_ml_element_is_presentational_hint_impl(base, name):
        return true
    return html_name_is_one_of(name, [math_ml_attribute_names_width, math_ml_attribute_names_height, math_ml_attribute_names_depth])

## The `parse_non_percentage_value` lambda of apply_presentational_hints: the attribute
## `attribute_name` of `element` parsed as a <length>, or none.
func math_ml_parse_non_percentage_value(element: DomElement*, parsing_params: CssParsingParams, attribute_name: ak.FlyString) -> CssStyleValue*?:
    if let attribute = dom_element_attribute(element, attribute_name):
        if let value = parse_css_type(parsing_params, ak.string_bytes_as_string_view(&attribute), CssValueType.length):
            return value
    return none

## Ported from Libraries/LibWeb/MathML/MathMLMspaceElement.cpp:29 Web::MathML::MathMLMspaceElement::apply_presentational_hints (override).
pub func math_ml_math_ml_mspace_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    math_ml_math_ml_element_apply_presentational_hints_impl(base, properties)
    # https://w3c.github.io/mathml-core/#attribute-mspace-width
    # The width, height, depth, if present, must have a value that is a valid <length-percentage>.
    let parsing_params = html_document_parsing_params(dom_node_document(base.dom_node()))

    # If the width attribute is present, valid and not a percentage then that attribute is used as a presentational hint
    # setting the element's width property to the corresponding value.
    if let width_value = math_ml_parse_non_percentage_value(base, parsing_params, math_ml_attribute_names_width):
        html_append_presentational_hint(properties, css_data.PropertyId.width, width_value)

    # https://w3c.github.io/mathml-core/#attribute-mspace-height
    # If the height attribute is absent, invalid or a percentage then the requested line-ascent is 0. Otherwise the
    # requested line-ascent is the resolved value of the height attribute, clamping negative values to 0.
    let height_value = math_ml_parse_non_percentage_value(base, parsing_params, math_ml_attribute_names_height)
    # FIXME: set the line-ascent

    # If both the height and depth attributes are present, valid and not a percentage then they are used as a
    # presentational hint setting the element's height property to the concatenation of the
    # strings "calc(", the height attribute value, " + ", the depth attribute value, and ")". If only one of these
    # attributes is present, valid and not a percentage then it is treated as a presentational hint setting the
    # element's height property to the corresponding value.
    let depth_value = math_ml_parse_non_percentage_value(base, parsing_params, math_ml_attribute_names_depth)
    if height_value != none and depth_value != none:
        let height_attribute = dom_element_attribute(base, math_ml_attribute_names_height) else trap("VERIFY: Optional::value")
        let depth_attribute = dom_element_attribute(base, math_ml_attribute_names_depth) else trap("VERIFY: Optional::value")
        var builder = ak.string_builder_new()
        ak.string_builder_append(&builder, "calc(")
        ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&height_attribute))
        ak.string_builder_append(&builder, " + ")
        ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&depth_attribute))
        ak.string_builder_append(&builder, ")")
        let height_string = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
        if let calc_height_value = parse_css_type(parsing_params, ak.string_bytes_as_string_view(&height_string), CssValueType.length):
            html_append_presentational_hint(properties, css_data.PropertyId.height, calc_height_value)
    elif let height = height_value:
        html_append_presentational_hint(properties, css_data.PropertyId.height, height)
    elif let depth = depth_value:
        html_append_presentational_hint(properties, css_data.PropertyId.height, depth)