Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / parser / value_parsing_5.lucb

#==============================================================================================
#
#   value_parsing_5 - parsing CSS values: color-mix(), light-dark(), <color>, corner shapes,
#                     counters, <ratio> and <string>
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/ValueParsing.cpp (lines 2082-2664): color-mix() (its
#       parse_component lambda is a function of its own), light-dark(), <color> (keywords,
#       functions, named and hex colors and the quirks-mode hashless hex color),
#       <corner-shape-value>, counter()/counters(), <counter-style-name>, <counter-style>, the
#       [ <integer> && <symbol> ] pair, <ratio> and <string>.
#
#       Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       (the full list of ValueParsing.cpp's copyright holders is in value_parsing_1)
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: color-mix() and light-dark() ============================================================

## The `parse_component` lambda of parse_color_mix_function: `<color> && <percentage [0,100]>?`.
func css_parser_parse_color_mix_component(this: CssParser*, function_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssColorMixStyleValueColorMixComponent?:
    css_syntax.token_stream_discard_whitespace(function_tokens)
    var percentage_style_value = css_parser_parse_percentage_value(this, function_tokens, CssNumericRange(min = 0.0, max = 100.0))
    css_syntax.token_stream_discard_whitespace(function_tokens)
    let color_style_value = css_parser_parse_color_value(this, function_tokens) else return none
    css_syntax.token_stream_discard_whitespace(function_tokens)
    if percentage_style_value == none:
        percentage_style_value = css_parser_parse_percentage_value(this, function_tokens, CssNumericRange(min = 0.0, max = 100.0))
        css_syntax.token_stream_discard_whitespace(function_tokens)
    return CssColorMixStyleValueColorMixComponent(
        color = color_style_value,
        percentage = percentage_style_value,
    )

## `component.percentage` is a literal percentage of 0 (not calculated).
func css_color_mix_component_has_zero_percentage(component: const CssColorMixStyleValueColorMixComponent*) -> bool:
    let percentage = component.percentage else return false
    if css_style_value_is_calculated(percentage):
        return false
    let value = css_percentage_style_value_percentage(css_style_value_as_percentage(percentage))
    return css_percentage_value(&value) == 0.0

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2083 Web::CSS::Parser::Parser::parse_color_mix_function.
# https://drafts.csswg.org/css-color-5/#color-mix
pub func css_parser_parse_color_mix_function(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # color-mix() = color-mix( <color-interpolation-method>? , [ <color> && <percentage [0,100]>? ]#)
    # FIXME: Update color-mix to accept 1+ colors instead of exactly 2.
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    let function = css_consume_color_function(tokens, "color-mix", none) else return none

    css_parser_push_temporary_value_parsing_context(this, css_function_context_of(&function))
    defer css_parser_pop_temporary_value_parsing_context(this)
    var function_tokens = css_syntax.token_stream_create(function.value)
    let color_interpolation_method = css_parser_parse_color_interpolation_method_value(this, &function_tokens)
    if color_interpolation_method != none:
        css_syntax.token_stream_discard_whitespace(&function_tokens)
        if not css_next_token_is_consumed(&function_tokens, .comma):
            return none

    let first_component = css_parser_parse_color_mix_component(this, &function_tokens) else return none
    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_next_token_is_consumed(&function_tokens, .comma):
        return none

    let second_component = css_parser_parse_color_mix_component(this, &function_tokens) else return none

    if css_color_mix_component_has_zero_percentage(&first_component) and css_color_mix_component_has_zero_percentage(&second_component):
        return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if css_syntax.token_stream_has_next_token(&function_tokens):
        return none

    css_syntax.state_transaction_commit(&transaction)
    var method: CssStyleValue*? = none
    if let interpolation_method = color_interpolation_method:
        method = interpolation_method.css_style_value()
    return css_color_mix_style_value_create(method, first_component, second_component).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2147 Web::CSS::Parser::Parser::parse_light_dark_color_value.
# https://drafts.csswg.org/css-color-5/#funcdef-light-dark
pub func css_parser_parse_light_dark_color_value(this: CssParser*, outer_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(outer_tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    css_syntax.token_stream_discard_whitespace(outer_tokens)
    let function = css_consume_color_function(outer_tokens, "light-dark", none) else return none

    var inner_tokens = css_syntax.token_stream_create(function.value)

    css_syntax.token_stream_discard_whitespace(&inner_tokens)
    let light = css_parser_parse_color_value(this, &inner_tokens) else return none

    css_syntax.token_stream_discard_whitespace(&inner_tokens)
    if not css_next_token_is_consumed(&inner_tokens, .comma):
        return none

    css_syntax.token_stream_discard_whitespace(&inner_tokens)
    let dark = css_parser_parse_color_value(this, &inner_tokens) else return none

    css_syntax.token_stream_discard_whitespace(&inner_tokens)
    if css_syntax.token_stream_has_next_token(&inner_tokens):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_light_dark_style_value_create(light, dark).css_style_value()

# mark: <color> =================================================================================

## `m_value_context[index].visit([](PropertyID const& property_id) { return property_has_quirk(property_id,
## Quirk::HashlessHexColor); }, [](auto const&) { return false; })`.
func css_value_context_allows_hashless_hex_color(context: const CssValueParsingContext*) -> bool:
    match *context:
        .property_id(property_id): return css_data.property_has_quirk(property_id, css_data.Quirk.hashless_hex_color)
        _: return false

## The keyword branch of parse_color_value (its own scope in C++): a keyword that has a color.
func css_parser_parse_color_keyword(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    if let keyword = css_parser_parse_keyword_value(this, tokens):
        if css_style_value_has_color(keyword):
            css_syntax.state_transaction_commit(&transaction)
            return keyword
    return none

## `Color::from_string(MUST(String::formatted("#{}", value)))`.
func css_color_from_hash_string(value: ak.StringView) -> gfx.Color?:
    let hash_string = css_string_formatted("#{}", [ak.arg_string_view(value)])
    return gfx.color_from_string(ak.string_bytes_as_string_view(&hash_string))

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2181 Web::CSS::Parser::Parser::parse_color_value.
# https://www.w3.org/TR/css-color-4/#color-syntax
pub func css_parser_parse_color_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:

    # Keywords: <system-color> | <deprecated-color> | currentColor
    if let keyword = css_parser_parse_color_keyword(this, tokens):
        return keyword

    # Functions
    if let color = css_parser_parse_color_function(this, tokens):
        return color

    if let color = css_parser_parse_color_mix_function(this, tokens):
        return color

    if let rgb = css_parser_parse_rgb_color_value(this, tokens):
        return rgb
    if let hsl = css_parser_parse_hsl_color_value(this, tokens):
        return hsl
    if let hwb = css_parser_parse_hwb_color_value(this, tokens):
        return hwb
    if let lab = css_parser_parse_lab_color_value(this, tokens):
        return lab
    if let lch = css_parser_parse_lch_color_value(this, tokens):
        return lch
    if let oklab = css_parser_parse_oklab_color_value(this, tokens):
        return oklab
    if let oklch = css_parser_parse_oklch_color_value(this, tokens):
        return oklch
    if let light_dark = css_parser_parse_light_dark_color_value(this, tokens):
        return light_dark

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    let component_value = css_syntax.token_stream_consume_a_token(tokens)

    if css_syntax.component_value_is(&component_value, .ident):
        let ident = css_component_value_ident(&component_value)

        if let color = gfx.color_from_string(ak.fly_string_bytes_as_string_view(&ident)):
            css_syntax.state_transaction_commit(&transaction)
            return css_color_style_value_create_from_color(color, CssColorSyntax.legacy, ident).css_style_value()
        # Otherwise, fall through to the hashless-hex-color case

    if css_syntax.component_value_is(&component_value, .hash_):
        let token = css_syntax.component_value_token(&component_value)
        let hash_value = css_syntax.token_hash_value(&token)
        if let color = css_color_from_hash_string(ak.fly_string_bytes_as_string_view(&hash_value)):
            css_syntax.state_transaction_commit(&transaction)
            return css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value()
        return none

    # https://drafts.csswg.org/css-color-4/#quirky-color
    if css_parser_in_quirks_mode(this):
        # "When CSS is being parsed in quirks mode, <quirky-color> is a type of <color> that is only valid in certain properties:"
        # (NOTE: List skipped for brevity; quirks data is assigned in Properties.json)
        # "It is not valid in properties that include or reference these properties, such as the background shorthand,
        # or inside functional notations such as color-mix()"

        var quirky_color_allowed = false
        if not ak.vector_is_empty(&this.m_value_context):
            quirky_color_allowed = css_value_context_allows_hashless_hex_color(ak.vector_first_const(&this.m_value_context))
        var i: usize = 1
        while i < ak.vector_size(&this.m_value_context) and quirky_color_allowed:
            quirky_color_allowed = css_value_context_allows_hashless_hex_color(ak.vector_at_const(&this.m_value_context, i))
            i += 1
        if quirky_color_allowed:
            # NOTE: This algorithm is no longer in the spec, since the concept got moved and renamed. However, it works,
            #       and so we might as well keep using it.

            # The value of a quirky color is obtained from the possible component values using the following algorithm,
            # aborting on the first step that returns a value:

            # 1. Let cv be the component value.
            let cv = component_value
            var serialization = ak.string_new()
            # 2. If cv is a <number-token> or a <dimension-token>, follow these substeps:
            if css_syntax.component_value_is(&cv, .number) or css_syntax.component_value_is(&cv, .dimension):
                let cv_token = css_syntax.component_value_token(&cv)
                # 1. If cv’s type flag is not "integer", return an error.
                #    This means that values that happen to use scientific notation, e.g., 5e5e5e, will fail to parse.
                if not css_syntax.token_is_integer(&cv_token):
                    return none

                # 2. If cv’s value is less than zero, return an error.
                let value = css_syntax.token_to_integer(&cv_token) if css_syntax.component_value_is(&cv, .number) else css_syntax.token_dimension_value_int(&cv_token)
                if value < 0:
                    return none

                # 3. Let serialization be the serialization of cv’s value, as a base-ten integer using digits 0-9 (U+0030 to U+0039) in the shortest form possible.
                var serialization_builder = ak.string_builder_new()
                css_appendff(&serialization_builder, "{}", [ak.arg_i64((i64)value)])

                # 4. If cv is a <dimension-token>, append the unit to serialization.
                if css_syntax.component_value_is(&cv, .dimension):
                    let unit_ = css_syntax.token_dimension_unit(&cv_token)
                    ak.string_builder_append_string_view(&serialization_builder, ak.fly_string_bytes_as_string_view(&unit_))

                # 5. If serialization consists of fewer than six characters, prepend zeros (U+0030) so that it becomes six characters.
                serialization = ak.string_builder_to_string(&serialization_builder) else trap("MUST: StringBuilder::to_string")
                if ak.string_builder_length(&serialization_builder) < 6:
                    var builder = ak.string_builder_new()
                    for j in 0..<(6 - ak.string_builder_length(&serialization_builder)):
                        _ = j
                        ak.string_builder_append(&builder, "0")
                    ak.string_builder_append_string_view(&builder, ak.string_builder_string_view(&serialization_builder))
                    serialization = ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
            # 3. Otherwise, cv is an <ident-token>; let serialization be cv’s value.
            else:
                if not css_syntax.component_value_is(&cv, .ident):
                    return none
                let ident = css_component_value_ident(&cv)
                serialization = ak.fly_string_to_string_const(&ident)

            # 4. If serialization does not consist of three or six characters, return an error.
            let serialization_view = ak.string_bytes_as_string_view(&serialization)
            if ak.string_view_length(&serialization_view) != 3 and ak.string_view_length(&serialization_view) != 6:
                return none

            # 5. If serialization contains any characters not in the range [0-9A-Fa-f] (U+0030 to U+0039, U+0041 to U+0046, U+0061 to U+0066), return an error.
            for c in ak.string_view_bytes(&serialization_view):
                if not ((c >= '0' and c <= '9') or (c >= 'A' and c <= 'F') or (c >= 'a' and c <= 'f')):
                    return none

            # 6. Return the concatenation of "#" (U+0023) and serialization.
            if let color = css_color_from_hash_string(serialization_view):
                css_syntax.state_transaction_commit(&transaction)
                return css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value()

    return none

# mark: <corner-shape-value> ====================================================================

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2329 Web::CSS::Parser::Parser::parse_corner_shape_value.
# https://drafts.csswg.org/css-borders-4/#typedef-corner-shape-value
pub func css_parser_parse_corner_shape_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <corner-shape-value> = round | scoop | bevel | notch | square | squircle | <superellipse()>
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    css_syntax.token_stream_discard_whitespace(tokens)

    let token = css_syntax.token_stream_consume_a_token(tokens)

    if css_syntax.component_value_is(&token, .ident):
        let ident = css_component_value_ident(&token)
        let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident)) else return none

        match keyword:
            .round, .scoop, .bevel, .notch, .square, .squircle:
                css_syntax.state_transaction_commit(&transaction)
                return css_keyword_style_value_create(keyword).css_style_value()
            _:
                return none

    if css_syntax.component_value_is_function_string_view(&token, ak.sv("superellipse")):
        # superellipse() = superellipse(<number> | infinity | -infinity)
        let function = css_syntax.component_value_function(&token)

        css_parser_push_temporary_value_parsing_context(this, css_function_context_of(&function))
        defer css_parser_pop_temporary_value_parsing_context(this)

        var function_tokens = css_syntax.token_stream_create(function.value)

        css_syntax.token_stream_discard_whitespace(&function_tokens)

        if css_parser_parse_all_as_single_keyword_value(this, &function_tokens, css_data.Keyword.negative_infinity) != none:
            css_syntax.state_transaction_commit(&transaction)
            return css_superellipse_style_value_create(css_number_style_value_create(-math.infinity).css_style_value()).css_style_value()

        if css_parser_parse_all_as_single_keyword_value(this, &function_tokens, css_data.Keyword.infinity) != none:
            css_syntax.state_transaction_commit(&transaction)
            return css_superellipse_style_value_create(css_number_style_value_create(math.infinity).css_style_value()).css_style_value()

        if let number_value = css_parser_parse_number_value(this, &function_tokens, css_infinite_range):
            css_syntax.token_stream_discard_whitespace(&function_tokens)

            if css_syntax.token_stream_has_next_token(&function_tokens):
                return none

            css_syntax.state_transaction_commit(&transaction)
            return css_superellipse_style_value_create(number_value).css_style_value()

    return none

# mark: Counters ================================================================================

## The `parse_counter_name` lambda of parse_counter_value.
func css_parser_parse_counter_name(this: CssParser*, name_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> ak.FlyString?:
    # https://drafts.csswg.org/css-lists-3/#typedef-counter-name
    # Counters are referred to in CSS syntax using the <counter-name> type, which represents
    # their name as a <custom-ident>. A <counter-name> name cannot match the keyword none;
    # such an identifier is invalid as a <counter-name>.
    var transaction = css_syntax.token_stream_begin_transaction(name_tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(name_tokens)

    let blacklist: ak.StringView[1] = [ak.sv("none")]
    let counter_name = css_parser_parse_custom_ident_value(this, name_tokens, blacklist) else return none

    css_syntax.token_stream_discard_whitespace(name_tokens)
    if css_syntax.token_stream_has_next_token(name_tokens):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_custom_ident_style_value_custom_ident(counter_name)

## The `parse_counter_style` lambda of parse_counter_value.
func css_parser_parse_counter_style(this: CssParser*, counter_style_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(counter_style_tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(counter_style_tokens)

    let counter_style = css_parser_parse_counter_style_value(this, counter_style_tokens) else return none

    css_syntax.token_stream_discard_whitespace(counter_style_tokens)
    if css_syntax.token_stream_has_next_token(counter_style_tokens):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return counter_style

## `CounterStyleStyleValue::create("decimal"_fly_string)`.
func css_decimal_counter_style_style_value() -> CssStyleValue*:
    return css_counter_style_style_value_create(FlyStringOrCssCounterStyleStyleValueSymbolsFunction.fly_string(ak.fly_string_from_str("decimal"))).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2386 Web::CSS::Parser::Parser::parse_counter_value.
# https://drafts.csswg.org/css-lists-3/#counter-functions
pub func css_parser_parse_counter_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    let token = css_syntax.token_stream_consume_a_token(tokens)
    if css_syntax.component_value_is_function_string_view(&token, ak.sv("counter")):
        # counter() = counter( <counter-name>, <counter-style>? )
        let function = css_syntax.component_value_function(&token)
        css_parser_push_temporary_value_parsing_context(this, css_function_context_of(&function))
        defer css_parser_pop_temporary_value_parsing_context(this)

        var function_tokens = css_syntax.token_stream_create(function.value)
        let function_values = css_parser_parse_a_comma_separated_list_of_component_values(this, &function_tokens)
        if ak.vector_is_empty(&function_values) or ak.vector_size(&function_values) > 2:
            return none

        var name_tokens = css_syntax.token_stream_create(*ak.vector_at_const(&function_values, 0))
        let counter_name = css_parser_parse_counter_name(this, &name_tokens) else return none

        var counter_style: CssStyleValue*? = none
        if ak.vector_size(&function_values) > 1:
            var counter_style_tokens = css_syntax.token_stream_create(*ak.vector_at_const(&function_values, 1))
            counter_style = css_parser_parse_counter_style(this, &counter_style_tokens)
            if counter_style == none:
                return none
        else:
            # In both cases, if the <counter-style> argument is omitted it defaults to `decimal`.
            counter_style = css_decimal_counter_style_style_value()

        css_syntax.state_transaction_commit(&transaction)
        return css_counter_style_value_create_counter(counter_name, counter_style else trap("VERIFY: a counter style")).css_style_value()

    if css_syntax.component_value_is_function_string_view(&token, ak.sv("counters")):
        # counters() = counters( <counter-name>, <string>, <counter-style>? )
        let function = css_syntax.component_value_function(&token)
        css_parser_push_temporary_value_parsing_context(this, css_function_context_of(&function))
        defer css_parser_pop_temporary_value_parsing_context(this)

        var function_tokens = css_syntax.token_stream_create(function.value)
        let function_values = css_parser_parse_a_comma_separated_list_of_component_values(this, &function_tokens)
        if ak.vector_size(&function_values) < 2 or ak.vector_size(&function_values) > 3:
            return none

        var name_tokens = css_syntax.token_stream_create(*ak.vector_at_const(&function_values, 0))
        let counter_name = css_parser_parse_counter_name(this, &name_tokens) else return none

        var string_tokens = css_syntax.token_stream_create(*ak.vector_at_const(&function_values, 1))
        css_syntax.token_stream_discard_whitespace(&string_tokens)
        let join_string = css_parser_parse_string_value(this, &string_tokens)
        css_syntax.token_stream_discard_whitespace(&string_tokens)
        if join_string == none or css_syntax.token_stream_has_next_token(&string_tokens):
            return none

        var counter_style: CssStyleValue*? = none
        if ak.vector_size(&function_values) > 2:
            var counter_style_tokens = css_syntax.token_stream_create(*ak.vector_at_const(&function_values, 2))
            counter_style = css_parser_parse_counter_style(this, &counter_style_tokens)
            if counter_style == none:
                return none
        else:
            # In both cases, if the <counter-style> argument is omitted it defaults to `decimal`.
            counter_style = css_decimal_counter_style_style_value()

        css_syntax.state_transaction_commit(&transaction)
        return css_counter_style_value_create_counters(counter_name, css_string_style_value_string_value(join_string else trap("VERIFY: a join string")), counter_style else trap("VERIFY: a counter style")).css_style_value()

    return none

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2497 Web::CSS::Parser::Parser::parse_counter_style_name.
# https://drafts.csswg.org/css-counter-styles-3/#typedef-counter-style-name
pub func css_parser_parse_counter_style_name(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> ak.FlyString?:
    # <counter-style-name> is a <custom-ident> that is not an ASCII case-insensitive match for none.
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    let blacklist: ak.StringView[1] = [ak.sv("none")]
    var custom_ident = css_parser_parse_custom_ident(this, tokens, blacklist) else return none

    # https://drafts.csswg.org/css-counter-styles-3/#the-counter-style-rule
    # Counter style names are case-sensitive. However, the names defined in this specification are ASCII lowercased
    # on parse wherever they are used as counter styles, e.g. in the list-style set of properties, in the
    # @counter-style rule, and in the counter() functions.

    # NB: The "names defined in this specification" are defined in the `CounterStyleNameKeyword` enum
    if let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&custom_ident)):
        if css_data.keyword_to_counter_style_name_keyword(keyword) != none:
            custom_ident = ak.fly_string_to_ascii_lowercase(&custom_ident)

    css_syntax.state_transaction_commit(&transaction)
    return custom_ident

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2522 Web::CSS::Parser::Parser::parse_counter_style_value.
# https://drafts.csswg.org/css-counter-styles-3/#typedef-counter-style
pub func css_parser_parse_counter_style_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <counter-style> = <counter-style-name> | <symbols()>
    # <symbols()> = symbols( <symbols-type>? [ <string> | <image> ]+ )
    # <symbols-type> = cyclic | numeric | alphabetic | symbolic | fixed
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    # <counter-style-name>
    if let counter_style_name = css_parser_parse_counter_style_name(this, tokens):
        css_syntax.state_transaction_commit(&transaction)
        return css_counter_style_style_value_create(FlyStringOrCssCounterStyleStyleValueSymbolsFunction.fly_string(counter_style_name)).css_style_value()

    # <symbols()>
    let maybe_function_token = css_syntax.token_stream_consume_a_token(tokens)

    if css_syntax.component_value_is_function_string_view(&maybe_function_token, ak.sv("symbols")):
        let function = css_syntax.component_value_function(&maybe_function_token)
        var argument_tokens = css_syntax.token_stream_create(function.value)

        # <symbols-type>?
        # NB: <symbols-type> defaults to symbolic if not provided.
        var symbols_type = css_data.SymbolsType.symbolic
        if let keyword = css_parser_parse_keyword_value(this, &argument_tokens):
            let maybe_symbols_type = css_data.keyword_to_symbols_type(css_style_value_to_keyword(keyword)) else return none

            symbols_type = maybe_symbols_type

        # [ <string> | <image> ]+
        # FIXME: In line with <symbol> we don't support <image> here - we may need to revisit this if other browsers
        #        implement it.
        var symbols = ak.Vector[ak.FlyString]()
        while css_syntax.token_stream_has_next_token(&argument_tokens):
            let maybe_string = css_parser_parse_string_value(this, &argument_tokens) else break

            ak.vector_append_t(&symbols, css_string_style_value_string_value(maybe_string))

        css_syntax.token_stream_discard_whitespace(&argument_tokens)

        if css_syntax.token_stream_has_next_token(&argument_tokens):
            return none

        # https://drafts.csswg.org/css-counter-styles-3/#symbols-function
        # If the system is alphabetic or numeric, there must be at least two <string>s or <image>s, or else the function is invalid.
        if ak.vector_is_empty(&symbols) or ((symbols_type == css_data.SymbolsType.alphabetic or symbols_type == css_data.SymbolsType.numeric) and ak.vector_size(&symbols) < 2):
            return none

        css_syntax.state_transaction_commit(&transaction)
        return css_counter_style_style_value_create(FlyStringOrCssCounterStyleStyleValueSymbolsFunction.css_counter_style_style_value_symbols_function(CssCounterStyleStyleValueSymbolsFunction(type_ = symbols_type, symbols = symbols))).css_style_value()

    return none

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2584 Web::CSS::Parser::Parser::parse_nonnegative_integer_symbol_pair_value.
pub func css_parser_parse_nonnegative_integer_symbol_pair_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    var integer: CssStyleValue*? = none
    var symbol: CssStyleValue*? = none

    while css_syntax.token_stream_has_next_token(tokens):
        if let integer_value = css_parser_parse_integer_value(this, tokens, css_non_negative_integer_range):
            if integer != none:
                return none

            integer = integer_value
            css_syntax.token_stream_discard_whitespace(tokens)
            continue

        if let symbol_value = css_parser_parse_symbol_value(this, tokens):
            if symbol != none:
                return none

            symbol = symbol_value
            css_syntax.token_stream_discard_whitespace(tokens)
            continue

        break

    let integer_value = integer else return none
    let symbol_value = symbol else return none

    css_syntax.state_transaction_commit(&transaction)

    var values = ak.Vector[CssStyleValue*]()
    ak.vector_append_t(&values, integer_value)
    ak.vector_append_t(&values, symbol_value)
    return css_style_value_list_create(values, CssStyleValueListSeparator.space).css_style_value()

# mark: <ratio> and <string> ====================================================================

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2623 Web::CSS::Parser::Parser::parse_ratio_value.
# https://drafts.csswg.org/css-values-4/#ratios
pub func css_parser_parse_ratio_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <ratio> = <number [0,∞]> [ / <number [0,∞]> ]?
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    css_syntax.token_stream_discard_whitespace(tokens)

    let numerator = css_parser_parse_number_value(this, tokens, css_non_negative_range) else return none

    css_syntax.token_stream_discard_whitespace(tokens)

    if css_next_token_is_delim(tokens, (u32)'/'):
        css_syntax.token_stream_discard_a_token(tokens)
        css_syntax.token_stream_discard_whitespace(tokens)

        let denominator = css_parser_parse_number_value(this, tokens, css_non_negative_range) else return none

        css_syntax.state_transaction_commit(&transaction)
        return css_ratio_style_value_create(numerator, denominator).css_style_value()

    css_syntax.state_transaction_commit(&transaction)
    # The second <number> is optional, defaulting to 1.
    return css_ratio_style_value_create(numerator, css_number_style_value_create(1.0).css_style_value()).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/ValueParsing.cpp:2654 Web::CSS::Parser::Parser::parse_string_value.
pub func css_parser_parse_string_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStringStyleValue*?:
    _ = this
    css_syntax.token_stream_discard_whitespace(tokens)
    let peek = css_syntax.token_stream_next_token(tokens)
    if css_syntax.component_value_is(&peek, .string):
        css_syntax.token_stream_discard_a_token(tokens)
        let token = css_syntax.component_value_token(&peek)
        return css_string_style_value_create(css_syntax.token_string(&token))

    return none