Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / style_values / counter_style_value.lucb

#==============================================================================================
#
#   counter_style_value - CounterStyleValue: counter() and counters()
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/CounterStyleValue.cpp (lines 1-112) and the inline members
#       of CounterStyleValue.h: resolving the counters of an element into text, and serialization.
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2024-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:22 Web::CSS::CounterStyleValue::create_counter.
pub func css_counter_style_value_create_counter(counter_name: ak.FlyString, counter_style: CssStyleValue*) -> CssCounterStyleValue*:
    let this = &(new CssCounterStyleValue[1] --- else trap("out of memory"))[0]
    css_counter_style_value_construct(this, .counter, counter_name, counter_style, ak.FlyString())
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:26 Web::CSS::CounterStyleValue::create_counters.
pub func css_counter_style_value_create_counters(counter_name: ak.FlyString, join_string: ak.FlyString, counter_style: CssStyleValue*) -> CssCounterStyleValue*:
    let this = &(new CssCounterStyleValue[1] --- else trap("out of memory"))[0]
    css_counter_style_value_construct(this, .counters, counter_name, counter_style, join_string)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.cpp:23 Web::CSS::CounterStyleValue::CounterStyleValue.
pub func css_counter_style_value_construct(this: CssCounterStyleValue*, function: CssCounterStyleValueCounterFunction, counter_name: ak.FlyString, counter_style: CssStyleValue*, join_string: ak.FlyString):
    css_style_value_construct(this.css_style_value(), CssStyleValueType.counter)
    css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_counter_style_value_vtable, &css_counter_style_value_class)
    this.m_properties = CssCounterStyleValueProperties(
        function = function,
        counter_name = counter_name,
        counter_style = counter_style,
        join_string = join_string,
    )

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:32 Web::CSS::CounterStyleValue::function_type.
pub func css_counter_style_value_function_type(this: CssCounterStyleValue*) -> CssCounterStyleValueCounterFunction:
    return this.m_properties.function

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:33 Web::CSS::CounterStyleValue::counter_name.
pub func css_counter_style_value_counter_name(this: CssCounterStyleValue*) -> ak.FlyString:
    return this.m_properties.counter_name

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:34 Web::CSS::CounterStyleValue::counter_style.
pub func css_counter_style_value_counter_style(this: CssCounterStyleValue*) -> CssStyleValue*:
    return this.m_properties.counter_style

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:35 Web::CSS::CounterStyleValue::join_string.
pub func css_counter_style_value_join_string(this: CssCounterStyleValue*) -> ak.FlyString:
    return this.m_properties.join_string

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.cpp:36 Web::CSS::CounterStyleValue::resolve.
pub func css_counter_style_value_resolve(this: CssCounterStyleValue*, element_reference: DomAbstractElement*) -> ak.String:
    # "If no counter named <counter-name> exists on an element where counter() or counters() is used,
    # one is first instantiated with a starting value of 0."
    let counters_set = dom_abstract_element_ensure_counters_set(element_reference)
    if css_counters_set_last_counter_with_name(counters_set, this.m_properties.counter_name) == none:
        _ = css_counters_set_instantiate_a_counter(counters_set, this.m_properties.counter_name, *element_reference, false, 0)

    # counter( <counter-name>, <counter-style>? )
    # "Represents the value of the innermost counter in the element’s CSS counters set named <counter-name>
    # using the counter style named <counter-style>."
    if this.m_properties.function == CssCounterStyleValueCounterFunction.counter:
        # NOTE: This should always be present because of the handling of a missing counter above.
        let counter = css_counters_set_last_counter_with_name(counters_set, this.m_properties.counter_name) else trap("VERIFY: Optional::value()")
        let style_scope = dom_abstract_element_style_scope(element_reference)
        let counter_style = css_counter_style_style_value_resolve_counter_style(css_style_value_as_counter_style(this.m_properties.counter_style), style_scope)
        return css_generate_a_counter_representation(counter_style, style_scope, counter.value else 0)

    # counters( <counter-name>, <string>, <counter-style>? )
    # "Represents the values of all the counters in the element’s CSS counters set named <counter-name>
    # using the counter style named <counter-style>, sorted in outermost-first to innermost-last order
    # and joined by the specified <string>."
    # NOTE: The way counters sets are inherited, this should be the order they appear in the counters set.
    var stb = ak.string_builder_new()
    let counters = css_counters_set_counters(counters_set)
    for counter in ak.vector_span_const(&counters):
        if not ak.fly_string_eq_fly_string(&counter.name, &this.m_properties.counter_name):
            continue

        let style_scope = dom_abstract_element_style_scope(element_reference)
        let counter_style = css_counter_style_style_value_resolve_counter_style(css_style_value_as_counter_style(this.m_properties.counter_style), style_scope)
        let counter_string = css_generate_a_counter_representation(counter_style, style_scope, counter.value else 0)
        if not ak.string_builder_is_empty(&stb):
            ak.string_builder_append_string_view(&stb, ak.fly_string_bytes_as_string_view(&this.m_properties.join_string))
        ak.string_builder_append_string_view(&stb, ak.string_bytes_as_string_view(&counter_string))
    return ak.string_builder_to_string_without_validation(&stb)

## serialize's lambda: `item->serialize(b, mode)` (its context is the SerializationMode).
func css_counter_style_value_serialize_item(context: void*?, builder: ak.StringBuilder*, item: const (CssStyleValue*?)*) -> unit:
    let mode = *(const css_syntax.SerializationMode*)(context else trap("no mode"))
    css_style_value_serialize(*item else trap("RefPtr: null"), builder, mode)

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.cpp:74 Web::CSS::CounterStyleValue::serialize (override).
# https://drafts.csswg.org/cssom-1/#ref-for-typedef-counter
pub func css_counter_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let this = (CssCounterStyleValue*)base
    # The return value of the following algorithm:
    # 1. Let s be the empty string.
    # (We use builder instead)

    # 2. If <counter> has three CSS component values append the string "counters(" to s.
    if this.m_properties.function == CssCounterStyleValueCounterFunction.counters:
        ak.string_builder_append(builder, "counters(")
    # 3. If <counter> has two CSS component values append the string "counter(" to s.
    elif this.m_properties.function == CssCounterStyleValueCounterFunction.counter:
        ak.string_builder_append(builder, "counter(")

    # 4. Let list be a list of CSS component values belonging to <counter>,
    #    omitting the last CSS component value if it is "decimal".
    var list = ak.Vector[CssStyleValue*?]()
    ak.vector_append_t(&list, css_custom_ident_style_value_create(this.m_properties.counter_name).css_style_value())
    if this.m_properties.function == CssCounterStyleValueCounterFunction.counters:
        ak.vector_append_t(&list, css_string_style_value_create(this.m_properties.join_string).css_style_value())
    let counter_style_text = css_style_value_to_string(this.m_properties.counter_style, mode)
    if not ak.string_eq_string_view(&counter_style_text, ak.sv("decimal")):
        ak.vector_append_t(&list, this.m_properties.counter_style)

    # 5. Let each item in list be the result of invoking serialize a CSS component value on that item.
    # 6. Append the result of invoking serialize a comma-separated list on list to s.
    var serialization_mode = mode
    css_serialize_a_comma_separated_list(builder, &list, ak.Function2[ak.StringBuilder*, const (CssStyleValue*?)*, unit](call = css_counter_style_value_serialize_item, context = &serialization_mode))

    # 7. Append ")" (U+0029) to s.
    ak.string_builder_append(builder, ")")

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.cpp:107 Web::CSS::CounterStyleValue::properties_equal.
pub func css_counter_style_value_properties_equal(this: CssCounterStyleValue*, other: CssCounterStyleValue*) -> bool:
    # Properties' defaulted operator==.
    return (this.m_properties.function == other.m_properties.function and ak.fly_string_eq_fly_string(&this.m_properties.counter_name, &other.m_properties.counter_name) and css_style_values_equal(this.m_properties.counter_style, other.m_properties.counter_style) and ak.fly_string_eq_fly_string(&this.m_properties.join_string, &other.m_properties.join_string))

## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h:43 Web::CSS::CounterStyleValue::is_computationally_independent (override).
pub func css_counter_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
    let this = (CssCounterStyleValue*)base
    return css_style_value_is_computationally_independent(this.m_properties.counter_style)

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::CounterStyleValue (CRTP helper) (override).
pub func css_counter_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
    if css_style_value_type(base) != css_style_value_type(other):
        return false
    return css_counter_style_value_properties_equal((CssCounterStyleValue*)base, (CssCounterStyleValue*)other)