Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_font_element.lucb

#==============================================================================================
#
#   html_font_element - HTML::HTMLFontElement: <font> and its presentational hints
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLFontElement.cpp (lines 1-150): the rules for parsing a legacy
#       font size, construction (which moves the mixin tables to HTMLFontElement's, as C++'s
#       vptrs move), initialize and the color / size / face presentational hints.
#
#==============================================================================================

## HTMLFontElement.cpp's file-local `enum class Mode` of parse_legacy_font_size.
enum HtmlFontElementMode:
    relative_plus
    relative_minus
    absolute

## Ported from HTMLFontElement.cpp:28 Web::HTML::HTMLFontElement::parse_legacy_font_size.
# https://html.spec.whatwg.org/multipage/rendering.html#rules-for-parsing-a-legacy-font-size
pub func html_font_element_parse_legacy_font_size(string: ak.StringView) -> css_data.Keyword?:
    let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
    let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)

    # 1. Let input be the attribute's value.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var lexer = ak.generic_lexer_new(string)

    # 3. Skip ASCII whitespace within input given position.
    ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace)

    # 4. If position is past the end of input, there is no presentational hint. Return.
    if ak.generic_lexer_is_eof(&lexer):
        return none

    # 5. If the character at position is a U+002B PLUS SIGN character (+), then let mode be relative-plus, and advance position to the next character. Otherwise, if the character at position is a U+002D HYPHEN-MINUS character (-), then let mode be relative-minus, and advance position to the next character. Otherwise, let mode be absolute.
    var mode = HtmlFontElementMode.absolute

    if ak.generic_lexer_peek(&lexer) == '+':
        mode = HtmlFontElementMode.relative_plus
        _ = ak.generic_lexer_consume(&lexer)
    elif ak.generic_lexer_peek(&lexer) == '-':
        mode = HtmlFontElementMode.relative_minus
        _ = ak.generic_lexer_consume(&lexer)

    # 6. Collect a sequence of code points that are ASCII digits from input given position, and let digits be the resulting sequence.
    let start_index = ak.generic_lexer_tell(&lexer)
    _ = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
    let end_index = ak.generic_lexer_tell(&lexer)
    let input = ak.generic_lexer_input(&lexer)
    let digits = ak.string_view_substring_view_usize(&input, start_index, end_index - start_index)
    let value_or_empty = ak.string_view_to_number_i32(&digits)

    # 7. If digits is the empty string, there is no presentational hint. Return.
    if value_or_empty == none:
        return none

    # 8. Interpret digits as a base-ten integer. Let value be the resulting number.
    var value = value_or_empty else trap("unreachable")

    # 9. If mode is relative-plus, then increment value by 3. If mode is relative-minus, then let value be the result of subtracting value from 3.
    # (C++'s i32 arithmetic, which wraps.)
    if mode == HtmlFontElementMode.relative_plus:
        value = value +% 3
    elif mode == HtmlFontElementMode.relative_minus:
        value = 3 -% value

    # 10. If value is greater than 7, let it be 7.
    if value > 7:
        value = 7

    # 11. If value is less than 1, let it be 1.
    if value < 1:
        value = 1

    # 12. Set the 'font-size' property to the keyword corresponding to the value of value according to the following table:
    match value:
        1:
            return css_data.Keyword.x_small
        2:
            return css_data.Keyword.small
        3:
            return css_data.Keyword.medium
        4:
            return css_data.Keyword.large
        5:
            return css_data.Keyword.x_large
        6:
            return css_data.Keyword.xx_large
        7:
            return css_data.Keyword.xxx_large
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from HTMLFontElement.cpp:101 Web::HTML::HTMLFontElement::HTMLFontElement.
pub func html_font_element_construct(this: HtmlFontElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLFontElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_font_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_font_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_font_element_animatable_vtable
    element.global_event_handlers.vtable = &html_font_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_font_element_form_associated_element_vtable

## Ported from HTMLFontElement.cpp:108 Web::HTML::HTMLFontElement::initialize (override).
pub func html_font_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_font_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLFontElement.cpp:114 Web::HTML::HTMLFontElement::is_presentational_hint (override).
pub func html_font_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[3] = [html_attribute_names_color, html_attribute_names_face, html_attribute_names_size]
    return html_name_is_one_of(name, names)

## Ported from HTMLFontElement.cpp:125 Web::HTML::HTMLFontElement::apply_presentational_hints (override).
pub func html_font_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, html_attribute_names_color):
            # https://html.spec.whatwg.org/multipage/rendering.html#phrasing-content-3: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.color, css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value())
        elif html_name_is(name, html_attribute_names_size):
            # When a font element has a size attribute, the user agent is expected to use the following steps, known as the rules for parsing a legacy font size, to treat the attribute as a presentational hint setting the element's 'font-size' property:
            if let font_size_keyword = html_font_element_parse_legacy_font_size(ak.string_bytes_as_string_view(&value)):
                let font_size = css_data.string_from_keyword(font_size_keyword)
                var params: CssParsingParams = ---
                css_parsing_params_construct_dom_document(&params, document)
                if let parsed_value = parse_css_value(params, font_size, css_data.PropertyId.font_size):
                    html_append_presentational_hint(properties, css_data.PropertyId.font_size, parsed_value)
        elif html_name_is(name, html_attribute_names_face):
            # When a font element has a face attribute, the user agent is expected to treat the attribute as a presentational hint setting the element's 'font-family' property to the attribute's value.
            var params: CssParsingParams = ---
            css_parsing_params_construct_dom_document(&params, document)
            if let parsed_value = parse_css_value(params, ak.string_bytes_as_string_view(&value), css_data.PropertyId.font_family):
                html_append_presentational_hint(properties, css_data.PropertyId.font_family, parsed_value)