Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   property_parsing_7 - parsing property values: display, flex, flex-flow
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 2170-2449): display (its single
#       and multi-component lambdas), the flex shorthand and the flex-flow shorthand.
#
#==============================================================================================

# mark: display =================================================================================

## `Display::from_short(short_)` as an Optional.
func css_display_short(short_: CssDisplayShort) -> CssDisplay?:
    return css_display_from_short(short_)

## The `parse_single_component_display` lambda of parse_display_value.
func css_parser_parse_single_component_display(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssDisplay?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    if let keyword_value = css_parser_parse_keyword_value(this, tokens):
        let keyword = css_style_value_to_keyword(keyword_value)
        if keyword == css_data.Keyword.list_item:
            css_syntax.state_transaction_commit(&transaction)
            return css_display_short(CssDisplayShort.list_item)

        if let display_outside = css_data.keyword_to_display_outside(keyword):
            css_syntax.state_transaction_commit(&transaction)
            match display_outside:
                .block:
                    return css_display_short(CssDisplayShort.block)
                .inline:
                    return css_display_short(CssDisplayShort.inline)
                .run_in:
                    return css_display_short(CssDisplayShort.run_in)
                _:
                    trap("VERIFY_NOT_REACHED")

        if let display_inside = css_data.keyword_to_display_inside(keyword):
            css_syntax.state_transaction_commit(&transaction)
            match display_inside:
                .flow:
                    return css_display_short(CssDisplayShort.flow)
                .flow_root:
                    return css_display_short(CssDisplayShort.flow_root)
                .table:
                    return css_display_short(CssDisplayShort.table)
                .flex:
                    return css_display_short(CssDisplayShort.flex)
                .grid:
                    return css_display_short(CssDisplayShort.grid)
                .ruby:
                    return css_display_short(CssDisplayShort.ruby)
                .math:
                    return css_display_short(CssDisplayShort.math)
                _:
                    trap("VERIFY_NOT_REACHED")

        if let display_internal = css_data.keyword_to_display_internal(keyword):
            css_syntax.state_transaction_commit(&transaction)
            return css_display_new_display_internal(display_internal)

        if let display_box = css_data.keyword_to_display_box(keyword):
            css_syntax.state_transaction_commit(&transaction)
            match display_box:
                .contents:
                    return css_display_short(CssDisplayShort.contents)
                .none_:
                    return css_display_short(CssDisplayShort.none_)
                _:
                    trap("VERIFY_NOT_REACHED")

        if let display_legacy = css_data.keyword_to_display_legacy(keyword):
            css_syntax.state_transaction_commit(&transaction)
            match display_legacy:
                .inline_block:
                    return css_display_short(CssDisplayShort.inline_block)
                .inline_table:
                    return css_display_short(CssDisplayShort.inline_table)
                .inline_flex:
                    return css_display_short(CssDisplayShort.inline_flex)
                .inline_grid:
                    return css_display_short(CssDisplayShort.inline_grid)
                _:
                    trap("VERIFY_NOT_REACHED")
    return none

## The `parse_multi_component_display` lambda of parse_display_value.
func css_parser_parse_multi_component_display(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssDisplay?:
    var list_item = CssDisplayListItem.no
    var inside: css_data.DisplayInside? = none
    var outside: css_data.DisplayOutside? = 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):
        css_syntax.token_stream_discard_whitespace(tokens)
        if not css_syntax.token_stream_has_next_token(tokens):
            break
        if let value = css_parser_parse_keyword_value(this, tokens):
            let keyword = css_style_value_to_keyword(value)
            if keyword == css_data.Keyword.list_item:
                if list_item == CssDisplayListItem.yes:
                    return none
                list_item = CssDisplayListItem.yes
                continue
            if let inside_value = css_data.keyword_to_display_inside(keyword):
                if inside != none:
                    return none
                inside = inside_value
                continue
            if let outside_value = css_data.keyword_to_display_outside(keyword):
                if outside != none:
                    return none
                outside = outside_value
                continue

        # Not a display value, abort.
        let next = css_syntax.token_stream_next_token(tokens)
        css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
            value_type = ak.fly_string_from_str("<display>"),
            value_string = css_syntax.component_value_to_string(&next),
            description = ak.string_from_str("Unrecognized value"),
        )))
        return none

    # The spec does not allow any other inside values to be combined with list-item
    # <display-outside>? && [ flow | flow-root ]? && list-item
    if list_item == CssDisplayListItem.yes and inside != none and inside != css_data.DisplayInside.flow and inside != css_data.DisplayInside.flow_root:
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_display_new_list_item(outside else css_data.DisplayOutside.block, inside else css_data.DisplayInside.flow, list_item)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2171 Web::CSS::Parser::Parser::parse_display_value.
# https://www.w3.org/TR/css-display-3/#the-display-properties
pub func css_parser_parse_display_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # NB: The `parse_single_component_display` and `parse_multi_component_display` lambdas are
    #     css_parser_parse_single_component_display and css_parser_parse_multi_component_display.

    # Count non-whitespace tokens to decide between single and multi-component parsing.
    # This is needed because var() substitution can leave trailing whitespace tokens.
    var non_whitespace_token_count: usize = 0
    for i in 0..<css_syntax.token_stream_remaining_token_count(tokens):
        let token = css_syntax.token_stream_peek_token(tokens, i)
        if not css_syntax.component_value_is(&token, .whitespace):
            non_whitespace_token_count += 1

    var display: CssDisplay? = none
    if non_whitespace_token_count == 1:
        display = css_parser_parse_single_component_display(this, tokens)
    else:
        display = css_parser_parse_multi_component_display(this, tokens)

    if let display_value = display:
        return css_display_style_value_create(display_value).css_style_value()

    return none

# mark: flex ====================================================================================

## The `make_flex_shorthand` lambda of parse_flex_shorthand_value (it commits the function's transaction).
func css_make_flex_shorthand(transaction: css_syntax.StateTransaction*, flex_grow: CssStyleValue*, flex_shrink: CssStyleValue*, flex_basis: CssStyleValue*) -> CssStyleValue*:
    css_syntax.state_transaction_commit(transaction)
    return css_shorthand_of(css_data.PropertyId.flex,
                            css_property_ids_of([css_data.PropertyId.flex_grow, css_data.PropertyId.flex_shrink, css_data.PropertyId.flex_basis]),
                            css_style_values_of([flex_grow, flex_shrink, flex_basis]))

## `PercentageStyleValue::create(Percentage(0))` as a StyleValue.
func css_zero_percentage_value() -> CssStyleValue*:
    return css_percentage_style_value_create(css_percentage_new(0.0)).css_style_value()

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2316 Web::CSS::Parser::Parser::parse_flex_shorthand_value.
pub func css_parser_parse_flex_shorthand_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)

    # NB: The `make_flex_shorthand` lambda is css_make_flex_shorthand.

    if css_syntax.token_stream_remaining_token_count(tokens) == 1:
        # One-value syntax: <flex-grow> | <flex-basis> | none
        let properties: css_data.PropertyId[3] = [css_data.PropertyId.flex_grow, css_data.PropertyId.flex_basis, css_data.PropertyId.flex]
        let property_and_value = css_parser_parse_css_value_for_properties(this, properties, tokens) else return none

        let value = property_and_value.style_value else trap("null style value")
        match property_and_value.property:
            .flex_grow:
                # NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
                # https://github.com/w3c/csswg-drafts/issues/5742
                let flex_basis = css_zero_percentage_value()
                let one = css_number_style_value_create(1.0).css_style_value()
                return css_make_flex_shorthand(&transaction, value, one, flex_basis)
            .flex_basis:
                let one = css_number_style_value_create(1.0).css_style_value()
                return css_make_flex_shorthand(&transaction, one, one, value)
            .flex:
                if css_style_value_is_keyword(value) and css_style_value_to_keyword(value) == css_data.Keyword.none_:
                    let zero = css_number_style_value_create(0.0).css_style_value()
                    return css_make_flex_shorthand(&transaction, zero, zero, css_keyword_value_of(css_data.Keyword.auto))
            _:
                trap("VERIFY_NOT_REACHED")

        return none

    var flex_grow: CssStyleValue*? = none
    var flex_shrink: CssStyleValue*? = none
    var flex_basis: CssStyleValue*? = none

    # NOTE: FlexGrow has to be before FlexBasis. `0` is a valid FlexBasis, but only
    #       if FlexGrow (along with optional FlexShrink) have already been specified.
    var remaining_longhands = css_property_ids_of([css_data.PropertyId.flex_grow, css_data.PropertyId.flex_basis])

    while css_syntax.token_stream_has_next_token(tokens):
        let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens) else return none
        let value = property_and_value.style_value else trap("release_nonnull")
        css_remove_property(&remaining_longhands, property_and_value.property)

        match property_and_value.property:
            .flex_grow:
                assert(flex_grow == none)
                flex_grow = value

                # Flex-shrink may optionally follow directly after.
                if let maybe_flex_shrink = css_parser_parse_css_value_for_property(this, css_data.PropertyId.flex_shrink, tokens):
                    flex_shrink = maybe_flex_shrink
                continue
            .flex_basis:
                assert(flex_basis == none)
                flex_basis = value
                continue
            _:
                trap("VERIFY_NOT_REACHED")

    if flex_grow == none:
        flex_grow = css_property_initial_value(css_data.PropertyId.flex_grow)
    if flex_shrink == none:
        flex_shrink = css_property_initial_value(css_data.PropertyId.flex_shrink)
    if flex_basis == none:
        # NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
        # https://github.com/w3c/csswg-drafts/issues/5742
        flex_basis = css_zero_percentage_value()

    return css_make_flex_shorthand(&transaction, flex_grow else trap("release_nonnull"), flex_shrink else trap("release_nonnull"), flex_basis else trap("release_nonnull"))

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:2410 Web::CSS::Parser::Parser::parse_flex_flow_value.
pub func css_parser_parse_flex_flow_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var flex_direction: CssStyleValue*? = none
    var flex_wrap: CssStyleValue*? = none

    var remaining_longhands = css_property_ids_of([css_data.PropertyId.flex_direction, css_data.PropertyId.flex_wrap])
    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 property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(&remaining_longhands), tokens) else return none
        let value = property_and_value.style_value else trap("release_nonnull")
        css_remove_property(&remaining_longhands, property_and_value.property)

        match property_and_value.property:
            .flex_direction:
                assert(flex_direction == none)
                flex_direction = value
                continue
            .flex_wrap:
                assert(flex_wrap == none)
                flex_wrap = value
                continue
            _:
                trap("VERIFY_NOT_REACHED")

    if flex_direction == none:
        flex_direction = css_property_initial_value(css_data.PropertyId.flex_direction)
    if flex_wrap == none:
        flex_wrap = css_property_initial_value(css_data.PropertyId.flex_wrap)

    css_syntax.state_transaction_commit(&transaction)
    return css_shorthand_of(css_data.PropertyId.flex_flow,
                            css_property_ids_of([css_data.PropertyId.flex_direction, css_data.PropertyId.flex_wrap]),
                            css_style_values_of([flex_direction else trap("release_nonnull"), flex_wrap else trap("release_nonnull")]))