Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   property_parsing_3 - parsing property values: color-scheme, counters, cursor, anchors,
#                        aspect-ratio, animation, background-position
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 780-1120).
#
#==============================================================================================

# mark: Shared callbacks ========================================================================

## `[this](TokenStream<ComponentValue>& inner_tokens) -> RefPtr<StyleValue const> { return parse_dashed_ident_value(inner_tokens); }`
## (the context is the parser).
func css_parser_parse_dashed_ident_callback(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let this = (CssParser*)(context else trap("no parser"))
    return (CssStyleValue*?)css_parser_parse_dashed_ident_value(this, tokens)

## `parse_comma_separated_value_list(tokens, <callback with the parser as context>)` as a StyleValue.
func css_parser_parse_comma_separated_values(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*,
                                             callback: func(void*?, css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?) -> CssStyleValue*?:
    return (CssStyleValue*?)css_parser_parse_comma_separated_value_list(this, tokens, ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssStyleValue*?](call = callback, context = this))

## `tokens.consume_a_token().is_ident(ident)`.
func css_consume_a_token_is_ident(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, ident: str) -> bool:
    let token = css_syntax.token_stream_consume_a_token(tokens)
    return css_syntax.component_value_is_ident(&token, ak.sv(ident))

# mark: color-scheme ============================================================================

## The `// normal` block of parse_color_scheme_value: `normal` as the whole value, in a transaction.
func css_parse_color_scheme_normal(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, done: bool*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    if css_consume_a_token_is_ident(tokens, "normal"):
        *done = true
        if css_syntax.token_stream_has_next_token(tokens):
            return none
        css_syntax.state_transaction_commit(&transaction)
        return css_color_scheme_style_value_normal().css_style_value()
    return none

## The `only?` blocks of parse_color_scheme_value: whether an `only` keyword was consumed, in a transaction.
func css_parse_color_scheme_only(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> bool:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    if css_consume_a_token_is_ident(tokens, "only"):
        css_syntax.state_transaction_commit(&transaction)
        return true
    return false

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:780 Web::CSS::Parser::Parser::parse_color_scheme_value.
pub func css_parser_parse_color_scheme_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # normal | [ light | dark | <custom-ident> ]+ && only?

    # normal
    var normal_returned = false
    let normal = css_parse_color_scheme_normal(tokens, &normal_returned)
    if normal_returned:
        return normal

    var only = false
    var schemes = ak.Vector[ak.String]()

    # only? && (..)
    if css_parse_color_scheme_only(tokens):
        only = true

    # [ light | dark | <custom-ident> ]+
    css_syntax.token_stream_discard_whitespace(tokens)
    while css_syntax.token_stream_has_next_token(tokens):
        var transaction = css_syntax.token_stream_begin_transaction(tokens)
        defer css_syntax.state_transaction_destroy(&transaction)

        # The 'normal', 'light', 'dark', and 'only' keywords are not valid <custom-ident>s in this property.
        # Note: only 'normal' is blacklisted here because 'light' and 'dark' aren't parsed differently and 'only' is checked for afterwards
        let blacklist: ak.StringView[1] = [ak.sv("normal")]
        let ident = css_parser_parse_custom_ident_value(this, tokens, blacklist) else return none

        let custom_ident = css_custom_ident_style_value_custom_ident(ident)
        if ak.fly_string_eq_string_view(&custom_ident, ak.sv("only")):
            break

        ak.vector_append_t(&schemes, ak.fly_string_to_string_const(&custom_ident))
        css_syntax.token_stream_discard_whitespace(tokens)
        css_syntax.state_transaction_commit(&transaction)

    # (..) && only?
    if not only:
        if css_parse_color_scheme_only(tokens):
            only = true

    css_syntax.token_stream_discard_whitespace(tokens)
    if css_syntax.token_stream_has_next_token(tokens) or ak.vector_is_empty(&schemes):
        return none

    return css_color_scheme_style_value_create(schemes, only).css_style_value()

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

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:845 Web::CSS::Parser::Parser::parse_counter_definitions_value.
pub func css_parser_parse_counter_definitions_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, allow_reversed: CssParserAllowReversed,
                                                    default_value_if_not_reversed: i32) -> CssStyleValue*?:
    # If AllowReversed is Yes, parses:
    #   [ <counter-name> <integer>? | <reversed-counter-name> <integer>? ]+
    # Otherwise parses:
    #   [ <counter-name> <integer>? ]+

    # FIXME: This disabled parsing of `reversed()` counters. Remove this line once they're supported.
    _ = allow_reversed
    let allow_reversed_ = CssParserAllowReversed.no

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

    var counter_definitions = ak.Vector[CssCounterDefinition]()
    while css_syntax.token_stream_has_next_token(tokens):
        var per_item_transaction = css_syntax.token_stream_begin_transaction(tokens)
        defer css_syntax.state_transaction_destroy(&per_item_transaction)
        var definition = CssCounterDefinition()

        # <counter-name> | <reversed-counter-name>
        let token = css_syntax.token_stream_next_token(tokens)

        # A <counter-name> name cannot match the keyword none; such an identifier is invalid as a <counter-name>.
        let blacklist: ak.StringView[1] = [ak.sv("none")]
        if let counter_name = css_parser_parse_custom_ident_value(this, tokens, blacklist):
            definition.name = css_custom_ident_style_value_custom_ident(counter_name)
            definition.is_reversed = false
        elif allow_reversed_ == CssParserAllowReversed.yes and css_syntax.component_value_is_function_string_view(&token, ak.sv("reversed")):
            let function = css_syntax.component_value_function(&token)
            var function_tokens = css_syntax.token_stream_create(function.value)
            css_syntax.token_stream_discard_a_token(tokens)
            css_syntax.token_stream_discard_whitespace(&function_tokens)
            let name_token = css_syntax.token_stream_consume_a_token(&function_tokens)
            if not css_syntax.component_value_is(&name_token, .ident):
                break
            css_syntax.token_stream_discard_whitespace(&function_tokens)
            if css_syntax.token_stream_has_next_token(&function_tokens):
                break

            definition.name = css_component_value_ident(&name_token)
            definition.is_reversed = true
        else:
            break
        css_syntax.token_stream_discard_whitespace(tokens)

        # <integer>?
        definition.value = css_parser_parse_integer_value(this, tokens, css_infinite_integer_range)
        if definition.value == none and not definition.is_reversed:
            definition.value = css_integer_style_value_create(default_value_if_not_reversed).css_style_value()

        ak.vector_append_t(&counter_definitions, definition)
        css_syntax.token_stream_discard_whitespace(tokens)
        css_syntax.state_transaction_commit(&per_item_transaction)

    if ak.vector_is_empty(&counter_definitions):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_counter_definitions_style_value_create(counter_definitions).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:906 Web::CSS::Parser::Parser::parse_counter_increment_value.
# https://drafts.csswg.org/css-lists-3/#propdef-counter-increment
pub func css_parser_parse_counter_increment_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ <counter-name> <integer>? ]+ | none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    return css_parser_parse_counter_definitions_value(this, tokens, CssParserAllowReversed.no, 1)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:916 Web::CSS::Parser::Parser::parse_counter_reset_value.
# https://drafts.csswg.org/css-lists-3/#propdef-counter-reset
pub func css_parser_parse_counter_reset_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ <counter-name> <integer>? | <reversed-counter-name> <integer>? ]+ | none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    return css_parser_parse_counter_definitions_value(this, tokens, CssParserAllowReversed.yes, 0)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:926 Web::CSS::Parser::Parser::parse_counter_set_value.
# https://drafts.csswg.org/css-lists-3/#propdef-counter-set
pub func css_parser_parse_counter_set_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ <counter-name> <integer>? ]+ | none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    return css_parser_parse_counter_definitions_value(this, tokens, CssParserAllowReversed.no, 0)

# mark: cursor ==================================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:936 Web::CSS::Parser::Parser::parse_cursor_value.
# https://drafts.csswg.org/css-ui-4/#cursor
pub func css_parser_parse_cursor_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [<cursor-image>,]* <cursor-predefined>
    # <cursor-image> = <url> <number>{2}?
    # So, any number of custom cursor definitions, and then a mandatory cursor name keyword, all comma-separated.

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    var cursors = ak.Vector[CssStyleValue*]()

    let parts = css_parser_parse_a_comma_separated_list_of_component_values(this, tokens)
    for i in 0..<ak.vector_size(&parts):
        let part = *ak.vector_at_const(&parts, i)
        var part_tokens = css_syntax.token_stream_create(part)

        if i == ak.vector_size(&parts) - 1:
            # Cursor keyword
            css_syntax.token_stream_discard_whitespace(&part_tokens)
            let keyword_value = css_parser_parse_keyword_value(this, &part_tokens) else return none
            if css_data.keyword_to_cursor_predefined(css_style_value_to_keyword(keyword_value)) == none:
                return none

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

            ak.vector_append_t(&cursors, keyword_value)
        else:
            # Custom cursor definition
            # <cursor-image> = <url> <number>{2}?
            # "Conforming user agents may, instead of <url>, support <image> which is a superset."

            css_syntax.token_stream_discard_whitespace(&part_tokens)
            let image_value = css_parser_parse_image_value(this, &part_tokens) else return none

            css_syntax.token_stream_discard_whitespace(&part_tokens)

            if css_syntax.token_stream_has_next_token(&part_tokens):
                # <number>{2}, which are the x and y coordinates of the hotspot
                let x = css_parser_parse_number_value(this, &part_tokens, css_infinite_range)
                css_syntax.token_stream_discard_whitespace(&part_tokens)
                let y = css_parser_parse_number_value(this, &part_tokens, css_infinite_range)
                css_syntax.token_stream_discard_whitespace(&part_tokens)
                if x == none or y == none or css_syntax.token_stream_has_next_token(&part_tokens):
                    return none

                ak.vector_append_t(&cursors, css_cursor_style_value_create(image_value, x, y).css_style_value())
                continue

            ak.vector_append_t(&cursors, css_cursor_style_value_create(image_value, none, none).css_style_value())

    if ak.vector_is_empty(&cursors):
        return none

    css_syntax.state_transaction_commit(&transaction)
    if ak.vector_size(&cursors) == 1:
        return *ak.vector_first(&cursors)

    return css_style_value_list_of(cursors, CssStyleValueListSeparator.comma)

# mark: Anchors =================================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1003 Web::CSS::Parser::Parser::parse_anchor_name_value.
# https://drafts.csswg.org/css-anchor-position/#name
pub func css_parser_parse_anchor_name_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | <dashed-ident>#
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    return css_parser_parse_comma_separated_values(this, tokens, css_parser_parse_dashed_ident_callback)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1015 Web::CSS::Parser::Parser::parse_anchor_scope_value.
# https://drafts.csswg.org/css-anchor-position/#anchor-scope
pub func css_parser_parse_anchor_scope_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | all | <dashed-ident>#
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

    if let all = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.all):
        return all

    return css_parser_parse_comma_separated_values(this, tokens, css_parser_parse_dashed_ident_callback)

# mark: aspect-ratio ============================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1030 Web::CSS::Parser::Parser::parse_aspect_ratio_value.
# https://www.w3.org/TR/css-sizing-4/#aspect-ratio
pub func css_parser_parse_aspect_ratio_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # `auto || <ratio>`
    var auto_value: CssStyleValue*? = none
    var ratio_value: CssStyleValue*? = none

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    while css_syntax.token_stream_has_next_token(tokens):
        let maybe_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.aspect_ratio, tokens) else return none

        if css_style_value_is_ratio(maybe_value):
            if ratio_value != none:
                return none
            ratio_value = maybe_value
            continue

        if css_style_value_is_keyword(maybe_value) and css_keyword_style_value_keyword(css_style_value_as_keyword(maybe_value)) == css_data.Keyword.auto:
            if auto_value != none:
                return none
            auto_value = maybe_value
            continue

        return none

    if let auto_ = auto_value:
        if let ratio = ratio_value:
            css_syntax.state_transaction_commit(&transaction)
            return css_style_value_list_of(css_style_values_of([auto_, ratio]), CssStyleValueListSeparator.space)

    if let ratio = ratio_value:
        css_syntax.state_transaction_commit(&transaction)
        return ratio

    if let auto_ = auto_value:
        css_syntax.state_transaction_commit(&transaction)
        return auto_

    return none

# mark: animation ===============================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1080 Web::CSS::Parser::Parser::parse_animation_value.
# https://drafts.csswg.org/css-animations-1/#animation
pub func css_parser_parse_animation_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [<'animation-duration'> || <easing-function> || <'animation-delay'> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state> || [ none | <keyframes-name> ] || <single-animation-timeline>]#
    # NB: While it isn't in the spec the CSSWG resolved to include `animation-timeline` as a reset-only sub-property
    #     of the `animation` shorthand so we shouldn't actually allow <single-animation-timeline>.
    #     https://github.com/w3c/csswg-drafts/issues/6946#issuecomment-1233190360

    let longhand_ids = css_property_ids_of([
        css_data.PropertyId.animation_duration,
        css_data.PropertyId.animation_timing_function,
        css_data.PropertyId.animation_delay,
        css_data.PropertyId.animation_iteration_count,
        css_data.PropertyId.animation_direction,
        css_data.PropertyId.animation_fill_mode,
        css_data.PropertyId.animation_play_state,
        css_data.PropertyId.animation_name,
    ])

    # FIXME: The animation-trigger properties are reset-only sub-properties of the animation shorthand.
    return css_parser_parse_coordinating_value_list_shorthand(this, tokens, css_data.PropertyId.animation, longhand_ids, css_property_ids_of([css_data.PropertyId.animation_timeline]))

# mark: background-position =====================================================================

## `[this](auto& tokens) { return parse_position_value(tokens, PositionParsingMode::BackgroundPosition); }` (the context is
## the parser).
func css_parser_parse_background_position_callback(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let this = (CssParser*)(context else trap("no parser"))
    return (CssStyleValue*?)css_parser_parse_position_value(this, tokens, CssParserPositionParsingMode.background_position)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:1102 Web::CSS::Parser::Parser::parse_background_position_value.
pub func css_parser_parse_background_position_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let background_position_value = css_parser_parse_comma_separated_value_list(this, tokens, ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssStyleValue*?](
        call = css_parser_parse_background_position_callback, context = this)) else return none

    var background_position_x_values = ak.Vector[CssStyleValue*]()
    var background_position_y_values = ak.Vector[CssStyleValue*]()

    let positions = css_style_value_list_values(background_position_value)
    for background_position in ak.vector_span_const(&positions):
        ak.vector_append_t(&background_position_x_values, css_position_style_value_edge_x(css_style_value_as_position(background_position)).css_style_value())
        ak.vector_append_t(&background_position_y_values, css_position_style_value_edge_y(css_style_value_as_position(background_position)).css_style_value())

    return css_shorthand_of(css_data.PropertyId.background_position,
                            css_property_ids_of([css_data.PropertyId.background_position_x, css_data.PropertyId.background_position_y]),
                            css_style_values_of([css_style_value_list_of(background_position_x_values, CssStyleValueListSeparator.comma),
                                                 css_style_value_list_of(background_position_y_values, CssStyleValueListSeparator.comma)]))