Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / math_ml / math_ml_element.lucb

#==============================================================================================
#
#   math_ml_element - MathML::MathMLElement: the base of every MathML element
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MathML/MathMLElement.cpp (lines 1-142) with the inline
#       members of MathMLElement.h: construction (which moves the mixin tables to
#       MathMLElement's, as C++'s vptrs move), the DOM steps shared with HTMLOrSVGElement, the
#       default ARIA role of <math>, and the presentational hints of the attributes common to
#       HTML and MathML elements and of the legacy MathML style attributes.
#
#       Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Construction =============================================================================

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:26 Web::MathML::MathMLElement::MathMLElement.
pub func math_ml_math_ml_element_construct(this: MathMlMathMlElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    dom_element_construct(this.dom_element(), document, qualified_name)
    # The bases after Element: GlobalEventHandlers (no state) and HTMLOrSVGElement<MathMLElement>
    # (its two fields start null and empty). The vptrs of every mixin base are MathMLElement's (a
    # subclass moves them on, DESIGN.md ยง2.5).
    this.element.slottable_mixin.vtable = &math_ml_math_ml_element_slottable_mixin_vtable
    this.element.aria_mixin.vtable = &math_ml_math_ml_element_aria_mixin_vtable
    this.element.animatable.vtable = &math_ml_math_ml_element_animatable_vtable
    this.global_event_handlers.vtable = &math_ml_math_ml_element_global_event_handlers_vtable
    this.global_event_handlers.class_id = this.cell().class_id

# mark: DOM steps ===============================================================================

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:31 Web::MathML::MathMLElement::attribute_changed (override).
pub func math_ml_math_ml_element_attribute_changed_impl(base: DomElement*, local_name: ak.FlyString, old_value: ak.String?, value: ak.String?,
                                                        namespace_: ak.FlyString?):
    let this = (MathMlMathMlElement*)base
    dom_element_attribute_changed_impl(base, local_name, old_value, value, namespace_)
    html_or_svg_element_attribute_changed(&this.html_or_svg_element, local_name, old_value, value, namespace_)

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:37 Web::MathML::MathMLElement::cloned (override).
pub func math_ml_math_ml_element_cloned_impl(base: DomNode*, node: DomNode*, clone_children: bool) -> !:
    let this = (MathMlMathMlElement*)base
    try dom_node_cloned_impl(base, node, clone_children)
    try html_or_svg_element_cloned(&this.html_or_svg_element, node, clone_children)
    return

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:44 Web::MathML::MathMLElement::inserted (override).
pub func math_ml_math_ml_element_inserted_impl(base: DomNode*):
    let this = (MathMlMathMlElement*)base
    dom_element_inserted_impl(base)
    html_or_svg_element_inserted(&this.html_or_svg_element)

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:50 Web::MathML::MathMLElement::initialize (override).
pub func math_ml_math_ml_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.math_ml_element)
    dom_element_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:56 Web::MathML::MathMLElement::default_role (override).
pub func math_ml_math_ml_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    let this = dom_element_from_aria_mixin(base)
    # https://www.w3.org/TR/html-aria/#el-math
    let local_name = dom_element_local_name(this)
    if ak.fly_string_eq_fly_string(&local_name, &math_ml_tag_names_math):
        return AriaRole.math
    return none

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:64 Web::MathML::MathMLElement::visit_edges (override).
pub func math_ml_math_ml_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (MathMlMathMlElement*)base
    dom_element_visit_edges_impl(base, visitor)
    html_or_svg_element_visit_edges(&this.html_or_svg_element, visitor)

## Ported from Libraries/LibWeb/MathML/MathMLElement.h:31 Web::MathML::MathMLElement::global_event_handlers_to_event_target (override).
pub func math_ml_math_ml_element_global_event_handlers_to_event_target_impl(base: HtmlGlobalEventHandlers*, arg0: ak.FlyString) -> DomEventTarget*?:
    return (DomEventTarget*)((u8*)base - memory.offset_of(MathMlMathMlElement, global_event_handlers))

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

## The `[&]() -> NonnullRefPtr<CSS::StyleValue const> { ... }()` of the scriptlevel hint: add(<U>) for a
## signed value, <U> otherwise.
func math_ml_scriptlevel_style_value(string_value: ak.StringView, integer_value: i32) -> CssStyleValue*:
    if ak.string_view_at(&string_value, 0) == '+' or ak.string_view_at(&string_value, 0) == '-':
        return css_function_style_value_create(ak.fly_string_from_str("add"), css_integer_style_value_create(integer_value).css_style_value()).css_style_value()
    return css_integer_style_value_create(integer_value).css_style_value()

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:70 Web::MathML::MathMLElement::is_presentational_hint (override).
pub func math_ml_math_ml_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
    return html_name_is_one_of(name, [math_ml_attribute_names_dir, math_ml_attribute_names_mathcolor, math_ml_attribute_names_mathbackground,
                                      math_ml_attribute_names_mathsize, math_ml_attribute_names_displaystyle, math_ml_attribute_names_scriptlevel])

## Ported from Libraries/LibWeb/MathML/MathMLElement.cpp:76 Web::MathML::MathMLElement::apply_presentational_hints (override).
pub func math_ml_math_ml_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    dom_element_apply_presentational_hints_impl(base, properties)
    let document = dom_node_document(base.dom_node())
    # 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)
        if html_name_is(name, math_ml_attribute_names_dir):
            # https://w3c.github.io/mathml-core/#attributes-common-to-html-and-mathml-elements
            # The dir attribute, if present, must be an ASCII case-insensitive match to ltr or rtl. In that case, the
            # user agent is expected to treat the attribute as a presentational hint setting the element's direction
            # property to the corresponding value. More precisely, an ASCII case-insensitive match to rtl is mapped to
            # rtl while an ASCII case-insensitive match to ltr is mapped to ltr.
            if html_value_is_ignoring_case(value, "ltr"):
                html_append_keyword_hint(properties, css_data.PropertyId.direction, css_data.Keyword.ltr)
            elif html_value_is_ignoring_case(value, "rtl"):
                html_append_keyword_hint(properties, css_data.PropertyId.direction, css_data.Keyword.rtl)
        elif html_name_is(name, math_ml_attribute_names_mathcolor):
            # https://w3c.github.io/mathml-core/#legacy-mathml-style-attributes
            # The mathcolor and mathbackground attributes, if present, must have a value that is a <color>. In that case,
            # the user agent is expected to treat these attributes as a presentational hint setting the element's color
            # and background-color properties to the corresponding values.
            if let parsed_value = parse_css_type(html_document_parsing_params(document), ak.string_bytes_as_string_view(&value), CssValueType.color):
                html_append_presentational_hint(properties, css_data.PropertyId.color, parsed_value)
        elif html_name_is(name, math_ml_attribute_names_mathbackground):
            if let parsed_value = parse_css_type(html_document_parsing_params(document), ak.string_bytes_as_string_view(&value), CssValueType.color):
                html_append_presentational_hint(properties, css_data.PropertyId.background_color, parsed_value)
        elif html_name_is(name, math_ml_attribute_names_mathsize):
            # https://w3c.github.io/mathml-core/#dfn-mathsize
            # The mathsize attribute, if present, must have a value that is a valid <length-percentage>.
            # In that case, the user agent is expected to treat the attribute as a presentational hint setting the
            # element's font-size property to the corresponding value.
            # NB: We parse the value as a font size, then filter out non LengthPercentage values, to ensure negative
            #     LengthPercentage values are rejected.
            if let parsed_value = parse_css_value(html_document_parsing_params(document), ak.string_bytes_as_string_view(&value), css_data.PropertyId.font_size):
                if css_style_value_is_length(parsed_value) or css_style_value_is_percentage(parsed_value) or css_style_value_is_calculated(parsed_value):
                    html_append_presentational_hint(properties, css_data.PropertyId.font_size, parsed_value)
        elif html_name_is(name, math_ml_attribute_names_displaystyle):
            # https://w3c.github.io/mathml-core/#dfn-displaystyle
            # The displaystyle attribute, if present, must have a value that is a boolean. In that case, the user agent
            # is expected to treat the attribute as a presentational hint setting the element's math-style property to
            # the corresponding value. More precisely, an ASCII case-insensitive match to true is mapped to normal while
            # an ASCII case-insensitive match to false is mapped to compact.
            if html_value_is_ignoring_case(value, "true"):
                html_append_keyword_hint(properties, css_data.PropertyId.math_style, css_data.Keyword.normal)
            elif html_value_is_ignoring_case(value, "false"):
                html_append_keyword_hint(properties, css_data.PropertyId.math_style, css_data.Keyword.compact)
        elif html_name_is(name, math_ml_attribute_names_scriptlevel):
            # https://w3c.github.io/mathml-core/#dfn-scriptlevel
            # The scriptlevel attribute, if present, must have value +<U>, -<U> or <U> where <U> is an unsigned-integer.
            # In that case the user agent is expected to treat the scriptlevel attribute as a presentational hint
            # setting the element's math-depth property to the corresponding value. More precisely, +<U>, -<U> and <U>
            # are respectively mapped to add(<U>) add(<-U>) and <U>.
            if let string_value = html_parse_integer_digits(ak.string_bytes_as_string_view(&value)):
                if let integer_value = ak.string_view_to_number_i32(&string_value, ak.TrimWhitespace.no):
                    let style_value = math_ml_scriptlevel_style_value(string_value, integer_value)
                    html_append_presentational_hint(properties, css_data.PropertyId.math_depth, style_value)