Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / svg / svg_animated_number.lucb

#==============================================================================================
#
#   svg_animated_number - SVG::SVGAnimatedNumber: a reflected number attribute
#
#   DESCRIPTION:
#       Port of LibWeb/SVG/SVGAnimatedNumber.cpp (lines 1-166): an SVGAnimatedNumber reflects
#       a number (or a number-optional-number) content attribute of its element
#       (https://svgwg.org/svg2-draft/types.html#InterfaceSVGAnimatedNumber).
#
#==============================================================================================

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:17 Web::SVG::SVGAnimatedNumber::create.
pub func svg_animated_number_create(realm: Realm*, element: SvgElement*, reflected_attribute: DomQualifiedName, initial_value: f32, supports_second_value: SvgAnimatedNumberSupportsSecondValue = SvgAnimatedNumberSupportsSecondValue.no, value_represented: SvgAnimatedNumberValueRepresented = SvgAnimatedNumberValueRepresented.first) -> SvgAnimatedNumber*:
    return realm_create_svg_animated_number(realm, realm, element, reflected_attribute, initial_value, supports_second_value, value_represented)

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:29 Web::SVG::SVGAnimatedNumber::SVGAnimatedNumber.
pub func svg_animated_number_construct(this: SvgAnimatedNumber*, realm: Realm*, element: SvgElement*, reflected_attribute: DomQualifiedName, initial_value: f32, supports_second_value: SvgAnimatedNumberSupportsSecondValue, value_represented: SvgAnimatedNumberValueRepresented):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)
    this.m_element = element
    this.m_reflected_attribute = reflected_attribute
    this.m_initial_value = initial_value
    this.m_supports_second_value = supports_second_value
    this.m_value_represented = value_represented

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:48 Web::SVG::SVGAnimatedNumber::base_val.
# https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
pub func svg_animated_number_base_val(this: SvgAnimatedNumber*) -> f32:
    # On getting baseVal or animVal, the following steps are run:
    return svg_animated_number_get_base_or_anim_value(this)

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:55 Web::SVG::SVGAnimatedNumber::set_base_val.
# https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
pub func svg_animated_number_set_base_val(this: SvgAnimatedNumber*, new_value: f32):
    # 1. Let value be the value being assigned to baseVal.
    let value = new_value

    # 2. Let new be a list of numbers.
    var new_ = ak.Vector[f32]()

    # 3. If the reflected attribute is defined to take an number followed by an optional second number, then:
    if this.m_supports_second_value == SvgAnimatedNumberSupportsSecondValue.yes:
        # 1. Let current be the value of the reflected attribute (using the attribute's initial value if it is not
        #    present or invalid).
        let current = dom_element_get_attribute_value(this.m_element.dom_element(), dom_qualified_name_local_name(&this.m_reflected_attribute),
                                                      dom_qualified_name_namespace(&this.m_reflected_attribute))
        let current_values = ak.string_split(&current, 0x20) else trap("MUST: String::split")

        # 2. Let first be the first number in current.
        var first = this.m_initial_value
        if ak.vector_size(&current_values) > 0:
            first = svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(ak.vector_at_const(&current_values, 0)))

        # 3. Let second be the second number in current if it has been explicitly specified, and if not, the implicit
        #    value as described in the definition of the attribute.
        # NB: All known usages of <number-optional-number> specify that a missing second number defaults to the value
        #     of the first number.
        var second = first
        if ak.vector_size(&current_values) > 1 and not ak.string_is_empty(ak.vector_at_const(&current_values, 1)):
            second = svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(ak.vector_at_const(&current_values, 1)))

        # 4. If this SVGAnimatedNumber object reflects the first number, then set first to value. Otherwise, set second
        #    to value.
        if this.m_value_represented == SvgAnimatedNumberValueRepresented.first:
            first = value
        else:
            second = value

        # 5. Append first to new.
        ak.vector_append_t(&new_, first)

        # 6. Append second to new.
        ak.vector_append_t(&new_, second)
    else:
        # 4. Otherwise, the reflected attribute is defined to take a single number value. Append value to new.
        ak.vector_append_t(&new_, value)

    # 5. Set the content attribute to a string consisting of each number in new serialized to an implementation
    #    specific string that, if parsed as an <number> using CSS syntax, would return the value closest to the number
    #    (given the implementation's supported Precisionreal number precision),
    #    joined and separated by a single U+0020 SPACE character.
    var builder = ak.string_builder_new()
    var first_item = true
    for item in ak.vector_span_const(&new_):
        if not first_item:
            ak.string_builder_append_u8(&builder, ' ')
        first_item = false
        ak.format_f32(&builder, item)
    let new_attribute_value = ak.string_builder_to_string_without_validation(&builder)
    dom_element_set_attribute_value(this.m_element.dom_element(), dom_qualified_name_local_name(&this.m_reflected_attribute), new_attribute_value,
                                    dom_qualified_name_prefix(&this.m_reflected_attribute), dom_qualified_name_namespace(&this.m_reflected_attribute))

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:109 Web::SVG::SVGAnimatedNumber::anim_val.
# https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__animVal
pub func svg_animated_number_anim_val(this: SvgAnimatedNumber*) -> f32:
    # On getting baseVal or animVal, the following steps are run:
    return svg_animated_number_get_base_or_anim_value(this)

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:115 Web::SVG::SVGAnimatedNumber::parse_value_or_initial.
func svg_animated_number_parse_value_or_initial(this: SvgAnimatedNumber*, number_value: ak.StringView) -> f32:
    let value = svg_attribute_parser_parse_number_percentage(number_value) else return this.m_initial_value
    return svg_number_percentage_value(&value)

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:124 Web::SVG::SVGAnimatedNumber::get_base_or_anim_value.
# https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
func svg_animated_number_get_base_or_anim_value(this: SvgAnimatedNumber*) -> f32:
    # 1. Let value be the value of the reflected attribute (using the attribute's initial value if it is not present or
    #    invalid).
    let value = dom_element_get_attribute_value(this.m_element.dom_element(), dom_qualified_name_local_name(&this.m_reflected_attribute),
                                                dom_qualified_name_namespace(&this.m_reflected_attribute))

    # 2. If the reflected attribute is defined to take an number followed by an optional second number, then:
    if this.m_supports_second_value == SvgAnimatedNumberSupportsSecondValue.yes:
        # 1. If this SVGAnimatedNumber object reflects the first number, then return the first value in value.
        let values = ak.string_split(&value, 0x20) else trap("MUST: String::split")
        if ak.vector_is_empty(&values):
            return this.m_initial_value
        if this.m_value_represented == SvgAnimatedNumberValueRepresented.first:
            return svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(ak.vector_at_const(&values, 0)))

        # 2. Otherwise, this SVGAnimatedNumber object reflects the second number. Return the second value in value if
        #    it has been explicitly specified, and if not, return the implicit value as described in the definition of
        #    the attribute.
        # NB: All known usages of <number-optional-number> specify that a missing second number defaults to the value
        #     of the first number.
        assert(this.m_value_represented == SvgAnimatedNumberValueRepresented.second)
        if ak.vector_size(&values) > 1 and not ak.string_is_empty(ak.vector_at_const(&values, 1)):
            return svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(ak.vector_at_const(&values, 1)))
        return svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(ak.vector_at_const(&values, 0)))

    # 3. Otherwise, the reflected attribute is defined to take a single number value. Return value.
    return svg_animated_number_parse_value_or_initial(this, ak.string_bytes_as_string_view(&value))

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:154 Web::SVG::SVGAnimatedNumber::initialize (override).
pub func svg_animated_number_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.svg_animated_number)
    js_cell_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/SVG/SVGAnimatedNumber.cpp:160 Web::SVG::SVGAnimatedNumber::visit_edges (override).
pub func svg_animated_number_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (SvgAnimatedNumber*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_element)