Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   property_parsing_17 - parsing property values: white-space-trim, white-space,
#                         view-timeline, will-change
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 5648-5922). The function-local
#       statics of parse_view_timeline_value's `append_entry` lambda are module variables set on
#       first use; they point into property_initial_value's cache, which lives for the whole
#       program.
#
#==============================================================================================

# mark: white-space-trim ========================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5649 Web::CSS::Parser::Parser::parse_white_space_trim_value.
# https://www.w3.org/TR/css-text-4/#white-space-trim
pub func css_parser_parse_white_space_trim_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | discard-before || discard-after || discard-inner

    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)

    var discard_before: CssStyleValue*? = none
    var discard_after: CssStyleValue*? = none
    var discard_inner: CssStyleValue*? = none

    while let parsed_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.white_space_trim, tokens):
        match css_keyword_style_value_keyword(css_style_value_as_keyword(parsed_value)):
            .discard_before:
                if discard_before != none:
                    return none
                discard_before = parsed_value
            .discard_after:
                if discard_after != none:
                    return none
                discard_after = parsed_value
            .discard_inner:
                if discard_inner != none:
                    return none
                discard_inner = parsed_value
            _:
                return none

        if not css_syntax.token_stream_has_next_token(tokens):
            break

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

    # NOTE: The values are appended here rather than in the loop above to canonicalize their order.
    if let value = discard_before:
        ak.vector_append_t(&parsed_values, value)
    if let value = discard_after:
        ak.vector_append_t(&parsed_values, value)
    if let value = discard_inner:
        ak.vector_append_t(&parsed_values, value)

    css_syntax.state_transaction_commit(&transaction)

    return css_style_value_list_of(parsed_values, CssStyleValueListSeparator.space)

# mark: white-space =============================================================================

## The `make_whitespace_shorthand` lambda of parse_white_space_shorthand (it commits the function's transaction).
func css_make_whitespace_shorthand(transaction: css_syntax.StateTransaction*, white_space_collapse: CssStyleValue*?, text_wrap_mode: CssStyleValue*?,
                                   white_space_trim: CssStyleValue*?) -> CssStyleValue*:
    css_syntax.state_transaction_commit(transaction)

    let collapse = white_space_collapse else css_property_initial_value(css_data.PropertyId.white_space_collapse)

    let wrap_mode = text_wrap_mode else css_property_initial_value(css_data.PropertyId.text_wrap_mode)

    let trim = white_space_trim else css_property_initial_value(css_data.PropertyId.white_space_trim)

    return css_shorthand_of(css_data.PropertyId.white_space,
                            css_property_ids_of([css_data.PropertyId.white_space_collapse, css_data.PropertyId.text_wrap_mode, css_data.PropertyId.white_space_trim]),
                            css_style_values_of([collapse, wrap_mode, trim]))

## The white-space-trim keywords at the front of `tokens`, consumed: the inner `while (true)` of
## parse_white_space_shorthand.
func css_consume_white_space_trim_component_values(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> ak.Vector[css_syntax.ComponentValue]:
    var white_space_trim_component_values = ak.Vector[css_syntax.ComponentValue]()

    while true:
        let peek_token = css_syntax.token_stream_next_token(tokens)

        if not css_syntax.component_value_is(&peek_token, .ident):
            break

        let ident = css_component_value_ident(&peek_token)
        let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&ident))

        if keyword == none or not css_data.property_accepts_keyword(css_data.PropertyId.white_space_trim, keyword else trap("unreachable")):
            break

        ak.vector_append_t(&white_space_trim_component_values, css_syntax.token_stream_consume_a_token(tokens))
    return white_space_trim_component_values

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5703 Web::CSS::Parser::Parser::parse_white_space_shorthand.
# https://www.w3.org/TR/css-text-4/#white-space-property
pub func css_parser_parse_white_space_shorthand(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # normal | pre | pre-wrap | pre-line | <'white-space-collapse'> || <'text-wrap-mode'> || <'white-space-trim'>

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

    # NB: The `make_whitespace_shorthand` lambda is css_make_whitespace_shorthand.

    # normal | pre | pre-wrap | pre-line
    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal) != none:
        return css_make_whitespace_shorthand(&transaction, css_keyword_value_of(css_data.Keyword.collapse), css_keyword_value_of(css_data.Keyword.wrap), css_keyword_value_of(css_data.Keyword.none_))

    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.pre) != none:
        return css_make_whitespace_shorthand(&transaction, css_keyword_value_of(css_data.Keyword.preserve), css_keyword_value_of(css_data.Keyword.nowrap), css_keyword_value_of(css_data.Keyword.none_))

    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.pre_wrap) != none:
        return css_make_whitespace_shorthand(&transaction, css_keyword_value_of(css_data.Keyword.preserve), css_keyword_value_of(css_data.Keyword.wrap), css_keyword_value_of(css_data.Keyword.none_))

    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.pre_line) != none:
        return css_make_whitespace_shorthand(&transaction, css_keyword_value_of(css_data.Keyword.preserve_breaks), css_keyword_value_of(css_data.Keyword.wrap), css_keyword_value_of(css_data.Keyword.none_))

    # <'white-space-collapse'> || <'text-wrap-mode'> || <'white-space-trim'>
    var white_space_collapse: CssStyleValue*? = none
    var text_wrap_mode: CssStyleValue*? = none
    var white_space_trim: CssStyleValue*? = none

    while css_syntax.token_stream_has_next_token(tokens):
        if let value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.white_space_collapse, tokens):
            if white_space_collapse != none:
                return none
            white_space_collapse = value
            continue

        if let value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.text_wrap_mode, tokens):
            if text_wrap_mode != none:
                return none
            text_wrap_mode = value
            continue

        let white_space_trim_component_values = css_consume_white_space_trim_component_values(tokens)

        if not ak.vector_is_empty(&white_space_trim_component_values):
            var white_space_trim_token_stream = css_syntax.token_stream_create(white_space_trim_component_values)

            if let value = css_parser_parse_white_space_trim_value(this, &white_space_trim_token_stream):
                if white_space_trim != none:
                    return none
                white_space_trim = value
                continue

        return none

    return css_make_whitespace_shorthand(&transaction, white_space_collapse, text_wrap_mode, white_space_trim)

# mark: view-timeline ===========================================================================

# The `static auto default_axis` and `default_inset` of parse_view_timeline_value's `append_entry` lambda.
var css_parse_view_timeline_value_default_axis: CssStyleValue*?
var css_parse_view_timeline_value_default_inset: CssStyleValue*?

## The `append_entry` lambda of parse_view_timeline_value.
func css_view_timeline_append_entry(names: ak.Vector[CssStyleValue*]*, axes: ak.Vector[CssStyleValue*]*, insets: ak.Vector[CssStyleValue*]*, name: CssStyleValue*?, axis: CssStyleValue*?,
                                    inset: CssStyleValue*?):
    ak.vector_append_t(names, name else trap("VERIFY: a view-timeline-name"))

    if css_parse_view_timeline_value_default_axis == none:
        css_parse_view_timeline_value_default_axis = css_first_initial_value(css_data.PropertyId.view_timeline_axis)
    if css_parse_view_timeline_value_default_inset == none:
        css_parse_view_timeline_value_default_inset = css_first_initial_value(css_data.PropertyId.view_timeline_inset)

    ak.vector_append_t(axes, css_value_or_initial(axis, css_parse_view_timeline_value_default_axis))
    ak.vector_append_t(insets, css_value_or_initial(inset, css_parse_view_timeline_value_default_inset))

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

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

    while true:
        var name: CssStyleValue*? = none
        var axis: CssStyleValue*? = none
        var inset: CssStyleValue*? = none

        # NB: The `append_entry` lambda is css_view_timeline_append_entry.

        css_syntax.token_stream_discard_whitespace(tokens)

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

        name = maybe_name

        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

            css_view_timeline_append_entry(&names, &axes, &insets, name, axis, inset)
            continue

        var remaining_longhands = css_property_ids_of([css_data.PropertyId.view_timeline_axis, css_data.PropertyId.view_timeline_inset])

        while css_syntax.token_stream_has_next_token(tokens) and not css_next_token_is(tokens, .comma):
            css_syntax.token_stream_discard_whitespace(tokens)

            let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens) else return none

            css_remove_property(&remaining_longhands, property_and_value.property)

            match property_and_value.property:
                .view_timeline_axis:
                    if axis != none:
                        return none

                    axis = property_and_value.style_value
                .view_timeline_inset:
                    if inset != none:
                        return none

                    inset = property_and_value.style_value
                _:
                    trap("VERIFY_NOT_REACHED")

        css_view_timeline_append_entry(&names, &axes, &insets, name, axis, inset)

        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.view_timeline,
                            css_property_ids_of([css_data.PropertyId.view_timeline_name, css_data.PropertyId.view_timeline_axis, css_data.PropertyId.view_timeline_inset]),
                            css_style_values_of([css_style_value_list_of(names, CssStyleValueListSeparator.comma),
                                                 css_style_value_list_of(axes, CssStyleValueListSeparator.comma),
                                                 css_style_value_list_of(insets, CssStyleValueListSeparator.comma)]))

# mark: will-change =============================================================================

## The `parse_animateable_feature` lambda of parse_will_change_value (the context is the parser).
func css_parser_parse_animateable_feature(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let this = (CssParser*)(context else trap("no parser"))
    let style_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.will_change, tokens) else return none

    if css_style_value_to_keyword(style_value) == css_data.Keyword.auto:
        return none

    return style_value

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:5899 Web::CSS::Parser::Parser::parse_will_change_value.
# https://drafts.csswg.org/css-will-change/#will-change
pub func css_parser_parse_will_change_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # auto | <animateable-feature>#
    # <animateable-feature> = scroll-position | contents | <custom-ident>
    if css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.auto) != none:
        return css_keyword_value_of(css_data.Keyword.auto)

    # NB: The `parse_animateable_feature` lambda is css_parser_parse_animateable_feature.

    return css_parser_parse_comma_separated_values(this, tokens, css_parser_parse_animateable_feature)