Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / parser / property_parsing_13.lucb

#==============================================================================================
#
#   property_parsing_13 - parsing property values: transition, translate, scale,
#                         scroll-timeline, scrollbar-color, scrollbar-gutter
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 4430-4727). parse_scroll_timeline_value's
#       function-local static (the initial scroll-timeline-axis) is a module variable set on first
#       use; it points into property_initial_value's cache, which lives for the whole program.
#
#==============================================================================================

# mark: transition ==============================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4431 Web::CSS::Parser::Parser::parse_transition_value.
# https://drafts.csswg.org/css-transitions-2/#transition-shorthand-property
pub func css_parser_parse_transition_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ [ none | <single-transition-property> ] || <time> || <easing-function> || <time> || <transition-behavior-value> ]#
    let longhand_ids = css_property_ids_of([
        css_data.PropertyId.transition_property,
        css_data.PropertyId.transition_duration,
        css_data.PropertyId.transition_timing_function,
        css_data.PropertyId.transition_delay,
        css_data.PropertyId.transition_behavior,
    ])

    let parsed_value = css_parser_parse_coordinating_value_list_shorthand(this, tokens, css_data.PropertyId.transition, longhand_ids) else return none

    # https://drafts.csswg.org/css-transitions-1/#transition-shorthand-property
    # If there is more than one <single-transition> in the shorthand, and any of the transitions has none as the
    # <single-transition-property>, then the declaration is invalid.
    let transition_property = css_shorthand_style_value_longhand(css_style_value_as_shorthand(parsed_value), css_data.PropertyId.transition_property) else trap("VERIFY: a transition-property")
    let transition_properties = css_style_value_list_values(css_style_value_as_value_list(transition_property))

    if ak.vector_size(&transition_properties) > 1:
        for property in ak.vector_span_const(&transition_properties):
            if css_style_value_to_keyword(property) == css_data.Keyword.none_:
                return none

    return parsed_value

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4458 Web::CSS::Parser::Parser::parse_transition_property_value.
pub func css_parser_parse_transition_property_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # https://drafts.csswg.org/css-transitions/#transition-property-property
    # none | <single-transition-property>#

    # none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return css_style_value_list_of(css_style_values_of([none_]), CssStyleValueListSeparator.comma)

    # <single-transition-property>#
    # <single-transition-property> = all | <custom-ident>
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    let transition_property_values = css_parser_parse_a_comma_separated_list_of_component_values(this, tokens)

    var transition_properties = ak.Vector[CssStyleValue*]()
    for value in ak.vector_span_const(&transition_property_values):
        var transition_property_tokens = css_syntax.token_stream_create(value)
        let blacklist: ak.StringView[1] = [ak.sv("none")]
        let custom_ident = css_parser_parse_custom_ident_value(this, &transition_property_tokens, blacklist)
        if custom_ident == none or css_syntax.token_stream_has_next_token(&transition_property_tokens):
            return none

        ak.vector_append_t(&transition_properties, (custom_ident else trap("release_nonnull")).css_style_value())
    css_syntax.state_transaction_commit(&transaction)
    return css_style_value_list_of(transition_properties, CssStyleValueListSeparator.comma)

# mark: translate and scale =====================================================================

## `TransformationStyleValue::create(property, function, values)` as a StyleValue.
func css_transformation_of(property: css_data.PropertyId, function: css_data.TransformFunction, values: const (CssStyleValue*)[]) -> CssStyleValue*:
    return css_transformation_style_value_create(property, function, css_style_values_of(values)).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4486 Web::CSS::Parser::Parser::parse_translate_value.
# https://drafts.csswg.org/css-transforms-2/#propdef-translate
pub func css_parser_parse_translate_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | <length-percentage> [ <length-percentage> <length>? ]?

    # none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

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

    # <length-percentage> [ <length-percentage> <length>? ]?
    let maybe_x = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        css_syntax.state_transaction_commit(&transaction)
        return css_transformation_of(css_data.PropertyId.translate, css_data.TransformFunction.translate, [maybe_x, css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value()])

    let maybe_y = css_parser_parse_length_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        css_syntax.state_transaction_commit(&transaction)
        return css_transformation_of(css_data.PropertyId.translate, css_data.TransformFunction.translate, [maybe_x, maybe_y])

    let maybe_z = css_parser_parse_length_value(this, tokens, css_infinite_range) else return none

    css_syntax.state_transaction_commit(&transaction)
    return css_transformation_of(css_data.PropertyId.translate, css_data.TransformFunction.translate3_d, [maybe_x, maybe_y, maybe_z])

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4526 Web::CSS::Parser::Parser::parse_scale_value.
# https://drafts.csswg.org/css-transforms-2/#propdef-scale
pub func css_parser_parse_scale_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | [ <number> | <percentage> ]{1,3}

    # none
    if let none_ = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.none_):
        return none_

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

    # [ <number> | <percentage> ]{1,3}
    let maybe_x = css_parser_parse_number_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        css_syntax.state_transaction_commit(&transaction)
        return css_transformation_of(css_data.PropertyId.scale, css_data.TransformFunction.scale, [maybe_x, maybe_x])

    let maybe_y = css_parser_parse_number_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        css_syntax.state_transaction_commit(&transaction)
        return css_transformation_of(css_data.PropertyId.scale, css_data.TransformFunction.scale, [maybe_x, maybe_y])

    let maybe_z = css_parser_parse_number_percentage_value(this, tokens, css_infinite_range, css_infinite_range) else return none

    css_syntax.state_transaction_commit(&transaction)
    return css_transformation_of(css_data.PropertyId.scale, css_data.TransformFunction.scale3_d, [maybe_x, maybe_y, maybe_z])

# mark: scroll-timeline =========================================================================

# The `static auto default_axis` of parse_scroll_timeline_value.
var css_parse_scroll_timeline_value_default_axis: CssStyleValue*?

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4566 Web::CSS::Parser::Parser::parse_scroll_timeline_value.
# https://drafts.csswg.org/scroll-animations-1/#scroll-timeline-shorthand
pub func css_parser_parse_scroll_timeline_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [ <'scroll-timeline-name'> <'scroll-timeline-axis'>? ]#
    var names = ak.Vector[CssStyleValue*]()
    var axes = ak.Vector[CssStyleValue*]()

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

    while true:
        if css_parse_scroll_timeline_value_default_axis == none:
            css_parse_scroll_timeline_value_default_axis = css_first_initial_value(css_data.PropertyId.scroll_timeline_axis)
        let default_axis = css_parse_scroll_timeline_value_default_axis else trap("unreachable")

        css_syntax.token_stream_discard_whitespace(tokens)

        let maybe_name = css_parser_parse_css_value_for_property(this, css_data.PropertyId.scroll_timeline_name, tokens) else return none

        ak.vector_append_t(&names, maybe_name)

        css_syntax.token_stream_discard_whitespace(tokens)

        if css_next_token_is(tokens, .comma):
            ak.vector_append_t(&axes, default_axis)
            css_syntax.token_stream_discard_a_token(tokens)

            # Disallow trailing commas
            if not css_syntax.token_stream_has_next_token(tokens):
                return none

            continue

        if not css_syntax.token_stream_has_next_token(tokens):
            ak.vector_append_t(&axes, default_axis)
            break

        let maybe_axis = css_parser_parse_css_value_for_property(this, css_data.PropertyId.scroll_timeline_axis, tokens) else return none

        ak.vector_append_t(&axes, maybe_axis)

        css_syntax.token_stream_discard_whitespace(tokens)

        if css_next_token_is(tokens, .comma):
            css_syntax.token_stream_discard_a_token(tokens)

            # Disallow trailing commas
            if not css_syntax.token_stream_has_next_token(tokens):
                return none

            continue

        if css_syntax.token_stream_has_next_token(tokens):
            return none
        break

    css_syntax.state_transaction_commit(&transaction)

    return css_shorthand_of(css_data.PropertyId.scroll_timeline,
                            css_property_ids_of([css_data.PropertyId.scroll_timeline_name, css_data.PropertyId.scroll_timeline_axis]),
                            css_style_values_of([css_style_value_list_of(names, CssStyleValueListSeparator.comma), css_style_value_list_of(axes, CssStyleValueListSeparator.comma)]))

# mark: scrollbar-color and scrollbar-gutter ====================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4635 Web::CSS::Parser::Parser::parse_scrollbar_color_value.
# https://drafts.csswg.org/css-scrollbars/#propdef-scrollbar-color
pub func css_parser_parse_scrollbar_color_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # auto | <color>{2}
    if not css_syntax.token_stream_has_next_token(tokens):
        return none
    if let auto_keyword = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.auto):
        return auto_keyword

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

    let thumb_color = css_parser_parse_color_value(this, tokens) else return none

    css_syntax.token_stream_discard_whitespace(tokens)

    let track_color = css_parser_parse_color_value(this, tokens) else return none
    css_syntax.token_stream_discard_whitespace(tokens)
    css_syntax.state_transaction_commit(&transaction)

    return css_scrollbar_color_style_value_create(thumb_color, track_color).css_style_value()

## The `parse_stable` lambda of parse_scrollbar_gutter_value.
func css_parse_scrollbar_gutter_stable(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> bool?:
    var stable_transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&stable_transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    let token = css_syntax.token_stream_consume_a_token(tokens)
    if not css_syntax.component_value_is(&token, .ident):
        return none
    let ident = css_component_value_ident(&token)
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("auto")):
        css_syntax.state_transaction_commit(&stable_transaction)
        return false
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("stable")):
        css_syntax.state_transaction_commit(&stable_transaction)
        return true
    return none

## The `parse_both_edges` lambda of parse_scrollbar_gutter_value.
func css_parse_scrollbar_gutter_both_edges(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> bool?:
    var edges_transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&edges_transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    let token = css_syntax.token_stream_consume_a_token(tokens)
    if not css_syntax.component_value_is(&token, .ident):
        return none
    let ident = css_component_value_ident(&token)
    if ak.fly_string_equals_ignoring_ascii_case_string_view(&ident, ak.sv("both-edges")):
        css_syntax.state_transaction_commit(&edges_transaction)
        return true
    return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4661 Web::CSS::Parser::Parser::parse_scrollbar_gutter_value.
# https://drafts.csswg.org/css-overflow/#propdef-scrollbar-gutter
pub func css_parser_parse_scrollbar_gutter_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    _ = this
    # auto | stable && both-edges?
    css_syntax.token_stream_discard_whitespace(tokens)
    if not css_syntax.token_stream_has_next_token(tokens):
        return none

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

    # NB: The `parse_stable` and `parse_both_edges` lambdas are css_parse_scrollbar_gutter_stable and
    #     css_parse_scrollbar_gutter_both_edges.

    var stable: bool? = none
    var both_edges: bool? = none
    stable = css_parse_scrollbar_gutter_stable(tokens)
    if let stable_value = stable:
        if stable_value:
            both_edges = css_parse_scrollbar_gutter_both_edges(tokens)
    else:
        both_edges = css_parse_scrollbar_gutter_both_edges(tokens)
        if both_edges != none:
            stable = css_parse_scrollbar_gutter_stable(tokens)
            if stable == none or not (stable else trap("unreachable")):
                return none

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

    css_syntax.state_transaction_commit(&transaction)

    var gutter_value: css_data.ScrollbarGutter
    if both_edges != none:
        gutter_value = css_data.ScrollbarGutter.both_edges
    elif stable != none and (stable else trap("unreachable")):
        gutter_value = css_data.ScrollbarGutter.stable
    else:
        gutter_value = css_data.ScrollbarGutter.auto
    return css_scrollbar_gutter_style_value_create(gutter_value).css_style_value()