Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   property_parsing_11 - parsing property values: position-area, position-visibility,
#                         quotes, a repeat style, text-decoration
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 3622-4088).
#
#==============================================================================================

# mark: position-area ===========================================================================

## The `is_x_keyword` lambda of parse_position_area.
func css_position_area_is_x_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .left, .center, .right, .span_left, .span_right, .x_start, .x_end, .span_x_start, .span_x_end, .self_x_start, .self_x_end, .span_self_x_start, .span_self_x_end, .span_all:
            return true
        _:
            return false

## The `is_y_keyword` lambda of parse_position_area.
func css_position_area_is_y_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .top, .center, .bottom, .span_top, .span_bottom, .y_start, .y_end, .span_y_start, .span_y_end, .self_y_start, .self_y_end, .span_self_y_start, .span_self_y_end, .span_all:
            return true
        _:
            return false

## The `is_block_keyword` lambda of parse_position_area.
func css_position_area_is_block_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .block_start, .center, .block_end, .span_block_start, .span_block_end, .span_all:
            return true
        _:
            return false

## The `is_inline_keyword` lambda of parse_position_area.
func css_position_area_is_inline_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .inline_start, .center, .inline_end, .span_inline_start, .span_inline_end, .span_all:
            return true
        _:
            return false

## The `is_self_block_keyword` lambda of parse_position_area.
func css_position_area_is_self_block_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .self_block_start, .center, .self_block_end, .span_self_block_start, .span_self_block_end, .span_all:
            return true
        _:
            return false

## The `is_self_inline_keyword` lambda of parse_position_area.
func css_position_area_is_self_inline_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .self_inline_start, .center, .self_inline_end, .span_self_inline_start, .span_self_inline_end, .span_all:
            return true
        _:
            return false

## The `is_start_end_keyword` lambda of parse_position_area.
func css_position_area_is_start_end_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .start, .center, .end, .span_start, .span_end, .span_all:
            return true
        _:
            return false

## The `is_self_start_end_keyword` lambda of parse_position_area.
func css_position_area_is_self_start_end_keyword(keyword: css_data.Keyword) -> bool:
    match keyword:
        .self_start, .center, .self_end, .span_self_start, .span_self_end, .span_all:
            return true
        _:
            return false

## The `is_axis_ambiguous` lambda of parse_position_area.
func css_position_area_is_axis_ambiguous(keyword: css_data.Keyword) -> bool:
    match keyword:
        .center, .span_all, .start, .end, .self_start, .self_end, .span_start, .span_end, .span_self_start, .span_self_end:
            return true
        _:
            return false

## The `create_keyword_list` lambda of parse_position_area (it commits the function's transaction).
func css_position_area_create_keyword_list(transaction: css_syntax.StateTransaction*, first: CssStyleValue*, second: CssStyleValue*) -> CssStyleValue*:
    css_syntax.state_transaction_commit(transaction)

    # If only a single keyword is given, it behaves as if the second keyword is span-all if the given keyword is
    # unambiguous about its axis.
    # These conditions ensure the span-all keyword is omitted when it would be implied.
    let first_keyword = css_keyword_style_value_keyword(css_style_value_as_keyword(first))
    let second_keyword = css_keyword_style_value_keyword(css_style_value_as_keyword(second))
    if not css_position_area_is_axis_ambiguous(first_keyword) and second_keyword == css_data.Keyword.span_all:
        return first
    if not css_position_area_is_axis_ambiguous(second_keyword) and first_keyword == css_data.Keyword.span_all:
        return second

    return css_style_value_list_of(css_style_values_of([first, second]), CssStyleValueListSeparator.space)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3622 Web::CSS::Parser::Parser::parse_position_area.
pub func css_parser_parse_position_area(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <position-area> = [
    #    [ left | center | right | span-left | span-right
    #    | x-start | x-end | span-x-start | span-x-end
    #    | x-self-start | x-self-end | span-x-self-start | span-x-self-end
    #    | span-all ]
    #    ||
    #    [ top | center | bottom | span-top | span-bottom
    #    | y-start | y-end | span-y-start | span-y-end
    #    | y-self-start | y-self-end | span-y-self-start | span-y-self-end
    #    | span-all ]
    #  |
    #    [ block-start | center | block-end | span-block-start | span-block-end | span-all ]
    #    ||
    #    [ inline-start | center | inline-end | span-inline-start | span-inline-end
    #    | span-all ]
    #  |
    #    [ self-block-start | center | self-block-end | span-self-block-start
    #    | span-self-block-end | span-all ]
    #    ||
    #    [ self-inline-start | center | self-inline-end | span-self-inline-start
    #    | span-self-inline-end | span-all ]
    #  |
    #    [ start | center | end | span-start | span-end | span-all ]{1,2}
    #    |
    #      [ self-start | center | self-end | span-self-start | span-self-end | span-all ]{1,2}
    #    ]

    # NB: The `is_*_keyword` and `is_axis_ambiguous` lambdas are css_position_area_is_*, and `create_keyword_list` is
    #     css_position_area_create_keyword_list.

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

    let first_keyword_value = css_parser_parse_keyword_value(this, tokens) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    let maybe_second_keyword_value = css_parser_parse_keyword_value(this, tokens)
    if maybe_second_keyword_value == none:
        let first_position_area_keyword = css_data.keyword_to_position_area(css_keyword_style_value_keyword(css_style_value_as_keyword(first_keyword_value)))
        if first_position_area_keyword == none:
            return none
        css_syntax.state_transaction_commit(&transaction)
        return first_keyword_value
    let second_keyword_value = maybe_second_keyword_value else trap("unreachable")

    let first_keyword = css_keyword_style_value_keyword(css_style_value_as_keyword(first_keyword_value))
    let second_keyword = css_keyword_style_value_keyword(css_style_value_as_keyword(second_keyword_value))

    if css_position_area_is_x_keyword(first_keyword) and css_position_area_is_y_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, first_keyword_value, second_keyword_value)
    if css_position_area_is_y_keyword(first_keyword) and css_position_area_is_x_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, second_keyword_value, first_keyword_value)

    if css_position_area_is_block_keyword(first_keyword) and css_position_area_is_inline_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, first_keyword_value, second_keyword_value)
    if css_position_area_is_inline_keyword(first_keyword) and css_position_area_is_block_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, second_keyword_value, first_keyword_value)

    if css_position_area_is_self_block_keyword(first_keyword) and css_position_area_is_self_inline_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, first_keyword_value, second_keyword_value)
    if css_position_area_is_self_inline_keyword(first_keyword) and css_position_area_is_self_block_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, second_keyword_value, first_keyword_value)

    if css_position_area_is_start_end_keyword(first_keyword) and css_position_area_is_start_end_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, first_keyword_value, second_keyword_value)
    if css_position_area_is_self_start_end_keyword(first_keyword) and css_position_area_is_self_start_end_keyword(second_keyword):
        return css_position_area_create_keyword_list(&transaction, first_keyword_value, second_keyword_value)

    return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3861 Web::CSS::Parser::Parser::parse_position_area_value.
# https://drafts.csswg.org/css-anchor-position/#position-area
pub func css_parser_parse_position_area_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | <position-area>

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

    # <position-area>
    return css_parser_parse_position_area(this, tokens)

# mark: position-visibility =====================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3874 Web::CSS::Parser::Parser::parse_position_visibility_value.
# https://drafts.csswg.org/css-anchor-position/#position-visibility
pub func css_parser_parse_position_visibility_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # always | [ anchors-valid || anchors-visible || no-overflow ]
    if let always = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.always):
        return always

    var anchors_valid_value: CssStyleValue*? = none
    var anchors_visible_value: CssStyleValue*? = none
    var no_overflow_value: CssStyleValue*? = none
    var values = ak.Vector[CssStyleValue*]()
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    while css_syntax.token_stream_has_next_token(tokens):
        let keyword_value = css_parser_parse_keyword_value(this, tokens) else return none

        match css_style_value_to_keyword(keyword_value):
            .anchors_valid:
                if anchors_valid_value != none:
                    return none
                anchors_valid_value = keyword_value
            .anchors_visible:
                if anchors_visible_value != none:
                    return none
                anchors_visible_value = keyword_value
            .no_overflow:
                if no_overflow_value != none:
                    return none
                no_overflow_value = keyword_value
            _:
                return none
        css_syntax.token_stream_discard_whitespace(tokens)

    if let value = anchors_valid_value:
        ak.vector_append_t(&values, value)
    if let value = anchors_visible_value:
        ak.vector_append_t(&values, value)
    if let value = no_overflow_value:
        ak.vector_append_t(&values, value)

    if ak.vector_is_empty(&values):
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_style_value_list_of(values, CssStyleValueListSeparator.space)

# mark: quotes ==================================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3927 Web::CSS::Parser::Parser::parse_quotes_value.
pub func css_parser_parse_quotes_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # https://www.w3.org/TR/css-content-3/#quotes-property
    # auto | none | [ <string> <string> ]+

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

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

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

    # Parse an even number of <string> values.
    var string_values = ak.Vector[CssStyleValue*]()
    while css_syntax.token_stream_has_next_token(tokens):
        let maybe_string = css_parser_parse_string_value(this, tokens) else return none

        ak.vector_append_t(&string_values, maybe_string.css_style_value())
        css_syntax.token_stream_discard_whitespace(tokens)

    if ak.vector_size(&string_values) % 2 != 0:
        return none

    css_syntax.state_transaction_commit(&transaction)
    return css_style_value_list_of(string_values, CssStyleValueListSeparator.space)

# mark: A repeat style ==========================================================================

## The `is_directional_repeat` lambda of parse_single_repeat_style_value.
func css_is_directional_repeat(value: CssStyleValue*) -> bool:
    let keyword = css_style_value_to_keyword(value)
    return keyword == css_data.Keyword.repeat_x or keyword == css_data.Keyword.repeat_y

## The `as_repeat` lambda of parse_single_repeat_style_value.
func css_as_repeat(keyword: css_data.Keyword) -> css_data.Repetition?:
    match keyword:
        .no_repeat:
            return css_data.Repetition.no_repeat
        .repeat:
            return css_data.Repetition.repeat
        .round:
            return css_data.Repetition.round
        .space:
            return css_data.Repetition.space
        _:
            return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3959 Web::CSS::Parser::Parser::parse_single_repeat_style_value.
pub func css_parser_parse_single_repeat_style_value(this: CssParser*, property: css_data.PropertyId, 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 `is_directional_repeat` and `as_repeat` lambdas are css_is_directional_repeat and css_as_repeat.

    let x_value = css_parser_parse_css_value_for_property(this, property, tokens) else return none

    if css_is_directional_repeat(x_value):
        let keyword = css_style_value_to_keyword(x_value)
        css_syntax.state_transaction_commit(&transaction)
        return css_repeat_style_style_value_create(
            css_data.Repetition.repeat if keyword == css_data.Keyword.repeat_x else css_data.Repetition.no_repeat,
            css_data.Repetition.no_repeat if keyword == css_data.Keyword.repeat_x else css_data.Repetition.repeat).css_style_value()

    let x_repeat = css_as_repeat(css_style_value_to_keyword(x_value)) else return none

    # See if we have a second value for Y
    let maybe_y_value = css_parser_parse_css_value_for_property(this, property, tokens)
    if maybe_y_value == none:
        # We don't have a second value, so use x for both
        css_syntax.state_transaction_commit(&transaction)
        return css_repeat_style_style_value_create(x_repeat, x_repeat).css_style_value()
    let y_value = maybe_y_value else trap("unreachable")
    if css_is_directional_repeat(y_value):
        return none

    let y_repeat = css_as_repeat(css_style_value_to_keyword(y_value)) else return none

    css_syntax.state_transaction_commit(&transaction)
    return css_repeat_style_style_value_create(x_repeat, y_repeat).css_style_value()

# mark: text-decoration =========================================================================

## The block of parse_text_decoration_value's loop that parses the next value for one of the remaining longhands,
## committing its transaction unless the value is a text-decoration-line (parsed again in full below).
func css_parser_parse_text_decoration_longhand(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*,
                                               remaining_longhands: const ak.Vector[css_data.PropertyId]*) -> CssParserPropertyAndValue?:
    var value_transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&value_transaction)
    let property_and_value = css_parser_parse_css_value_for_properties(this, ak.vector_span_const(remaining_longhands), tokens) else return none
    # FIXME: text-decoration-line can't be parsed fully with parse_css_value_for_properties(), so don't commit
    #        the transaction, and then parse it manually below.
    if property_and_value.property != css_data.PropertyId.text_decoration_line:
        css_syntax.state_transaction_commit(&value_transaction)
    return property_and_value

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:4019 Web::CSS::Parser::Parser::parse_text_decoration_value.
pub func css_parser_parse_text_decoration_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    var decoration_line: CssStyleValue*? = none
    var decoration_thickness: CssStyleValue*? = none
    var decoration_style: CssStyleValue*? = none
    var decoration_color: CssStyleValue*? = none

    var remaining_longhands = css_property_ids_of([css_data.PropertyId.text_decoration_color, css_data.PropertyId.text_decoration_line, css_data.PropertyId.text_decoration_style,
                                                   css_data.PropertyId.text_decoration_thickness])

    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_text_decoration_longhand(this, tokens, &remaining_longhands) else return none
        let property = property_and_value.property
        let value = property_and_value.style_value
        css_remove_property(&remaining_longhands, property)

        match property:
            .text_decoration_color:
                assert(decoration_color == none)
                decoration_color = value else trap("release_nonnull")
                continue
            .text_decoration_line:
                assert(decoration_line == none)
                # NB: The tokens for this didn't get consumed, see above. So we parse it fully now.
                let parsed_decoration_line = css_parser_parse_text_decoration_line_value(this, tokens) else return none
                decoration_line = parsed_decoration_line
                continue
            .text_decoration_thickness:
                assert(decoration_thickness == none)
                decoration_thickness = value else trap("release_nonnull")
                continue
            .text_decoration_style:
                assert(decoration_style == none)
                decoration_style = value else trap("release_nonnull")
                continue
            _:
                trap("VERIFY_NOT_REACHED")

    if decoration_line == none:
        decoration_line = css_property_initial_value(css_data.PropertyId.text_decoration_line)
    if decoration_thickness == none:
        decoration_thickness = css_property_initial_value(css_data.PropertyId.text_decoration_thickness)
    if decoration_style == none:
        decoration_style = css_property_initial_value(css_data.PropertyId.text_decoration_style)
    if decoration_color == none:
        decoration_color = css_property_initial_value(css_data.PropertyId.text_decoration_color)

    css_syntax.state_transaction_commit(&transaction)
    return css_shorthand_of(css_data.PropertyId.text_decoration,
                            css_property_ids_of([css_data.PropertyId.text_decoration_line, css_data.PropertyId.text_decoration_thickness, css_data.PropertyId.text_decoration_style,
                                                 css_data.PropertyId.text_decoration_color]),
                            css_style_values_of([decoration_line else trap("release_nonnull"), decoration_thickness else trap("release_nonnull"),
                                                 decoration_style else trap("release_nonnull"), decoration_color else trap("release_nonnull")]))