Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   property_parsing_10 - parsing property values: math-depth, overflow-clip-margin,
#                         paint-order, place-*, position-anchor, position-try-fallbacks
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/PropertyParsing.cpp (lines 3286-3621).
#
#==============================================================================================

# mark: math-depth ==============================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3286 Web::CSS::Parser::Parser::parse_math_depth_value.
pub func css_parser_parse_math_depth_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # https://w3c.github.io/mathml-core/#propdef-math-depth
    # auto-add | add(<integer>) | <integer>
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    # auto-add
    if let keyword = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.auto_add):
        css_syntax.state_transaction_commit(&transaction)
        return keyword

    # add(<integer>)
    let next = css_syntax.token_stream_next_token(tokens)
    if css_syntax.component_value_is_function_string_view(&next, ak.sv("add")):
        let function = css_syntax.component_value_function(&next)
        css_parser_push_temporary_value_parsing_context(this, CssValueParsingContext.css_function_context(value = CssFunctionContext(name = ak.fly_string_bytes_as_string_view(&function.name))))
        defer css_parser_pop_temporary_value_parsing_context(this)

        var add_tokens = css_syntax.token_stream_create(function.value)
        css_syntax.token_stream_discard_whitespace(&add_tokens)
        if let integer_value = css_parser_parse_integer_value(this, &add_tokens, css_infinite_integer_range):
            css_syntax.token_stream_discard_whitespace(&add_tokens)
            if css_syntax.token_stream_has_next_token(&add_tokens):
                return none
            css_syntax.token_stream_discard_a_token(tokens)  # add()
            css_syntax.state_transaction_commit(&transaction)
            return css_function_style_value_create(ak.fly_string_from_str("add"), integer_value).css_style_value()
        return none

    # <integer>
    if let integer_value = css_parser_parse_integer_value(this, tokens, css_infinite_integer_range):
        css_syntax.state_transaction_commit(&transaction)
        return integer_value

    return none

# mark: overflow-clip-margin ====================================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3326 Web::CSS::Parser::Parser::parse_overflow_clip_margin_value.
# https://drafts.csswg.org/css-overflow-4/#overflow-clip-margin
pub func css_parser_parse_overflow_clip_margin_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <visual-box> || <length [0,∞]>
    # FIXME: Implement the <visual-box> part of this.

    if let length = css_parser_parse_length_value(this, tokens, css_non_negative_range):
        return length

    return none

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3338 Web::CSS::Parser::Parser::parse_overflow_clip_margin_shorthand.
pub func css_parser_parse_overflow_clip_margin_shorthand(this: CssParser*, property_id: css_data.PropertyId, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <visual-box> || <length [0,∞]>
    # FIXME: Implement the <visual-box> part of this.

    if let value = css_parser_parse_overflow_clip_margin_value(this, tokens):
        let longhands = css_data.longhands_for_shorthand(property_id)
        var longhand_values = ak.Vector[CssStyleValue*]()
        ak.vector_resize_with_default_value(&longhand_values, ak.vector_size(&longhands), value)

        return css_shorthand_of(property_id, longhands, longhand_values)

    return none

# mark: paint-order =============================================================================

## The flags of parse_paint_order_value that its `parse_paint_order_keyword` lambda captures by reference.
struct CssParserParsePaintOrderValueState:
    var has_fill: bool
    var has_stroke: bool
    var has_markers: bool

## The `parse_paint_order_keyword` lambda of parse_paint_order_value.
func css_parser_parse_paint_order_keyword(this: CssParser*, inner_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, state: CssParserParsePaintOrderValueState*) -> CssStyleValue*?:
    let maybe_value = css_parser_parse_keyword_value(this, inner_tokens) else return none

    match css_style_value_to_keyword(maybe_value):
        .fill:
            if state.has_fill:
                return none
            state.has_fill = true
        .markers:
            if state.has_markers:
                return none
            state.has_markers = true
        .stroke:
            if state.has_stroke:
                return none
            state.has_stroke = true
        _:
            return none

    return maybe_value

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3354 Web::CSS::Parser::Parser::parse_paint_order_value.
pub func css_parser_parse_paint_order_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    if let normal = css_parser_parse_all_as_single_keyword_value(this, tokens, css_data.Keyword.normal):
        return normal

    var state = CssParserParsePaintOrderValueState(has_fill = false, has_stroke = false, has_markers = false)

    # NB: The `parse_paint_order_keyword` lambda is css_parser_parse_paint_order_keyword.

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

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    let first_keyword_value = css_parser_parse_paint_order_keyword(this, tokens, &state) 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 first_keyword_value

    let second_keyword_value = css_parser_parse_paint_order_keyword(this, tokens, &state) else return none

    css_syntax.token_stream_discard_whitespace(tokens)
    if css_syntax.token_stream_has_next_token(tokens):
        # The third keyword is parsed to ensure it is valid, but it doesn't get added to the list. This follows the
        # shortest-serialization principle, as the value can always be inferred.
        if css_parser_parse_paint_order_keyword(this, tokens, &state) == none:
            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 expected_second_keyword = css_data.Keyword.fill
    if css_style_value_to_keyword(first_keyword_value) == css_data.Keyword.fill:
        expected_second_keyword = css_data.Keyword.stroke

    if expected_second_keyword == css_style_value_to_keyword(second_keyword_value):
        return first_keyword_value

    return css_style_value_list_of(css_style_values_of([first_keyword_value, second_keyword_value]), CssStyleValueListSeparator.space)

# mark: place-content, place-items, place-self ==================================================

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3432 Web::CSS::Parser::Parser::parse_place_content_value.
pub func css_parser_parse_place_content_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)
    let maybe_align_content_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.align_content, tokens) else return none

    if not css_syntax.token_stream_has_next_token(tokens):
        if not css_data.property_accepts_keyword(css_data.PropertyId.justify_content, css_style_value_to_keyword(maybe_align_content_value)):
            return none
        css_syntax.state_transaction_commit(&transaction)
        return css_shorthand_of(css_data.PropertyId.place_content,
                                css_property_ids_of([css_data.PropertyId.align_content, css_data.PropertyId.justify_content]),
                                css_style_values_of([maybe_align_content_value, maybe_align_content_value]))

    let maybe_justify_content_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.justify_content, tokens) else return none
    css_syntax.state_transaction_commit(&transaction)
    return css_shorthand_of(css_data.PropertyId.place_content,
                            css_property_ids_of([css_data.PropertyId.align_content, css_data.PropertyId.justify_content]),
                            css_style_values_of([maybe_align_content_value, maybe_justify_content_value]))

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3457 Web::CSS::Parser::Parser::parse_place_items_value.
pub func css_parser_parse_place_items_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)
    let maybe_align_items_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.align_items, tokens) else return none

    if not css_syntax.token_stream_has_next_token(tokens):
        if not css_data.property_accepts_keyword(css_data.PropertyId.justify_items, css_style_value_to_keyword(maybe_align_items_value)):
            return none
        css_syntax.state_transaction_commit(&transaction)
        return css_shorthand_of(css_data.PropertyId.place_items,
                                css_property_ids_of([css_data.PropertyId.align_items, css_data.PropertyId.justify_items]),
                                css_style_values_of([maybe_align_items_value, maybe_align_items_value]))

    let maybe_justify_items_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.justify_items, tokens) else return none
    css_syntax.state_transaction_commit(&transaction)
    return css_shorthand_of(css_data.PropertyId.place_items,
                            css_property_ids_of([css_data.PropertyId.align_items, css_data.PropertyId.justify_items]),
                            css_style_values_of([maybe_align_items_value, maybe_justify_items_value]))

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3482 Web::CSS::Parser::Parser::parse_place_self_value.
pub func css_parser_parse_place_self_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)
    let maybe_align_self_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.align_self, tokens) else return none

    if not css_syntax.token_stream_has_next_token(tokens):
        if not css_data.property_accepts_keyword(css_data.PropertyId.justify_self, css_style_value_to_keyword(maybe_align_self_value)):
            return none
        css_syntax.state_transaction_commit(&transaction)
        return css_shorthand_of(css_data.PropertyId.place_self,
                                css_property_ids_of([css_data.PropertyId.align_self, css_data.PropertyId.justify_self]),
                                css_style_values_of([maybe_align_self_value, maybe_align_self_value]))

    let maybe_justify_self_value = css_parser_parse_css_value_for_property(this, css_data.PropertyId.justify_self, tokens) else return none
    css_syntax.state_transaction_commit(&transaction)
    return css_shorthand_of(css_data.PropertyId.place_self,
                            css_property_ids_of([css_data.PropertyId.align_self, css_data.PropertyId.justify_self]),
                            css_style_values_of([maybe_align_self_value, maybe_justify_self_value]))

# mark: position-anchor and position-try-fallbacks ==============================================

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

    # <anchor-name> = <dashed-ident>
    return (CssStyleValue*?)css_parser_parse_dashed_ident_value(this, tokens)

## `[&](auto& inner_tokens) { return parse_single_position_try_fallbacks_value(inner_tokens); }` (the context is the
## parser).
func css_parser_parse_single_position_try_fallbacks_callback(context: void*?, inner_tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let this = (CssParser*)(context else trap("no parser"))
    return css_parser_parse_single_position_try_fallbacks_value(this, inner_tokens)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3523 Web::CSS::Parser::Parser::parse_position_try_fallbacks_value.
# https://drafts.csswg.org/css-anchor-position/#position-try-fallbacks
pub func css_parser_parse_position_try_fallbacks_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # none | [ [<dashed-ident> || <try-tactic>] | <position-area> ]#

    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_single_position_try_fallbacks_callback)

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3535 Web::CSS::Parser::Parser::parse_single_position_try_fallbacks_value.
pub func css_parser_parse_single_position_try_fallbacks_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # [<dashed-ident> || <try-tactic>] | <position-area>
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    if let position_area = css_parser_parse_position_area(this, tokens):
        css_syntax.state_transaction_commit(&transaction)
        return position_area

    var dashed_ident: CssStyleValue*? = none
    var try_tactic: CssStyleValue*? = none
    while css_syntax.token_stream_has_next_token(tokens):
        if let try_tactic_value = css_parser_parse_try_tactic_value(this, tokens):
            if try_tactic != none:
                return none
            try_tactic = try_tactic_value
        elif let maybe_dashed_ident = css_parser_parse_dashed_ident_value(this, tokens):
            if dashed_ident != none:
                return none
            dashed_ident = maybe_dashed_ident.css_style_value()
        else:
            break
        css_syntax.token_stream_discard_whitespace(tokens)

    var values = ak.Vector[CssStyleValue*]()
    if let ident = dashed_ident:
        ak.vector_append_t(&values, ident)
    if let tactic = try_tactic:
        ak.vector_append_t(&values, tactic)

    if ak.vector_is_empty(&values):
        return none

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

## Ported from Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp:3577 Web::CSS::Parser::Parser::parse_try_tactic_value.
# https://drafts.csswg.org/css-anchor-position-1/#typedef-position-try-fallbacks-try-tactic
pub func css_parser_parse_try_tactic_value(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    # <try-tactic> = flip-block || flip-inline || flip-start
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)
    var values = ak.Vector[CssStyleValue*]()
    var has_flip_block = false
    var has_flip_inline = false
    var has_flip_start = false
    while css_syntax.token_stream_has_next_token(tokens):
        let keyword_value = css_parser_parse_keyword_value(this, tokens) else break
        match css_style_value_to_keyword(keyword_value):
            .flip_block:
                if has_flip_block:
                    return none
                has_flip_block = true
                ak.vector_append_t(&values, keyword_value)
            .flip_inline:
                if has_flip_inline:
                    return none
                has_flip_inline = true
                ak.vector_append_t(&values, keyword_value)
            .flip_start:
                if has_flip_start:
                    return none
                has_flip_start = true
                ak.vector_append_t(&values, keyword_value)
            _:
                return none
        css_syntax.token_stream_discard_whitespace(tokens)

    css_syntax.state_transaction_commit(&transaction)
    if ak.vector_is_empty(&values):
        return none
    return css_style_value_list_of(values, CssStyleValueListSeparator.space)